Skip to content

Commit

Permalink
Merge pull request #107 from oduck-team/feature/104
Browse files Browse the repository at this point in the history
회원 탈퇴 구현 #104
  • Loading branch information
FaberJoo authored Nov 15, 2023
2 parents 81464d7 + f988d9f commit e63befe
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 98 deletions.
20 changes: 16 additions & 4 deletions src/main/java/io/oduck/api/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class Member extends BaseEntity {
private List<AttractionPoint> attractionPoints;

@Builder
public Member(Long id, Role role, LoginType loginType, AuthSocial authSocial, AuthLocal authLocal, MemberProfile memberProfile) {
public Member(Long id, Role role, LoginType loginType, AuthSocial authSocial, AuthLocal authLocal,
MemberProfile memberProfile) {
this.id = id;
this.role = role;
this.loginType = loginType;
Expand All @@ -79,26 +80,37 @@ public Member(Long id, Role role, LoginType loginType, AuthSocial authSocial, A
// TODO: set 말고 다른 이름으로 변경하기
public void relateAuthSocial(AuthSocial authSocial) {
this.authSocial = authSocial;
if(authSocial != null) {
if (authSocial != null) {
authSocial.relateMember(this);
}
}

public void relateAuthLocal(AuthLocal authLocal) {
this.authLocal = authLocal;
if(authLocal != null) {
if (authLocal != null) {
authLocal.relateMember(this);
}
}

public void relateMemberProfile(MemberProfile memberProfile) {
this.memberProfile = memberProfile;
if(memberProfile != null) {
if (memberProfile != null) {
memberProfile.relateMember(this);
}
}

public void delete() {
this.deletedAt = LocalDateTime.now();
this.memberProfile.delete();

if (this.authLocal != null) {
this.authLocal.delete();
}

if (this.authSocial != null) {
this.authSocial.delete();
}

this.role = Role.WITHDRAWAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import io.oduck.api.global.audit.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -71,4 +70,8 @@ public void updateName(String name) {
public void updateInfo(String info) {
this.info = info;
}

public void delete() {
this.deletedAt = LocalDateTime.now();
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/oduck/api/domain/member/entity/Role.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.oduck.api.domain.member.entity;

public enum Role {
ADMIN, MEMBER, GUEST
ADMIN, MEMBER, GUEST, WITHDRAWAL
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.oduck.api.domain.member.repository;

import io.oduck.api.domain.member.entity.Member;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberRepository extends JpaRepository<Member,Long>, MemberRepositoryCustom{

Optional<Member> findByIdAndDeletedAtIsNull(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Optional<ProfileWithoutActivity> selectProfileByName(String name) {
)
.from(memberProfile)
.where(memberProfile.name.eq(name))
.where(memberProfile.deletedAt.isNull())
.fetchOne());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService{
public class MemberServiceImpl implements MemberService {
private final PasswordEncoder passwordEncoder;
private final AuthLocalRepository authLocalRepository;
private final MemberRepository memberRepository;
Expand All @@ -46,18 +46,12 @@ public void signUpByLocal(CreateReq createReq) {
try {
String encryptedPassword = passwordEncoder.encode(createReq.getPassword());

AuthLocal authLocal = AuthLocal.builder()
.email(createReq.getEmail())
.password(encryptedPassword)
.build();
AuthLocal authLocal = AuthLocal.builder().email(createReq.getEmail())
.password(encryptedPassword).build();

MemberProfile memberProfile = MemberProfile.builder()
.name(generateNickname())
.build();
MemberProfile memberProfile = MemberProfile.builder().name(generateNickname()).build();

Member member = Member.builder()
.loginType(LoginType.LOCAL)
.build();
Member member = Member.builder().loginType(LoginType.LOCAL).build();

member.relateAuthLocal(authLocal);
member.relateMemberProfile(memberProfile);
Expand All @@ -77,26 +71,19 @@ public MemberProfileRes getProfileByName(String name, Long memberId) {
throw new NotFoundException("Member");
}
Long reviewsCount = memberRepository.countReviewsByMemberId(memberProfile.getMemberId());
Long bookmarksCount = memberRepository.countBookmarksByMemberId(memberProfile.getMemberId());
Long bookmarksCount =
memberRepository.countBookmarksByMemberId(memberProfile.getMemberId());
Long likesCount = memberRepository.countLikesByMemberId(memberProfile.getMemberId());


Activity activity = Activity.builder()
.reviews(reviewsCount)
.bookmarks(bookmarksCount)
.likes(likesCount)
.point(memberProfile.getPoint())
.build();
Activity activity = Activity.builder().reviews(reviewsCount).bookmarks(bookmarksCount)
.likes(likesCount).point(memberProfile.getPoint()).build();

MemberProfileRes memberProfileRes = MemberProfileRes. builder()
.isMine(memberProfile.getMemberId().equals(memberId))
.memberId(memberProfile.getMemberId())
.name (memberProfile.getName())
.description(memberProfile.getDescription())
.thumbnail(memberProfile.getThumbnail())
.backgroundImage (memberProfile.getBackgroundImage())
.activity(activity)
.build();
MemberProfileRes memberProfileRes = MemberProfileRes.builder()
.isMine(memberProfile.getMemberId().equals(memberId))
.memberId(memberProfile.getMemberId()).name(memberProfile.getName())
.description(memberProfile.getDescription()).thumbnail(memberProfile.getThumbnail())
.backgroundImage(memberProfile.getBackgroundImage()).activity(activity).build();

return memberProfileRes;
}
Expand All @@ -116,41 +103,32 @@ public void updateProfile(PatchReq body, Long memberId) {
}

// Null 체크
Optional
.ofNullable(body.getDescription())
.ifPresent(
memberProfile::updateInfo
);

Optional.ofNullable(body.getDescription()).ifPresent(memberProfile::updateInfo);

memberProfileRepository.save(memberProfile);
}

@Override
@Transactional
public void withdrawMember(Long memberId) {
Member member = getMemberById(memberId);
member.delete();
memberRepository.save(member);
}

private Member getMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(
() -> new NotFoundException("Member")
);
return memberRepository.findByIdAndDeletedAtIsNull(memberId)
.orElseThrow(() -> new NotFoundException("Member"));
}

private MemberProfile getProfileByMemberId(Long memberId) {
return memberProfileRepository.findByMemberId(memberId)
.orElseThrow(
() -> new NotFoundException("Member")
);
.orElseThrow(() -> new NotFoundException("Member"));
}

private ProfileWithoutActivity getProfileWithoutActivity(String name) {
return memberRepository.selectProfileByName(name)
.orElseThrow(
() -> new NotFoundException("Member")
);
.orElseThrow(() -> new NotFoundException("Member"));
}

private void checkDuplicatedName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import io.oduck.api.global.audit.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -43,4 +43,6 @@ public AuthLocal(Member member, String email, String password) {
public void relateMember(Member member) {
this.member = member;
}

public void delete() { this.deletedAt = LocalDateTime.now(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -50,4 +50,6 @@ public AuthSocial(Long id, Member member, String email, String socialId, SocialT
public void relateMember(Member member) {
this.member = member;
}

public void delete() { this.deletedAt = LocalDateTime.now(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Status getStatus(AuthUser user) {
.point(memberProfile.getPoint())
.build();

if (user.getRole().equals("ADMIN")) {
if (user.getRole().equals("ADMIN") || user.getRole().equals("WITHDRAWAL")) {
status = AdminStatus.builder()
.status(status)
.role(user.getRole())
Expand Down Expand Up @@ -74,7 +74,7 @@ public void login(LocalAuthDto localAuthDto) {
}

private String extractPasswordIfAdmin(Role role, String password) {
if (role.equals(Role.MEMBER)) return password;
if (!role.equals(Role.ADMIN)) return password;

LocalTime now = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
Expand Down
Loading

0 comments on commit e63befe

Please sign in to comment.