Skip to content

Commit

Permalink
Merge pull request #184 from Katchup-dev/refactor/#179-profile
Browse files Browse the repository at this point in the history
[REFACTOR] 프로필 정보 설정 API 리팩토링
  • Loading branch information
unanchoi authored Oct 29, 2023
2 parents d81527d + 82f133c commit 3739c15
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ApiResponseDto<MemberProfileGetResponseDto> getMemberProfile(Principal pr
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@PutMapping("/profile/me")
public ApiResponseDto updateMemberProfile(@RequestPart("profileImage") MultipartFile profileImage, @Valid MemberProfileUpdateRequestDto request, Principal principal) {
public ApiResponseDto updateMemberProfile(@RequestPart("profileImage") @Nullable MultipartFile profileImage, @Valid MemberProfileUpdateRequestDto request, Principal principal) {
memberService.updateMemberProfile(MemberUtil.getMemberId(principal), profileImage, request);
return ApiResponseDto.success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import site.katchup.katchupserver.common.domain.BaseEntity;

import java.nio.ByteBuffer;
import java.util.Objects;

import static jakarta.persistence.GenerationType.*;
import static lombok.AccessLevel.*;
Expand Down Expand Up @@ -53,8 +54,20 @@ public void updateMemberStatus(boolean isNewUser, boolean isDeleted, String refr
this.refreshToken = refreshToken;
}

public void updateMemberProfile(MemberProfile memberProfile) {
this.memberProfile = memberProfile;
public void updateIntroduction(String introduction) {
if (introduction != null) {
this.memberProfile.updateIntroduction(introduction);
}
}

public void updateNickname(String nickname) {
if (nickname != null) {
this.memberProfile.updateNickname(nickname);
}
}

public void updateImageUrl(String imageUrl) {
this.memberProfile.updateImageUrl(imageUrl);
}

public void clearRefreshToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public MemberProfile(String nickname, String introduction, String imageUrl) {
this.introduction = introduction;
this.imageUrl = imageUrl;
}

protected void updateNickname(String nickname) {
this.nickname = nickname;
}

protected void updateIntroduction(String introduction) {
this.introduction = introduction;
}

protected void updateImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand All @@ -11,9 +12,9 @@
@Schema(description = "회원 프로필 수정 요청 DTO")
public class MemberProfileUpdateRequestDto {
@Schema(description = "회원 닉네임", example = "unan")
@NotNull
@Min(value = 2, message = "MP-117") @Max(value = 20, message = "MP-102")
private String nickname;
@Schema(description = "회원 한줄 소개", example = "안녕하세요")
@NotNull
@Max(value = 100, message = "MP-118")
private String introduction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ public class MemberProfileGetResponseDto {
private String imageUrl;
@Schema(description = "회원 닉네임", example = "unan")
private String nickname;
@Schema(description = "회원 소개", example = "안녕하세요")
private String introduction;
@Schema(description = "회원 이메일", example = "[email protected]")
private String email;

@Builder
public MemberProfileGetResponseDto(String imageUrl, String nickname, String email) {
public MemberProfileGetResponseDto(String imageUrl, String nickname, String introduction, String email) {
this.imageUrl = imageUrl;
this.nickname = nickname;
this.introduction = introduction;
this.email = email;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,33 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import site.katchup.katchupserver.api.member.domain.Member;
import site.katchup.katchupserver.api.member.domain.MemberProfile;
import site.katchup.katchupserver.api.member.dto.request.MemberProfileUpdateRequestDto;
import site.katchup.katchupserver.api.member.dto.response.MemberProfileGetResponseDto;
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.api.member.service.MemberService;
import site.katchup.katchupserver.common.exception.BadRequestException;
import site.katchup.katchupserver.common.exception.InternalServerException;
import site.katchup.katchupserver.common.response.ErrorCode;
import site.katchup.katchupserver.external.s3.S3Service;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberServiceImpl implements MemberService {
private static final String PROFILE_IMAGE_PREFIX = "profiles";
private static final String PROFILE_IMAGE_FOLDER_NAME = "profiles";
private static final List<String> VALID_IMAGE_CONTENT_TYPE = List.of("image/jpeg", "image/jpg", "image/png", "image/gif", "image/tiff", "image/tif");
private static final long MAX_PROFILE_IMAGE_SIZE = 5 * 1024 * 1024;

private final MemberRepository memberRepository;
private final S3Service s3Service;

@Override
public MemberProfileGetResponseDto getMemberProfile(Long memberId) {
Member member = memberRepository.findByIdOrThrow(memberId);

return MemberProfileGetResponseDto.of(member);
}

Expand All @@ -37,16 +40,28 @@ public MemberProfileGetResponseDto getMemberProfile(Long memberId) {
public void updateMemberProfile(Long memberId, MultipartFile profileImage, MemberProfileUpdateRequestDto request) {
Member member = memberRepository.findByIdOrThrow(memberId);
try {
String key = s3Service.uploadImage( s3Service.makeUploadPrefix(member.getUserUUID(), PROFILE_IMAGE_PREFIX) , profileImage);
member.updateMemberProfile(MemberProfile.builder()
.nickname(request.getNickname())
.introduction(request.getIntroduction())
.imageUrl(s3Service.findUrlByName(key))
.build());
if (Objects.nonNull(profileImage)) {
validateExtension(profileImage);
validateFileSize(profileImage);
String key = s3Service.uploadImage(s3Service.makeUploadPrefix(member.getUserUUID(), PROFILE_IMAGE_FOLDER_NAME), profileImage);
member.updateImageUrl(s3Service.findUrlByName(key));
}
member.updateIntroduction(request.getIntroduction());
member.updateNickname(request.getNickname());
} catch (IOException e) {
throw new InternalServerException(ErrorCode.INTERNAL_SERVER_ERROR);
}
}

private void validateFileSize(MultipartFile profileImage) {
if (profileImage.getSize() > MAX_PROFILE_IMAGE_SIZE) {
throw new BadRequestException(ErrorCode.INVALID_PROFILE_IMAGE_SIZE);
}
}

}
private void validateExtension(MultipartFile profileImage) {
if (!VALID_IMAGE_CONTENT_TYPE.contains(profileImage.getContentType())) {
throw new BadRequestException(ErrorCode.INVALID_PROFILE_IMAGE_TYPE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public enum ErrorCode {
DUPLICATE_TASK_NAME("TK-105"),
NOT_PDF_FILE_TYPE("CD-106"),
FILE_SIZE_EXCEED("CD-107"),
INVALID_PROFILE_IMAGE_TYPE("MP-115"),
INVALID_PROFILE_IMAGE_SIZE("MP-116"),
INVALID_MEMBER_NICKNAME("MP-117"),
INVALID_MEMBER_INTRODUCTION("MP-118"),

/**
* 401 UNAUTHORIZED
Expand Down

0 comments on commit 3739c15

Please sign in to comment.