Skip to content

Commit

Permalink
Feat : DIG-103 회원 탈퇴 기능 추가
Browse files Browse the repository at this point in the history
- soft delete 방식
- 일부 서비스 메서드 이름 수정
- 누락된 import 수정,  변수명 수정
  • Loading branch information
dunowljj committed Nov 13, 2023
1 parent 420b1a7 commit f502d8a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/ogjg/daitgym/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ogjg.daitgym.domain;

import com.ogjg.daitgym.domain.routine.Routine;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import lombok.Builder;
Expand All @@ -11,7 +12,7 @@
import java.time.LocalDate;

import static jakarta.persistence.EnumType.STRING;
import static jakarta.persistence.FetchType.*;
import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

@Getter
Expand Down Expand Up @@ -81,6 +82,10 @@ public String changeNickname(String newNickname) {
return this.nickname;
}

public void withdraw() {
this.isDeleted = true;
}

public void changeHealthClub(HealthClub newHealthClub) {
if (this.healthClub != null) {
this.healthClub.getUsers().remove(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public List<FollowListDto> followingList(String nickname) {
new QFollowListDto(
user.imageUrl,
user.nickname,
user.intro,
user.introduction,
inbody.score
))
.from(follow)
Expand All @@ -40,7 +40,7 @@ public List<FollowListDto> followerList(String nickname) {
new QFollowListDto(
user.imageUrl,
user.nickname,
user.intro,
user.introduction,
inbody.score
)
).from(follow)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/ogjg/daitgym/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public ApiResponse<EditInitialNicknameResponse> editInitialNickname(
);
}

/**
* 회원 탈퇴
*/
@PatchMapping("/withdraw")
public ApiResponse<?> withdraw(
@AuthenticationPrincipal OAuth2JwtUserDetails userDetails
) {
String loginEmail = userDetails.getEmail();

userService.updateUserDeleted(loginEmail);
return new ApiResponse<>(ErrorCode.SUCCESS.changeMessage("회원탈퇴 성공"));
}

/**
* 프로필 불러오기
*/
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/ogjg/daitgym/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public GetUserProfileGetResponse getUserProfile(String loginEmail, String nickna

return GetUserProfileGetResponse.builder()
.healthClubName(user.getHealthClub().getName())
.journalCount(exerciseJournalRepository.countByUserEmail(user.getEmail()))
// .journalCount(exerciseJournalRepository.countByUserEmail(user.getEmail()))
.followerCount(followRepository.countByFollowPKTargetEmail(user.getEmail()))
.followingCount(followRepository.countByFollowPKFollowerEmail(user.getEmail()))
.build();
Expand Down Expand Up @@ -173,7 +173,7 @@ public EditInitialNicknameResponse editInitialNickname(String loginEmail, EditNi
}

// 해당 메시지를 사용해야하는데 기존 에러코드를 수정할 수는 없어서 임시 사용
if (isUserNotFound(loginEmail)) {
if (isUserNotFoundByEmail(loginEmail)) {
throw new NotFoundUser("존재하지 않는 회원입니다.");
}

Expand All @@ -199,6 +199,20 @@ private boolean isNicknameAlreadyExist(String newNickname) {
return userRepository.findByNickname(newNickname).isPresent();
}

@Transactional(readOnly = true)
public void updateUserDeleted(String loginEmail) {
if (isUserNotFoundByEmail(loginEmail)) {
throw new NotFoundUser("존재하지 않는 회원입니다.");
}

User findUser = findUserByEmail(loginEmail);
findUser.withdraw();
}

private boolean isUserNotFoundByEmail(String loginEmail) {
return !userRepository.findByEmail(loginEmail).isPresent();
}

private User findUserByNickname(String nickname) {
return userRepository.findByNickname(nickname)
.orElseThrow(NotFoundUser::new);
Expand Down

0 comments on commit f502d8a

Please sign in to comment.