diff --git a/api/src/main/java/org/yapp/domain/user/application/UserService.java b/api/src/main/java/org/yapp/domain/user/application/UserService.java index ff62909d..c0db4884 100644 --- a/api/src/main/java/org/yapp/domain/user/application/UserService.java +++ b/api/src/main/java/org/yapp/domain/user/application/UserService.java @@ -19,75 +19,80 @@ @RequiredArgsConstructor public class UserService { - private final UserRepository userRepository; - private final UserRejectHistoryRepository userRejectHistoryRepository; - private final JwtUtil jwtUtil; + private final UserRepository userRepository; + private final UserRejectHistoryRepository userRejectHistoryRepository; + private final JwtUtil jwtUtil; - /** - * Role을 USER로 바꾸고 변경된 토큰을 반환한다. - * - * @return 액세스토큰과 리프레시 토큰 - */ - @Transactional - public OauthLoginResponse completeProfileInitialize(Long userId, Profile profile) { - User user = - userRepository.findById(userId) - .orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER)); - user.setProfile(profile); - user.updateUserRole(RoleStatus.PENDING.getStatus()); - String oauthId = user.getOauthId(); - String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(), - 600000L); - String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(), - 864000000L); - return new OauthLoginResponse(RoleStatus.PENDING.getStatus(), accessToken, refreshToken); - } - - public User getUserById(Long userId) { - return userRepository.findById(userId) + /** + * Role을 USER로 바꾸고 변경된 토큰을 반환한다. + * + * @return 액세스토큰과 리프레시 토큰 + */ + @Transactional + public OauthLoginResponse completeProfileInitialize(Long userId, Profile profile) { + User user = + userRepository.findById(userId) .orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER)); - } + user.setProfile(profile); + user.updateUserRole(RoleStatus.PENDING.getStatus()); + String oauthId = user.getOauthId(); + String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(), + 600000L); + String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(), + 864000000L); + return new OauthLoginResponse(RoleStatus.PENDING.getStatus(), accessToken, refreshToken); + } - /** - * Role을 Register로 바꾸고 변경된 토큰을 반환한다. - * - * @return 액세스토큰과 리프레시 토큰 - */ - @Transactional - public OauthLoginResponse registerPhoneNumber(Long userId, String phoneNumber) { - User user = - userRepository.findById(userId) - .orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER)); - user.updateUserRole(RoleStatus.REGISTER.getStatus()); - user.initializePhoneNumber(phoneNumber); - String oauthId = user.getOauthId(); - String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(), - 600000L); - String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(), - 864000000L); - return new OauthLoginResponse(RoleStatus.REGISTER.getStatus(), accessToken, refreshToken); - } + public User getUserById(Long userId) { + return userRepository.findById(userId) + .orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER)); + } - @Transactional(readOnly = true) - public UserRejectHistoryResponse getUserRejectHistoryLatest(Long userId) { - User user = this.getUserById(userId); - Profile profile = user.getProfile(); + /** + * Role을 Register로 바꾸고 변경된 토큰을 반환한다. + * + * @return 액세스토큰과 리프레시 토큰 + */ + @Transactional + public OauthLoginResponse registerPhoneNumber(Long userId, String phoneNumber) { + User user = + userRepository.findById(userId) + .orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER)); + user.updateUserRole(RoleStatus.REGISTER.getStatus()); + user.initializePhoneNumber(phoneNumber); + String oauthId = user.getOauthId(); + String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(), + 600000L); + String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(), + 864000000L); + return new OauthLoginResponse(RoleStatus.REGISTER.getStatus(), accessToken, refreshToken); + } - boolean reasonImage = false; - boolean reasonDescription = false; + @Transactional(readOnly = true) + public UserRejectHistoryResponse getUserRejectHistoryLatest(Long userId) { + User user = this.getUserById(userId); + Profile profile = user.getProfile(); - UserRejectHistory userRejectHistory = userRejectHistoryRepository.findTopByUserIdOrderByCreatedAtDesc( - userId).orElse(null); + boolean reasonImage = false; + boolean reasonDescription = false; - if (userRejectHistory != null) { - reasonImage = userRejectHistory.isReasonImage(); - reasonDescription = userRejectHistory.isReasonDescription(); - } + UserRejectHistory userRejectHistory = userRejectHistoryRepository.findTopByUserIdOrderByCreatedAtDesc( + userId).orElse(null); - return new UserRejectHistoryResponse( - profile.getProfileStatus(), - reasonImage, - reasonDescription - ); + if (userRejectHistory != null) { + reasonImage = userRejectHistory.isReasonImage(); + reasonDescription = userRejectHistory.isReasonDescription(); } + + return new UserRejectHistoryResponse( + profile.getProfileStatus(), + reasonImage, + reasonDescription + ); + } + + @Transactional + public void deleteUser(Long userId) { + userRepository.deleteById(userId); + } } diff --git a/api/src/main/java/org/yapp/domain/user/presentation/UserController.java b/api/src/main/java/org/yapp/domain/user/presentation/UserController.java index 9c407aa3..e3c3b90d 100644 --- a/api/src/main/java/org/yapp/domain/user/presentation/UserController.java +++ b/api/src/main/java/org/yapp/domain/user/presentation/UserController.java @@ -6,6 +6,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -18,17 +19,25 @@ @RequestMapping("/api/users") public class UserController { - private final UserService userService; + private final UserService userService; - @PreAuthorize(value = "hasRole('USER')") - @GetMapping("/reject") - @Operation(summary = "사용자 거절 사유 조회", description = "사용자의 최근 거절 사유를 조회합니다.", tags = {"사용자"}) - public ResponseEntity> getUserRejectHistory( - @AuthenticationPrincipal Long userId) { - UserRejectHistoryResponse response = userService.getUserRejectHistoryLatest( - userId); - - return ResponseEntity.status(HttpStatus.OK) - .body(CommonResponse.createSuccess(response)); - } + @PreAuthorize(value = "hasRole('USER')") + @GetMapping("/reject") + @Operation(summary = "사용자 거절 사유 조회", description = "사용자의 최근 거절 사유를 조회합니다.", tags = {"사용자"}) + public ResponseEntity> getUserRejectHistory( + @AuthenticationPrincipal Long userId) { + UserRejectHistoryResponse response = userService.getUserRejectHistoryLatest( + userId); + + return ResponseEntity.status(HttpStatus.OK) + .body(CommonResponse.createSuccess(response)); + } + + @DeleteMapping + @PreAuthorize(value = "hasRole('USER')") + @Operation(summary = "회원 탈퇴", description = "회원 탈퇴합니다.", tags = {"사용자"}) + public ResponseEntity> deleteUser(@AuthenticationPrincipal Long userId) { + userService.deleteUser(userId); + return ResponseEntity.ok(CommonResponse.createSuccessWithNoContent()); + } } diff --git a/core/domain/src/main/java/org/yapp/core/domain/block/BlockContact.java b/core/domain/src/main/java/org/yapp/core/domain/block/BlockContact.java index 53cc5427..4db910cf 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/block/BlockContact.java +++ b/core/domain/src/main/java/org/yapp/core/domain/block/BlockContact.java @@ -15,6 +15,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.yapp.core.domain.BaseEntity; import org.yapp.core.domain.user.User; @@ -33,14 +35,15 @@ ) public class BlockContact extends BaseEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) - private User user; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) + private User user; - @Column(nullable = false) - private String phoneNumber; + @Column(nullable = false) + private String phoneNumber; } \ No newline at end of file diff --git a/core/domain/src/main/java/org/yapp/core/domain/match/MatchInfo.java b/core/domain/src/main/java/org/yapp/core/domain/match/MatchInfo.java index 72e886f4..3dab0053 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/match/MatchInfo.java +++ b/core/domain/src/main/java/org/yapp/core/domain/match/MatchInfo.java @@ -12,6 +12,8 @@ import java.time.LocalDate; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.yapp.core.domain.match.enums.UserMatchStatus; import org.yapp.core.domain.user.User; import org.yapp.core.exception.ApplicationException; @@ -31,6 +33,7 @@ public class MatchInfo { @ManyToOne @JoinColumn(name = "user_1") + @OnDelete(action = OnDeleteAction.CASCADE) private User user1; @Column(name = "user_1_match_status") @@ -39,6 +42,7 @@ public class MatchInfo { @ManyToOne @JoinColumn(name = "user_2") + @OnDelete(action = OnDeleteAction.CASCADE) private User user2; @Column(name = "user_2_match_status") diff --git a/core/domain/src/main/java/org/yapp/core/domain/report/Report.java b/core/domain/src/main/java/org/yapp/core/domain/report/Report.java index 2c1928de..44ac42ec 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/report/Report.java +++ b/core/domain/src/main/java/org/yapp/core/domain/report/Report.java @@ -11,6 +11,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.yapp.core.domain.BaseEntity; import org.yapp.core.domain.user.User; @@ -21,19 +23,21 @@ @Getter public class Report extends BaseEntity { - @Id - @Column(name = "report_id") - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @Column(name = "report_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @ManyToOne - @JoinColumn(name = "reporter_user_id", nullable = false) - private User reporter; + @ManyToOne + @JoinColumn(name = "reporter_user_id", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) + private User reporter; - @ManyToOne - @JoinColumn(name = "reported_user_id", nullable = false) - private User reportedUser; + @ManyToOne + @JoinColumn(name = "reported_user_id", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) + private User reportedUser; - @Column(length = 500) - private String reason; + @Column(length = 500) + private String reason; } diff --git a/core/domain/src/main/java/org/yapp/core/domain/term/TermAgreement.java b/core/domain/src/main/java/org/yapp/core/domain/term/TermAgreement.java index f9bf01d1..3c5bb192 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/term/TermAgreement.java +++ b/core/domain/src/main/java/org/yapp/core/domain/term/TermAgreement.java @@ -13,6 +13,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.yapp.core.domain.user.User; @Entity @@ -22,19 +24,20 @@ @Builder public class TermAgreement { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "agreement_id") - private long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "agreement_id") + private long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) - private User user; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + @OnDelete(action = OnDeleteAction.CASCADE) + private User user; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "term_id", nullable = false) - private Term term; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "term_id", nullable = false) + private Term term; - @Column(nullable = false) - private LocalDateTime agreedAt; + @Column(nullable = false) + private LocalDateTime agreedAt; } diff --git a/core/domain/src/main/java/org/yapp/core/domain/user/User.java b/core/domain/src/main/java/org/yapp/core/domain/user/User.java index 6689580d..b77412af 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/user/User.java +++ b/core/domain/src/main/java/org/yapp/core/domain/user/User.java @@ -1,5 +1,6 @@ package org.yapp.core.domain.user; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -23,37 +24,37 @@ @NoArgsConstructor public class User extends BaseEntity { - @Id - @Column(name = "user_id") - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @Column(name = "user_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @Column(name = "oauth_id") - private String oauthId; + @Column(name = "oauth_id") + private String oauthId; - @Column(name = "name") - private String name; + @Column(name = "name") + private String name; - @Column(name = "phone") - private String phoneNumber; + @Column(name = "phone") + private String phoneNumber; - @Column(name = "role") - private String role; + @Column(name = "role") + private String role; - @OneToOne - @JoinColumn(name = "profile_id", unique = true) // User가 profile_id를 FK로 가짐 - private Profile profile; + @OneToOne(cascade = {CascadeType.REMOVE}) + @JoinColumn(name = "profile_id", unique = true) // User가 profile_id를 FK로 가짐 + private Profile profile; - public void initializePhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } + public void initializePhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } - public void setProfile(Profile profile) { - this.profile = profile; - } + public void setProfile(Profile profile) { + this.profile = profile; + } - public void updateUserRole(String role) { - this.role = role; - } + public void updateUserRole(String role) { + this.role = role; + } } diff --git a/core/domain/src/main/java/org/yapp/core/domain/user/UserRejectHistory.java b/core/domain/src/main/java/org/yapp/core/domain/user/UserRejectHistory.java index 3bd62ee2..8dfaa6a2 100644 --- a/core/domain/src/main/java/org/yapp/core/domain/user/UserRejectHistory.java +++ b/core/domain/src/main/java/org/yapp/core/domain/user/UserRejectHistory.java @@ -13,6 +13,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.yapp.core.domain.BaseEntity; @Entity @@ -22,20 +24,21 @@ @Getter public class UserRejectHistory extends BaseEntity { - @Id - @Column(name = "user_reject_history_id") - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @Column(name = "user_reject_history_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id") - private User user; + @ManyToOne(fetch = FetchType.LAZY) + @OnDelete(action = OnDeleteAction.CASCADE) + @JoinColumn(name = "user_id") + private User user; - @ColumnDefault("false") - @Builder.Default - private boolean reasonImage = false; + @ColumnDefault("false") + @Builder.Default + private boolean reasonImage = false; - @Builder.Default - @ColumnDefault("false") - private boolean reasonDescription = false; + @Builder.Default + @ColumnDefault("false") + private boolean reasonDescription = false; }