-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/14 user entity #30
base: dev
Are you sure you want to change the base?
Changes from all commits
37220b5
149edbe
bc29368
5a9e0e7
ca8bb3e
fdd3f41
3703b14
c7d7ebc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,7 @@ dependencies { | |
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
bootJar{ | ||
archivesBaseName = 'app' | ||
archiveFileName = 'app.jar' | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package playlist.server.config.security; | ||
|
||
|
||
import java.util.List; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.util.CollectionUtils; | ||
import playlist.server.exception.SecurityContextNotFoundException; | ||
|
||
public class SecurityUtils { | ||
|
||
private static SimpleGrantedAuthority swagger = new SimpleGrantedAuthority("ROLE_SWAGGER"); | ||
|
||
private static List<SimpleGrantedAuthority> notUserAuthority = List.of(swagger); | ||
|
||
public static Long getCurrentUserId() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
throw SecurityContextNotFoundException.EXCEPTION; | ||
} | ||
|
||
if (authentication.isAuthenticated() | ||
&& !CollectionUtils.containsAny( | ||
authentication.getAuthorities(), notUserAuthority)) { | ||
return authentication.getName().equals("anonymousUser") | ||
? 0L | ||
: Long.valueOf(authentication.getName()); | ||
} | ||
// μ€μ¨κ±° μ μ μΌμ μ μ μμ΄λ 0 λ°ν | ||
return 0L; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package playlist.server.user.controller; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import playlist.server.user.model.dto.UserCreateDTO; | ||
import playlist.server.user.service.devLoginService; | ||
|
||
@RestController | ||
@RequestMapping("/users") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class UserController { | ||
|
||
private final devLoginService devLoginService; | ||
|
||
@GetMapping("/create") | ||
public void createDevUser(@RequestParam String email, @RequestParam String password) { | ||
log.info("email, password : " + email + " " + password); | ||
|
||
UserCreateDTO userCreateDTO = new UserCreateDTO(email, password); | ||
devLoginService.execute(userCreateDTO); | ||
|
||
// model.addAttribute("email", email); | ||
// model.addAttribute("password",password); | ||
|
||
} | ||
} | ||
Comment on lines
+21
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RequestParamμΌλ‘ κ°μ΄ λκ°λ λ€μ΄κ°λ κ²μ μ’μ§ μμ΅λλ€ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ€ μ°Έκ³ νκ² μ΅λλ€ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. νμΈνμ΅λλ€ μΆν API ν리νμ€νΈμ λ°μν΄μ μ¬λ¦¬κ² μ΅λλ€ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package playlist.server.user.model.dto; | ||
|
||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UserCreateDTO { | ||
private final String email; | ||
private final String password; | ||
|
||
// @QueryProjection | ||
public UserCreateDTO(String email, String password) { | ||
this.email = email; | ||
this.password = password; | ||
} | ||
} | ||
Comment on lines
+7
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ’μ΅λλ€ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package playlist.server.user.service; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import playlist.server.domain.domains.auth.adaptor.UserAdaptor; | ||
import playlist.server.domain.domains.auth.domain.AccountRole; | ||
import playlist.server.domain.domains.auth.domain.User; | ||
import playlist.server.user.model.dto.UserCreateDTO; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class devLoginService { | ||
private final UserAdaptor userAdaptor; | ||
|
||
public void execute(UserCreateDTO userCreateDTO) { | ||
User user = | ||
User.builder() | ||
.email(userCreateDTO.getEmail()) | ||
.password(userCreateDTO.getPassword()) | ||
.role(AccountRole.ADMIN) | ||
.build(); | ||
userAdaptor.save(user); | ||
} | ||
Comment on lines
+18
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @builder ν¨ν΄μΌλ‘ ꡬνν κ² μ’μ΅λλ€ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package playlist.server.utils; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import playlist.server.config.security.SecurityUtils; | ||
import playlist.server.domain.domains.auth.adaptor.UserAdaptor; | ||
import playlist.server.domain.domains.auth.domain.User; | ||
|
||
@RequiredArgsConstructor | ||
public class UserUtils { | ||
|
||
private final UserAdaptor userAdaptor; | ||
|
||
public Long getUserId() { | ||
return SecurityUtils.getCurrentUserId(); | ||
} | ||
|
||
public User getUser() { | ||
return userAdaptor.query(SecurityUtils.getCurrentUserId()); | ||
} | ||
} | ||
Comment on lines
+10
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utils classλ‘ μμ£Ό μ¨μΌνλ λ©μλλ₯Ό λ°λ‘ ꡬνν΄λμ κ² μ’λ€μ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package playlist.server.exception; | ||
|
||
public class SecurityContextNotFoundException extends BaseException { | ||
|
||
public static final BaseException EXCEPTION = new SecurityContextNotFoundException(); | ||
|
||
private SecurityContextNotFoundException() { | ||
super(GlobalException.SECURITY_CONTEXT_NOT_FOUND_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package playlist.server.domain.domains; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import java.sql.Timestamp; | ||
import lombok.Getter; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import org.hibernate.annotations.UpdateTimestamp; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(value = {AuditingEntityListener.class}) | ||
public abstract class AbstractTimeStamp { | ||
|
||
@Column( | ||
name = "created_at", | ||
nullable = false, | ||
columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") | ||
@CreationTimestamp | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss") | ||
private Timestamp createdAt; | ||
|
||
@Column( | ||
name = "updated_at", | ||
nullable = false, | ||
columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") | ||
@UpdateTimestamp | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss") | ||
private Timestamp updatedAt; | ||
} | ||
Comment on lines
+14
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AbstractTimeStamp μΆμν΄λμ€λ₯Ό μ΄μ©ν΄μ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package playlist.server.domain.domains.auth.Exception; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import playlist.server.dto.ErrorDetail; | ||
import playlist.server.exception.BaseErrorCode; | ||
|
||
@RequiredArgsConstructor | ||
public enum UserException implements BaseErrorCode { | ||
USER_NOT_FOUND_ERROR(NOT_FOUND.value(), "User_404_1", "μ μ λ₯Ό μ°Ύμ μ μμ΅λλ€."); | ||
|
||
private final Integer statusCode; | ||
private final String errorCode; | ||
private final String reason; | ||
|
||
@Override | ||
public ErrorDetail getErrorDetail() { | ||
return ErrorDetail.of(statusCode, errorCode, reason); | ||
} | ||
} | ||
Comment on lines
+10
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μλ‘μ΄ Exceptionμ λ§λ κ² μμ£Ό μ’μ΅λλ€ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package playlist.server.domain.domains.auth.Exception; | ||
|
||
|
||
import playlist.server.exception.BaseException; | ||
|
||
public class UserNotFoundException extends BaseException { | ||
public static final BaseException EXCEPTION = new UserNotFoundException(); | ||
|
||
private UserNotFoundException() { | ||
super(UserException.USER_NOT_FOUND_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package playlist.server.domain.domains.auth.adaptor; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import playlist.server.annotation.Adaptor; | ||
import playlist.server.domain.domains.auth.Exception.UserNotFoundException; | ||
import playlist.server.domain.domains.auth.domain.User; | ||
import playlist.server.domain.domains.auth.repository.UserRepository; | ||
|
||
@Adaptor | ||
@RequiredArgsConstructor | ||
public class UserAdaptor { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public User query(Long userId) { | ||
return userRepository.findById(userId).orElseThrow(() -> UserNotFoundException.EXCEPTION); | ||
} | ||
|
||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄λΆλΆμ orElseThrow()λ μ΄λ€ κ²½μ°μ μΈ μ μμκΉμ |
||
public User save(User user) { | ||
return userRepository.save(user); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package playlist.server.domain.domains.auth.domain; | ||
|
||
public enum AccountRole { | ||
USER, | ||
ADMIN; | ||
} | ||
Comment on lines
+3
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. USERμ κ²½μ° There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ£Όμμ λ¨κΈ°λ©΄ μ’κ² λ€λ 건κ°μ? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package playlist.server.domain.domains.auth.domain; | ||
|
||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import playlist.server.domain.domains.AbstractTimeStamp; | ||
|
||
@Getter | ||
@Entity | ||
// @Table(name = "follow") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Follow extends AbstractTimeStamp { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
// μ΄ λ°μΌλ‘λ λκ° κΈ°λ‘ν λ§ν μμ λ€ | ||
|
||
@Column(nullable = false) | ||
private Long userId; | ||
|
||
@Column(nullable = false) | ||
private Long followUserId; | ||
} | ||
Comment on lines
+20
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. κ΅Ώ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package playlist.server.domain.domains.auth.domain; | ||
|
||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import playlist.server.domain.domains.AbstractTimeStamp; | ||
|
||
@Getter | ||
@Entity | ||
// @Table(name = "following") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Following extends AbstractTimeStamp { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
// μ΄ λ°μΌλ‘λ λκ° κΈ°λ‘ν λ§ν μμ λ€ | ||
|
||
@Column(nullable = false) | ||
private Long userId; | ||
|
||
@Column(nullable = false) | ||
private Long followingUserId; | ||
} | ||
Comment on lines
+14
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. κ΅Ώ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μκ°ν΄λ³Όκ²μ.. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package playlist.server.domain.domains.auth.domain; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import java.sql.Timestamp; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import playlist.server.domain.domains.AbstractTimeStamp; | ||
|
||
@Getter | ||
@Entity | ||
@Table(name = "user_login_history") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class History extends AbstractTimeStamp { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(name = "login_at", nullable = false) | ||
@CreationTimestamp | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss") | ||
private Timestamp loginAt; | ||
|
||
// μ΄ λ°μΌλ‘λ λκ° κΈ°λ‘ν λ§ν μμ λ€ | ||
|
||
@Column(nullable = false, length = 200) | ||
private String param1; | ||
|
||
@Column(nullable = false, length = 200) | ||
private String param2; | ||
|
||
@Column(nullable = false, length = 200) | ||
private String param3; | ||
|
||
@Column(nullable = false, length = 200) | ||
private String param4; | ||
|
||
@Column(nullable = false, length = 200) | ||
private String param5; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package playlist.server.domain.domains.auth.domain; | ||
|
||
public enum LoginType { | ||
DEFAULT, | ||
KAKAO, | ||
NAVER, | ||
GOOGLE, | ||
APPLE, | ||
GITHUB | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ΅Ώκ΅Ώ μ’μ΅λλ€