Skip to content

Commit

Permalink
feat: 회원정보 수정API 설계 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
BokDoong committed Oct 9, 2023
1 parent 11eb9d7 commit 964b930
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public enum ErrorCode {
EXCEED_KEYWORD_LIMIT(TaleCode.KEYWORD_COUNT_LIMIT.getCode(), BAD_REQUEST, "입력 키워드 개수 초과"),
DUPLICATED_KEYWORD(TaleCode.KEYWORD_DUPLICATED.getCode(), BAD_REQUEST, "중복된 키워드가 있는 경우"),
IMAGE_PROCESSING_IO(TaleCode.IMAGE_IO.getCode(), BAD_REQUEST, "이미지 처리 중 문제가 발생한 경우"),
IMAGE_NON_EXITED(TaleCode.NON_EXISTED_IMAGE.getCode(), NOT_ACCEPTABLE, "삭제할 이미지가 존재하지 않는 경우"),
TALE_IMAGE_NON_EXISTED(TaleCode.NON_EXISTED_IMAGE.getCode(), NOT_ACCEPTABLE, "삭제할 이미지가 존재하지 않는 경우"),

// Member
MEMBER_NOT_FOUND(MemberCode.NOT_FOUND.getCode(), NOT_FOUND, "존재하지 않는 회원"),
EXISTED_MEMBER(MemberCode.MEMBER_EXISTED.getCode(), CONFLICT, "이미 회원가입 되어있을 경우"),
DUPLICATED_NICKNAME(MemberCode.NICKNAME_DUPLICATED.getCode(), CONFLICT, "동일한 닉네임이 존재하는 경우"),
MEMBER_IMAGE_NON_EXISTED(MemberCode.NON_EXISTED_IMAGE.getCode(), CONFLICT, "삭제할 이미지가 존재하지 않는 경우")
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum MemberCode {
NOT_FOUND("M-001"),
MEMBER_EXISTED("M-002"),
NICKNAME_DUPLICATED("M-003"),
NON_EXISTED_IMAGE("M-004"),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import hanium.englishfairytale.common.files.FileManageService;
import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.NotFoundException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.application.dto.MemberCreateCommand;
import hanium.englishfairytale.member.application.dto.MemberImageUpdateCommand;
import hanium.englishfairytale.member.application.dto.MemberUpdatePasswordCommand;
import hanium.englishfairytale.member.domain.ImageRepository;
import hanium.englishfairytale.member.domain.Member;
import hanium.englishfairytale.member.domain.MemberImage;
import hanium.englishfairytale.member.domain.MemberRepository;
Expand All @@ -17,6 +21,7 @@
public class MemberCommandService {

private final MemberRepository memberRepository;
private final ImageRepository imageRepository;
private final FileManageService fileManageService;

@Transactional
Expand All @@ -25,6 +30,50 @@ public Long register(MemberCreateCommand memberCreateCommand) {
return createAndSaveMember(memberCreateCommand);
}

@Transactional
public void updateNickname(Long memberId, String nickname) {
Member member = findMember(memberId);
member.updateNickname(nickname);
}

@Transactional
public void updatePassword(MemberUpdatePasswordCommand updatePasswordCommand) {
Member member = findMember(updatePasswordCommand.getMemberId());
member.updatePassword(updatePasswordCommand.getPassword());
}

@Transactional
public void updateMemberImage(MemberImageUpdateCommand imageUpdateCommand) {
Member member = findMember(imageUpdateCommand.getMemberId());
member.updateMemberImage(createAndSaveMemberImage(imageUpdateCommand.getImage()));
}

@Transactional
public void deleteMemberImage(Long memberId) {
Member member = findMember(memberId);
Long imageId = findImageId(member);
deleteImage(member, imageId);
}

private void deleteImage(Member member, Long imageId) {
member.makeImageNull();
imageRepository.delete(imageId);
}

private Long findImageId(Member member) {
verifyImageIsEmpty(member);
return member.getImageId();
}

private void verifyImageIsEmpty(Member member) {
member.checkImageEmpty();
}

private Member findMember(Long id) {
return memberRepository.findMemberById(id)
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));
}

private Long createAndSaveMember(MemberCreateCommand memberCreateCommand) {
Member member = createMember(memberCreateCommand);
if (!checkImageEmpty(memberCreateCommand)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class MemberQueryService {
private final TaleQueryDao taleQueryDao;

@Transactional
public void verifyNickName(String nickName) {
verifyNicknameDuplicated(nickName);
public void verifyNickname(String nickname) {
verifyNicknameDuplicated(nickname);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hanium.englishfairytale.member.application.dto;

import lombok.Builder;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

@Builder
@Getter
public class MemberImageUpdateCommand {
private Long memberId;
private MultipartFile image;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hanium.englishfairytale.member.application.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MemberUpdatePasswordCommand {
Long memberId;
String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hanium.englishfairytale.member.domain;

public interface ImageRepository {

void delete(Long memberImageId);
}
32 changes: 32 additions & 0 deletions src/main/java/hanium/englishfairytale/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hanium.englishfairytale.member.domain;

import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.tale.domain.Tale;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -55,4 +57,34 @@ public void addTale(Tale newTale) {
public void putImage(MemberImage memberImage) {
image.putMemberImage(memberImage);
}

public void updateNickname(String nickname) {
this.nickname = nickname;
}
public void updatePassword(String password) {
this.password = password;
}

public void updateMemberImage(MemberImage memberImage) {
if (image == null) {
this.image = new Image();
this.image.putMemberImage(memberImage);
} else {
image.putMemberImage(memberImage);
}
}

public void checkImageEmpty() {
if (image == null) {
throw new BusinessException(ErrorCode.MEMBER_IMAGE_NON_EXISTED);
}
}

public Long getImageId() {
return this.image.getMemberImage().getId();
}

public void makeImageNull() {
this.image = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hanium.englishfairytale.member.infra;

import hanium.englishfairytale.member.domain.ImageRepository;
import hanium.englishfairytale.member.domain.MemberImage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;

@Repository
@RequiredArgsConstructor
public class MemberImageJpaRepository implements ImageRepository {

private final EntityManager em;

@Override
public void delete(Long memberImageId) {
MemberImage memberImage = em.find(MemberImage.class, memberImageId);
em.remove(memberImage);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package hanium.englishfairytale.member.infra.http;

import com.amazonaws.Response;
import hanium.englishfairytale.member.application.MemberCommandService;
import hanium.englishfairytale.member.application.MemberQueryService;
import hanium.englishfairytale.member.application.dto.MemberCreateCommand;
import hanium.englishfairytale.member.application.dto.MemberDetailInfo;
import hanium.englishfairytale.member.application.dto.MemberInfo;
import hanium.englishfairytale.member.application.dto.*;
import hanium.englishfairytale.member.infra.http.dto.MemberCreateDto;
import hanium.englishfairytale.member.infra.http.dto.MemberUpdatePasswordDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -30,8 +28,8 @@ public ResponseEntity<Long> register(@Validated @RequestPart MemberCreateDto mem
}

@PostMapping("/check")
public void checkNicknameDuplicated(@RequestParam String nickName) {
memberQueryService.verifyNickName(nickName);
public void checkNicknameDuplicated(@RequestParam String nickname) {
memberQueryService.verifyNickname(nickname);
}

@GetMapping("{memberId}")
Expand All @@ -44,7 +42,35 @@ public ResponseEntity<MemberDetailInfo> findMemberDetailInfo(@PathVariable Long
return new ResponseEntity<>(memberQueryService.findMemberDetailInfo(memberId), HttpStatus.OK);
}

@PatchMapping("/nickname")
public void updateNickname(@RequestParam Long memberId, @RequestParam String nickname) {
memberCommandService.updateNickname(memberId, nickname);
}

@PatchMapping("/password")
public void updatePassword(@Validated @RequestBody MemberUpdatePasswordDto updatePasswordDto) {
memberCommandService.updatePassword(toPasswordUpdateCommand(updatePasswordDto));
}

@PatchMapping("/{memberId}/image")
public void updateImage(@PathVariable Long memberId, @RequestPart MultipartFile image) {
memberCommandService.updateMemberImage(toImageUpdateCommand(memberId, image));
}

@DeleteMapping("/{memberId}/image")
public void deleteImage(@PathVariable Long memberId) {
memberCommandService.deleteMemberImage(memberId);
}

private MemberCreateCommand toCreateCommand(MemberCreateDto memberCreateDto, MultipartFile image) {
return converter.toCommand(memberCreateDto, image);
}

private MemberUpdatePasswordCommand toPasswordUpdateCommand(MemberUpdatePasswordDto updatePasswordDto) {
return converter.toCommand(updatePasswordDto);
}

private MemberImageUpdateCommand toImageUpdateCommand(Long memberId, MultipartFile image) {
return converter.toCommand(memberId, image);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package hanium.englishfairytale.member.infra.http;

import hanium.englishfairytale.member.application.dto.MemberCreateCommand;
import hanium.englishfairytale.member.application.dto.MemberImageUpdateCommand;
import hanium.englishfairytale.member.application.dto.MemberUpdatePasswordCommand;
import hanium.englishfairytale.member.infra.http.dto.MemberCreateDto;
import hanium.englishfairytale.member.infra.http.dto.MemberUpdatePasswordDto;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -17,4 +20,18 @@ public MemberCreateCommand toCommand(MemberCreateDto dto, MultipartFile image) {
.image(image)
.build();
}

public MemberUpdatePasswordCommand toCommand(MemberUpdatePasswordDto dto) {
return MemberUpdatePasswordCommand.builder()
.memberId(dto.getMemberId())
.password(dto.getPassword())
.build();
}

public MemberImageUpdateCommand toCommand(Long id, MultipartFile image) {
return MemberImageUpdateCommand.builder()
.memberId(id)
.image(image)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hanium.englishfairytale.member.infra.http.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MemberUpdatePasswordDto {
@NotNull
private Long memberId;
@NotNull
@Pattern(regexp = "^(?i)(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{2,10}$")
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public TaleCreateResponse create(TaleCreateCommand taleCreateCommand) {

@Transactional
public void deleteTale(Long taleId) {
findExistedTale(taleId);
findTale(taleId);
taleRepository.deleteByTaleId(taleId);
}

@Transactional
public void updateTaleImage(TaleUpdateCommand taleUpdateCommand) {
Tale tale = findExistedTale(taleUpdateCommand.getTaleId());
Tale tale = findTale(taleUpdateCommand.getTaleId());
tale.updateTaleImage(createAndSaveTaleImage(taleUpdateCommand.getImage()));
}

@Transactional
public void deleteTaleImage(Long taleId) {
Tale tale = findExistedTale(taleId);
Tale tale = findTale(taleId);
Long imageId = findImageId(tale);
deleteImage(tale, imageId);
}
Expand All @@ -69,11 +69,10 @@ private Long findImageId(Tale tale) {
}

private void verifyImageIsEmpty(Tale tale) {
if (tale.checkImageEmpty())
throw new BusinessException(ErrorCode.IMAGE_NON_EXITED);
tale.checkImageEmpty();
}

private Tale findExistedTale(Long taleId) {
private Tale findTale(Long taleId) {
return taleQueryDao.findTaleByTaleId(taleId)
.orElseThrow(() -> new NotFoundException(ErrorCode.TALE_NOT_FOUND));
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/hanium/englishfairytale/tale/domain/Tale.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hanium.englishfairytale.tale.domain;

import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.domain.Member;
import lombok.*;

Expand Down Expand Up @@ -65,8 +67,10 @@ public void updateTaleImage(TaleImage taleImage) {
}
}

public boolean checkImageEmpty() {
return image == null;
public void checkImageEmpty() {
if (image == null) {
throw new BusinessException(ErrorCode.TALE_IMAGE_NON_EXISTED);
}
}

public Long getImageId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hanium.englishfairytale.tale.infra;

import hanium.englishfairytale.tale.domain.ImageRepository;
import hanium.englishfairytale.tale.domain.Tale;
import hanium.englishfairytale.tale.domain.TaleImage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
Expand All @@ -10,7 +9,7 @@

@Repository
@RequiredArgsConstructor
public class ImageJpaRepository implements ImageRepository {
public class TaleImageJpaRepository implements ImageRepository {

private final EntityManager em;

Expand Down
Loading

0 comments on commit 964b930

Please sign in to comment.