From ef919b766c92074b6ad03def7c5750f0eb639598 Mon Sep 17 00:00:00 2001 From: yeon015 Date: Wed, 21 Aug 2024 01:02:30 +0900 Subject: [PATCH] =?UTF-8?q?feat(#191)=20:=20=ED=83=88=ED=87=B4=20=EC=82=AC?= =?UTF-8?q?=EC=9C=A0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/ErrorStatus.java | 5 ++- .../juinjang/controller/OAuthController.java | 24 +++++++++++- .../dto/auth/WithdrawReasonRequestDto.java | 15 ++++++++ .../th/juinjang/model/entity/Withdraw.java | 31 ++++++++++++++++ .../model/entity/enums/WithdrawReason.java | 11 ++++++ .../withdraw/WithdrawRepository.java | 11 ++++++ .../WithdrawService/WithdrawService.java | 37 +++++++++++++++++++ 7 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/main/java/umc/th/juinjang/model/dto/auth/WithdrawReasonRequestDto.java create mode 100644 src/main/java/umc/th/juinjang/model/entity/Withdraw.java create mode 100644 src/main/java/umc/th/juinjang/model/entity/enums/WithdrawReason.java create mode 100644 src/main/java/umc/th/juinjang/repository/withdraw/WithdrawRepository.java create mode 100644 src/main/java/umc/th/juinjang/service/WithdrawService/WithdrawService.java diff --git a/src/main/java/umc/th/juinjang/apiPayload/code/status/ErrorStatus.java b/src/main/java/umc/th/juinjang/apiPayload/code/status/ErrorStatus.java index f9acf1c..8851e39 100644 --- a/src/main/java/umc/th/juinjang/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/umc/th/juinjang/apiPayload/code/status/ErrorStatus.java @@ -90,7 +90,10 @@ public enum ErrorStatus implements BaseErrorCode { S3_DELTE_FAILED(HttpStatus.NOT_FOUND, "S34000", "해당 file이 s3에 존재하지 않습니다. 백엔드 팀에 문의바랍니다."), //record 에러 - RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "RECORD400", "record가 존재하지 않습니다"); + RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "RECORD400", "record가 존재하지 않습니다"), + + //withdraw 에러 + WITHDRAW_REASON_NOT_FOUND(HttpStatus.NOT_FOUND, "WITHDRAW400", "해당 탈퇴 사유 enum 값이 존재하지 않습니다"); private final HttpStatus httpStatus; diff --git a/src/main/java/umc/th/juinjang/controller/OAuthController.java b/src/main/java/umc/th/juinjang/controller/OAuthController.java index 4f65b35..bb7ee46 100644 --- a/src/main/java/umc/th/juinjang/controller/OAuthController.java +++ b/src/main/java/umc/th/juinjang/controller/OAuthController.java @@ -16,15 +16,20 @@ import umc.th.juinjang.apiPayload.code.status.SuccessStatus; import umc.th.juinjang.apiPayload.exception.handler.MemberHandler; import umc.th.juinjang.model.dto.auth.LoginResponseDto; +import umc.th.juinjang.model.dto.auth.WithdrawReasonRequestDto; import umc.th.juinjang.model.dto.auth.apple.AppleLoginRequestDto; import umc.th.juinjang.model.dto.auth.apple.AppleSignUpRequestDto; import umc.th.juinjang.model.dto.auth.kakao.KakaoLoginRequestDto; import umc.th.juinjang.model.dto.auth.kakao.KakaoSignUpRequestDto; import umc.th.juinjang.model.entity.Member; +import umc.th.juinjang.model.entity.Withdraw; +import umc.th.juinjang.repository.withdraw.WithdrawRepository; import umc.th.juinjang.service.JwtService; +import umc.th.juinjang.service.WithdrawService.WithdrawService; import umc.th.juinjang.service.auth.OAuthService; import java.io.IOException; +import java.util.List; import static umc.th.juinjang.apiPayload.code.status.ErrorStatus.*; @@ -36,6 +41,9 @@ public class OAuthController { private final OAuthService oauthService; + private final WithdrawService withdrawService; + + private final WithdrawRepository withdrawRepository; // 카카오 로그인 @@ -102,7 +110,7 @@ public ApiResponse appleSignUp(@RequestBody @Validated AppleSi // 카카오 탈퇴 @DeleteMapping("/withdraw/kakao") - public ApiResponse kakaoWithdraw(@AuthenticationPrincipal Member member, @RequestHeader("target-id") String kakaoTargetId) { + public ApiResponse kakaoWithdraw(@AuthenticationPrincipal Member member, @RequestHeader("target-id") String kakaoTargetId, @RequestBody @Validated WithdrawReasonRequestDto withdrawReasonReqDto) { Long targetId; if(kakaoTargetId == null) { @@ -117,6 +125,9 @@ public ApiResponse kakaoWithdraw(@AuthenticationPrincipal Member member, @Reques // 카카오 계정 연결 끊기 boolean isUnlink = oauthService.kakaoWithdraw(member, targetId); + // 탈퇴 사유 추가 + withdrawService.addWithdrawReason(withdrawReasonReqDto.getWithdrawReason()); + // 사용자 정보 삭제 (DB) if (!isUnlink) { throw new ExceptionHandler(NOT_UNLINK_KAKAO); @@ -130,8 +141,17 @@ public ApiResponse kakaoWithdraw(@AuthenticationPrincipal Member member, @Reques // 애플 탈퇴 @DeleteMapping("/withdraw/apple") public ApiResponse withdraw(@AuthenticationPrincipal Member member, - @Nullable@RequestHeader("X-Apple-Code") final String code){ + @Nullable@RequestHeader("X-Apple-Code") final String code, @RequestBody @Validated WithdrawReasonRequestDto withdrawReasonReqDto){ oauthService.appleWithdraw(member, code); + // 탈퇴 사유 추가 + withdrawService.addWithdrawReason(withdrawReasonReqDto.getWithdrawReason()); return ApiResponse.onSuccess(SuccessStatus.MEMBER_DELETE); } + + // count 임시 test용 (최종 탈퇴 테스트 완료 후 지울 예정) +// @PostMapping("/withdraw/count") +// public ApiResponse> withdrawReason(@RequestBody @Validated WithdrawReasonRequestDto withdrawReasonReqDto) { +// withdrawService.addWithdrawReason(withdrawReasonReqDto.getWithdrawReason()); +// return ApiResponse.onSuccess(withdrawRepository.findAll()); +// } } \ No newline at end of file diff --git a/src/main/java/umc/th/juinjang/model/dto/auth/WithdrawReasonRequestDto.java b/src/main/java/umc/th/juinjang/model/dto/auth/WithdrawReasonRequestDto.java new file mode 100644 index 0000000..6addb2a --- /dev/null +++ b/src/main/java/umc/th/juinjang/model/dto/auth/WithdrawReasonRequestDto.java @@ -0,0 +1,15 @@ +package umc.th.juinjang.model.dto.auth; + +import jakarta.validation.constraints.NotEmpty; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class WithdrawReasonRequestDto { + @NotEmpty + List withdrawReason; +} diff --git a/src/main/java/umc/th/juinjang/model/entity/Withdraw.java b/src/main/java/umc/th/juinjang/model/entity/Withdraw.java new file mode 100644 index 0000000..28024dd --- /dev/null +++ b/src/main/java/umc/th/juinjang/model/entity/Withdraw.java @@ -0,0 +1,31 @@ +package umc.th.juinjang.model.entity; + +import jakarta.persistence.*; +import lombok.*; +import org.hibernate.annotations.ColumnDefault; +import umc.th.juinjang.model.entity.common.BaseEntity; +import umc.th.juinjang.model.entity.enums.WithdrawReason; + +@Entity +@Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class Withdraw extends BaseEntity { + + @Id + @Column(name="withdraw_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long withdrawId; + + @Column(name="reason") + @Enumerated(EnumType.STRING) + private WithdrawReason withdrawReason; + + @ColumnDefault("0") + private Long count; + + public void updateCount(Long count) { + this.count = count; + } +} diff --git a/src/main/java/umc/th/juinjang/model/entity/enums/WithdrawReason.java b/src/main/java/umc/th/juinjang/model/entity/enums/WithdrawReason.java new file mode 100644 index 0000000..fa71cc5 --- /dev/null +++ b/src/main/java/umc/th/juinjang/model/entity/enums/WithdrawReason.java @@ -0,0 +1,11 @@ +package umc.th.juinjang.model.entity.enums; + +public enum WithdrawReason { + + NOT_USE, // 더 이상 쓸 일이 없어요 + CANNOT_USE, // 앱 사용법을 모르겠어요 + NOT_HELP, // 임장에 도움이 되지 않아요 + CANNOT_FUNCTION, // 앱이 정상적으로 작동하지 않아요 + CONCERN_SECURITY, // 보안이 걱정돼요 + OTHER_SERVICE // 다른 서비스가 더 좋아요 +} diff --git a/src/main/java/umc/th/juinjang/repository/withdraw/WithdrawRepository.java b/src/main/java/umc/th/juinjang/repository/withdraw/WithdrawRepository.java new file mode 100644 index 0000000..89ceec6 --- /dev/null +++ b/src/main/java/umc/th/juinjang/repository/withdraw/WithdrawRepository.java @@ -0,0 +1,11 @@ +package umc.th.juinjang.repository.withdraw; + +import org.springframework.data.jpa.repository.JpaRepository; +import umc.th.juinjang.model.entity.Withdraw; +import umc.th.juinjang.model.entity.enums.WithdrawReason; + +import java.util.Optional; + +public interface WithdrawRepository extends JpaRepository { + Optional findByWithdrawReason(WithdrawReason withdrawReason); +} diff --git a/src/main/java/umc/th/juinjang/service/WithdrawService/WithdrawService.java b/src/main/java/umc/th/juinjang/service/WithdrawService/WithdrawService.java new file mode 100644 index 0000000..77b841a --- /dev/null +++ b/src/main/java/umc/th/juinjang/service/WithdrawService/WithdrawService.java @@ -0,0 +1,37 @@ +package umc.th.juinjang.service.WithdrawService; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import umc.th.juinjang.apiPayload.ExceptionHandler; +import umc.th.juinjang.apiPayload.code.status.ErrorStatus; +import umc.th.juinjang.model.entity.Withdraw; +import umc.th.juinjang.model.entity.enums.WithdrawReason; +import umc.th.juinjang.repository.withdraw.WithdrawRepository; + +import java.util.List; + +@Slf4j +@Service +@Transactional +@RequiredArgsConstructor +public class WithdrawService { + + private final WithdrawRepository withdrawRepository; + + public void addWithdrawReason(List withdrawReasons) { + for(String reason : withdrawReasons) { + WithdrawReason withdrawReason = WithdrawReason.valueOf(reason.toUpperCase()); + + Withdraw withdraw = withdrawRepository.findByWithdrawReason(withdrawReason) + .orElseThrow(() -> new ExceptionHandler(ErrorStatus.WITHDRAW_REASON_NOT_FOUND)); + + withdraw.updateCount(withdraw.getCount() + 1); + System.out.println("withdraw id : " + withdraw.getWithdrawId()); + System.out.println("withdraw enum : " + withdraw.getWithdrawReason()); + System.out.println("withdraw count : " + withdraw.getCount()); + withdrawRepository.save(withdraw); + } + } +}