Skip to content

Commit

Permalink
refactor(member): change api request&response data (#89)
Browse files Browse the repository at this point in the history
* refactor(error): change class name TeamAlreadyExistsException -> TeamExistsException

* feat(member): add Member Delete Method & team field & setting team

* feat(team): add setting teamLeader method

* feat(member): add member delete

* test(member): test member delete method

* refactor(member): change member api classification

* feat(code): add initialize code sql on local envirionment (#87)

* feat(codeFixture): add getter methods

* feat(sql): add code insert sql to submodule

* feat(code): add find code by code_value query & test

* refactor(repository): change code setUp method in AbstractRepositoryUnitTest

---------

Co-authored-by: KAispread <[email protected]>

* feat(valid): add bean validator for create Member RequestDto

* test(valid): test bean validator

* feat(error): add error messages for validator

* refactor(member): refactor CreateMemberRequestDto

* feat(code): add query findByCodePk and CodePk static factory method

* test(code): test query

* refactor(member): create member api refactoring

* test(member): test create member service method

* test(member): test create member api

* test(member): read member detail api test refactoring

* refactor(member): change MemberDetailResponseDto specification

* refactor(fixture): change MemberDetailResponseDto specification

* refactor(member): modify memberService specification

* feat(member): add find member with fetch join & test

* refactor(member): modify member spec

* refactor(member): change member api controller & test

* refactor(member): refactor read member service code

* test(member): test read member

* fix(member): fix compile error

* refactor(team): modify data type from Integer to DrinkRate Enum

* refactor(member): add Member update method and change nickname length

* feat(member): add MBTI exception

* refactor(team): remove unnecessary field on team constructor

* test(member): test member nickname length validation

* test(member): test update member

* feat(member): add update member (nickname, mbti)

* refactor(member): remove unused dto

* style(member): change number controller parameter

* refactor(member): change nickname length validation 3 -> 2

* feat(member): add nickname format validation

* refactor(validate): change CustomFormatValidator static REGX instance

* feat(member): add update value validation logic

* feat(member): add bean valid annotation that validate nickname format

---------

Co-authored-by: KAispread <[email protected]>
Co-authored-by: chaerim <[email protected]>
  • Loading branch information
3 people authored Aug 8, 2023
1 parent 3b1578e commit 4eab177
Show file tree
Hide file tree
Showing 54 changed files with 1,516 additions and 580 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.e2i.wemeet.config.resolver.member.MemberId;
import com.e2i.wemeet.dto.request.member.CreateMemberRequestDto;
import com.e2i.wemeet.dto.request.member.ModifyMemberRequestDto;
import com.e2i.wemeet.dto.request.member.UpdateMemberRequestDto;
import com.e2i.wemeet.dto.response.ResponseDto;
import com.e2i.wemeet.dto.response.member.MemberDetailResponseDto;
import com.e2i.wemeet.dto.response.member.MemberInfoResponseDto;
import com.e2i.wemeet.dto.response.member.RoleResponseDto;
import com.e2i.wemeet.dto.response.member.MemberRoleResponseDto;
import com.e2i.wemeet.security.model.MemberPrincipal;
import com.e2i.wemeet.service.member.MemberService;
import com.e2i.wemeet.service.member_image.MemberImageService;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
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.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
Expand All @@ -40,52 +40,39 @@ public ResponseDto<Void> createMember(@RequestBody @Valid CreateMemberRequestDto
}

@GetMapping
public ResponseDto<MemberDetailResponseDto> getMemberDetail(
@AuthenticationPrincipal MemberPrincipal memberPrincipal) {
Long memberId = memberPrincipal.getMemberId();
MemberDetailResponseDto result = memberService.getMemberDetail(memberId);
public ResponseDto<MemberDetailResponseDto> getMemberDetail(@MemberId Long memberId) {
MemberDetailResponseDto result = memberService.readMemberDetail(memberId);

return ResponseDto.success("Get Member-detail Success", result);
}

@GetMapping("/info")
public ResponseDto<MemberInfoResponseDto> getMemberInfo(
@AuthenticationPrincipal MemberPrincipal memberPrincipal) {
Long memberId = memberPrincipal.getMemberId();
MemberInfoResponseDto result = memberService.getMemberInfo(memberId);
@PatchMapping
public ResponseDto<Void> update(@MemberId Long memberId,
@Valid @RequestBody UpdateMemberRequestDto requestDto) {
memberService.updateMember(memberId, requestDto);

return ResponseDto.success("Get Member-Info Success", result);
}

@PutMapping
public ResponseDto<Void> modifyMember(@AuthenticationPrincipal MemberPrincipal memberPrincipal,
@RequestBody @Valid ModifyMemberRequestDto requestDto) {
Long memberId = memberPrincipal.getMemberId();
memberService.modifyMember(memberId, requestDto);

return ResponseDto.success("Modify Member Success");
return ResponseDto.success("Update Member Success");
}

@GetMapping("/role")
public ResponseDto<RoleResponseDto> getMemberRole(
@AuthenticationPrincipal MemberPrincipal memberPrincipal) {
RoleResponseDto result = memberService.getMemberRole(memberPrincipal.getMemberId());
public ResponseDto<MemberRoleResponseDto> getMemberRole(@AuthenticationPrincipal MemberPrincipal memberPrincipal) {
MemberRoleResponseDto result = memberService.readMemberRole(memberPrincipal);

return ResponseDto.success("Get Member Role Success", result);
}

@DeleteMapping
public ResponseDto<Void> deleteMember(@MemberId Long memberId) {
memberService.deleteMember(memberId);

return ResponseDto.success("Delete Member Success");
}

@PostMapping("/profile-image")
public ResponseDto<Void> uploadProfileImage(@MemberId Long memberId,
@RequestPart("file") MultipartFile file) {
memberImageService.uploadProfileImage(memberId, file);

return ResponseDto.success("Upload Profile Image Success");
}

@DeleteMapping
public ResponseDto<Void> delete(@MemberId Long memberId) {
memberService.deleteMember(memberId, LocalDateTime.now());

return ResponseDto.success("Delete Member Success");
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/e2i/wemeet/domain/code/CodePk.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public CodePk(String codeId, String groupCodeId) {
this.codeId = codeId;
this.groupCodeId = groupCodeId;
}

// CE-001 -> CODE PK 생성
public static CodePk of(final String groupCodeIdWithCodeId) {
final String[] split = groupCodeIdWithCodeId.split("-");
return new CodePk(split[1], split[0]);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/e2i/wemeet/domain/code/CodeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface CodeRepository extends JpaRepository<Code, CodePk> {

Optional<Code> findByCodeValue(String codeValue);

Optional<Code> findByCodePk(CodePk codePk);

}
53 changes: 41 additions & 12 deletions src/main/java/com/e2i/wemeet/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import com.e2i.wemeet.domain.member.data.Mbti;
import com.e2i.wemeet.domain.member.data.ProfileImage;
import com.e2i.wemeet.domain.member.data.Role;
import com.e2i.wemeet.domain.team.Team;
import com.e2i.wemeet.dto.request.member.UpdateMemberRequestDto;
import com.e2i.wemeet.exception.badrequest.MemberHasBeenDeletedException;
import com.e2i.wemeet.exception.badrequest.TeamExistsException;
import com.e2i.wemeet.exception.unauthorized.CreditNotEnoughException;
import com.e2i.wemeet.exception.unauthorized.UnAuthorizedRoleException;
import com.e2i.wemeet.util.validator.CustomFormatValidator;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Embedded;
Expand All @@ -21,12 +26,17 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -72,13 +82,16 @@ public class Member extends BaseTimeEntity {
@Column(nullable = false)
private Role role;

@OneToMany(mappedBy = "teamLeader", cascade = CascadeType.PERSIST)
private List<Team> team = new ArrayList<>();

@Column(name = "DELETED_AT")
private LocalDateTime deletedAt;

@Builder
public Member(String nickname, Gender gender, String phoneNumber, String email,
CollegeInfo collegeInfo, Mbti mbti, Integer credit, Boolean imageAuth,
ProfileImage profileImage, Role role, LocalDateTime deletedAt) {
ProfileImage profileImage, Role role) {
this.nickname = nickname;
this.gender = gender;
this.phoneNumber = phoneNumber;
Expand All @@ -89,7 +102,6 @@ public Member(String nickname, Gender gender, String phoneNumber, String email,
this.imageAuth = imageAuth;
this.profileImage = profileImage;
this.role = role;
this.deletedAt = deletedAt;
}

public void addCredit(int amount) {
Expand All @@ -107,17 +119,8 @@ public void setRole(final Role role) {
this.role = role;
}

public void modifyNickname(String nickname) {
this.nickname = nickname;
}

public void modifyMbti(Mbti mbti) {
this.mbti = mbti;
}

// TODO :: refactoring
public boolean isEmailAuthenticated() {
return false;
return StringUtils.hasText(this.email);
}

public Member checkMemberValid() {
Expand All @@ -137,6 +140,32 @@ public void saveEmail(final String email) {
this.email = email;
}

public void delete(LocalDateTime deletedAt) {
// Team 이 존재한다면 예외 발생
List<LocalDateTime> notDeleted = this.team.stream()
.map(Team::getDeletedAt)
.filter(Objects::isNull)
.toList();
if (!notDeleted.isEmpty()) {
throw new TeamExistsException();
}

this.deletedAt = deletedAt;
}

public void setTeam(Team team) {
if (!this.team.contains(team)) {
this.team.add(team);
}
}

public void update(UpdateMemberRequestDto requestDto) {
CustomFormatValidator.validateNicknameFormat(requestDto.nickname());

this.nickname = requestDto.nickname();
this.mbti = Mbti.findBy(requestDto.mbti());
}

public void saveProfileImage(final ProfileImage profileImage) {
this.profileImage = profileImage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.e2i.wemeet.domain.member.persist.PersistLoginRepository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface MemberRepository extends JpaRepository<Member, Long>, PersistLoginRepository {

Optional<Member> findByPhoneNumber(String phoneNumber);

Optional<Member> findByEmail(String email);

@Query("select m from Member m join fetch m.collegeInfo.collegeCode where m.memberId = :memberId")
Optional<Member> findByIdFetchCode(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Embeddable;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
Expand All @@ -17,7 +18,7 @@
@Embeddable
public class CollegeInfo {

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "collegeCodeId", referencedColumnName = "code_id")
@JoinColumn(name = "collegeGroupCodeId", referencedColumnName = "group_code_id")
private Code collegeCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public enum Gender {

@JsonCreator
public static Gender findBy(String value) {

return Arrays.stream(Gender.values())
.filter(gender -> gender.name().equals(value.toUpperCase()))
.findFirst()
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/e2i/wemeet/domain/member/data/Mbti.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.e2i.wemeet.domain.member.data;

import com.e2i.wemeet.exception.ErrorCode;
import com.e2i.wemeet.exception.badrequest.InvalidDatabaseKeyToEnumException;
import com.e2i.wemeet.exception.badrequest.InvalidValueException;
import com.e2i.wemeet.exception.badrequest.InvalidMbtiException;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import lombok.Getter;
Expand All @@ -22,11 +21,10 @@ public enum Mbti {

@JsonCreator
public static Mbti findBy(String value) {

return Arrays.stream(Mbti.values())
.filter(mbti -> mbti.name().equals(value.toUpperCase()))
.findFirst()
.orElseThrow(() -> new InvalidValueException(ErrorCode.INVALID_MBTI_VALUE));
.orElseThrow(InvalidMbtiException::new);
}

public static Mbti findByKey(int key) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/e2i/wemeet/domain/team/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,26 @@ public class Team extends BaseTimeEntity {
private LocalDateTime deletedAt;

@Builder
public Team(Integer memberNum, Gender gender, Region region, DrinkRate drinkRate,
DrinkWithGame drinkWithGame,
AdditionalActivity additionalActivity,
String introduction, Member teamLeader, LocalDateTime deletedAt) {
public Team(Integer memberNum, Region region, DrinkRate drinkRate,
DrinkWithGame drinkWithGame, AdditionalActivity additionalActivity,
String introduction, Member teamLeader) {
setTeamLeader(teamLeader);
this.memberNum = memberNum;
this.gender = gender;
this.region = region;
this.drinkRate = drinkRate;
this.drinkWithGame = drinkWithGame;
this.additionalActivity = additionalActivity;
this.introduction = introduction;
}

/*
* 팀장 설정은 생성자에서만 가능
* 연관 관계 설정은 Team 에서 진행
*/
private void setTeamLeader(final Member teamLeader) {
this.teamLeader = teamLeader;
this.deletedAt = deletedAt;
this.gender = teamLeader.getGender();
teamLeader.setTeam(this);
}

public Team checkTeamValid() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.e2i.wemeet.dto.request.member;

import jakarta.validation.constraints.NotBlank;
import com.e2i.wemeet.domain.code.Code;
import com.e2i.wemeet.domain.member.data.CollegeInfo;
import com.e2i.wemeet.domain.member.data.CollegeType;
import com.e2i.wemeet.util.validator.bean.CollegeCodeValid;
import com.e2i.wemeet.util.validator.bean.CollegeTypeValid;
import lombok.Builder;
import org.hibernate.validator.constraints.Length;

@Builder
public record CollegeInfoRequestDto(
@NotBlank(message = "{not.blank.college}")
String college,
@NotBlank(message = "{not.blank.college.type}")
@CollegeCodeValid
String collegeCode,

@CollegeTypeValid
String collegeType,
@NotBlank(message = "{not.blank.admission.year}")

@Length(message = "{length.admissionYear}", min = 2, max = 2)
String admissionYear
) {

public CollegeInfo toCollegeInfo(Code collegeCode) {
return CollegeInfo.builder()
.collegeCode(collegeCode)
.collegeType(CollegeType.valueOf(this.collegeType))
.admissionYear(admissionYear)
.build();
}
}
Loading

0 comments on commit 4eab177

Please sign in to comment.