From 46be3f72a9c376c61bb72c181170346c4e18679b Mon Sep 17 00:00:00 2001 From: BK Date: Tue, 24 Oct 2023 20:26:53 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20image=20=EA=B4=80=EB=A0=A8=20=ED=9A=8C?= =?UTF-8?q?=EC=9B=90=EC=A0=95=EB=B3=B4=20=EC=88=98=EC=A0=95,=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BE/controller/UserController.java | 19 +++++++--- src/main/java/igoMoney/BE/domain/User.java | 13 ++++--- .../BE/dto/request/NicknameChangeRequest.java | 20 ++++++++++ ...st.java => ProfileImageChangeRequest.java} | 9 ++--- .../BE/dto/request/RecordSaveRequest.java | 1 - .../java/igoMoney/BE/service/UserService.java | 37 +++++++++---------- 6 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 src/main/java/igoMoney/BE/dto/request/NicknameChangeRequest.java rename src/main/java/igoMoney/BE/dto/request/{UserUpdateRequest.java => ProfileImageChangeRequest.java} (55%) diff --git a/src/main/java/igoMoney/BE/controller/UserController.java b/src/main/java/igoMoney/BE/controller/UserController.java index 49799c1..64aeaac 100644 --- a/src/main/java/igoMoney/BE/controller/UserController.java +++ b/src/main/java/igoMoney/BE/controller/UserController.java @@ -1,6 +1,7 @@ package igoMoney.BE.controller; -import igoMoney.BE.dto.request.UserUpdateRequest; +import igoMoney.BE.dto.request.NicknameChangeRequest; +import igoMoney.BE.dto.request.ProfileImageChangeRequest; import igoMoney.BE.dto.response.NotificationResponse; import igoMoney.BE.dto.response.UserResponse; import igoMoney.BE.service.UserService; @@ -41,15 +42,23 @@ public ResponseEntity checkNicknameDuplicate(@PathVariable("nickname") Str return new ResponseEntity(HttpStatus.OK); } - // 회원정보 수정 - @PatchMapping("") - public ResponseEntity updateUser(@Valid UserUpdateRequest request) throws IOException { + // 회원 닉네임 수정 + @PatchMapping("nickname") + public ResponseEntity changeUserNickname(@Valid NicknameChangeRequest request) { - userService.updateUser(request); + userService.changeUserNickname(request.getUserId(), request.getNickname()); return new ResponseEntity(HttpStatus.OK); } + // 회원 프로필 이미지 수정 + @PatchMapping("profile-image") + public ResponseEntity changeUserNickname(@Valid ProfileImageChangeRequest request) throws IOException { + + userService.changeProfileImage(request.getUserId(), request.getImage()); + return new ResponseEntity(HttpStatus.OK); + } + // 확인 안한 알림 목록 조회 @GetMapping("notification/{userId}/") public ResponseEntity> getUncheckedNotificationList(@PathVariable("userId") Long userId){ diff --git a/src/main/java/igoMoney/BE/domain/User.java b/src/main/java/igoMoney/BE/domain/User.java index 1a067a7..8b45c6e 100644 --- a/src/main/java/igoMoney/BE/domain/User.java +++ b/src/main/java/igoMoney/BE/domain/User.java @@ -1,6 +1,5 @@ package igoMoney.BE.domain; -import igoMoney.BE.dto.request.UserUpdateRequest; import jakarta.persistence.*; import lombok.*; import org.springframework.data.annotation.CreatedDate; @@ -87,12 +86,14 @@ public String getPassword() { // 회원관리 - public void updateUser(UserUpdateRequest request, String imageUrl) { - this.nickname = request.getNickname(); - if(request.getImageChanged()){ - this.image = imageUrl; - } + public void updateProfileImage(String imageUUID) { + this.image = imageUUID; } + + public void updateNickname(String newNick) { + this.nickname = newNick; + } + public void updateUserStatus(Boolean inChallenge, Long myChallengeId) { this.inChallenge = inChallenge; this.myChallengeId = myChallengeId; diff --git a/src/main/java/igoMoney/BE/dto/request/NicknameChangeRequest.java b/src/main/java/igoMoney/BE/dto/request/NicknameChangeRequest.java new file mode 100644 index 0000000..5b10246 --- /dev/null +++ b/src/main/java/igoMoney/BE/dto/request/NicknameChangeRequest.java @@ -0,0 +1,20 @@ +package igoMoney.BE.dto.request; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; +import lombok.*; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class NicknameChangeRequest { + + @NotNull + private Long userId; + @NotBlank(message = "닉네임은 공백일 수 없습니다.") + @Pattern(regexp = "^[^\n\t\r\f]{3,8}$", message = "닉네임은 \\t, \\r, \\n, \\f 제외 최소3자 최대 8자여야 합니다.") + private String nickname; +} diff --git a/src/main/java/igoMoney/BE/dto/request/UserUpdateRequest.java b/src/main/java/igoMoney/BE/dto/request/ProfileImageChangeRequest.java similarity index 55% rename from src/main/java/igoMoney/BE/dto/request/UserUpdateRequest.java rename to src/main/java/igoMoney/BE/dto/request/ProfileImageChangeRequest.java index 41f7d5f..ab9739b 100644 --- a/src/main/java/igoMoney/BE/dto/request/UserUpdateRequest.java +++ b/src/main/java/igoMoney/BE/dto/request/ProfileImageChangeRequest.java @@ -1,6 +1,5 @@ package igoMoney.BE.dto.request; -import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import lombok.*; import org.springframework.web.multipart.MultipartFile; @@ -10,12 +9,10 @@ @NoArgsConstructor @AllArgsConstructor @Builder -public class UserUpdateRequest { +public class ProfileImageChangeRequest { @NotNull - private Long id; - @NotEmpty - private String nickname; + private Long userId; + @NotNull private MultipartFile image; - private Boolean imageChanged; // 프로필 이미지 변경 여부 } diff --git a/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java b/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java index 5e27886..b885544 100644 --- a/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java +++ b/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java @@ -1,6 +1,5 @@ package igoMoney.BE.dto.request; -import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.*; diff --git a/src/main/java/igoMoney/BE/service/UserService.java b/src/main/java/igoMoney/BE/service/UserService.java index 9a61fe3..cd6c0c5 100644 --- a/src/main/java/igoMoney/BE/service/UserService.java +++ b/src/main/java/igoMoney/BE/service/UserService.java @@ -4,7 +4,6 @@ import igoMoney.BE.common.exception.ErrorCode; import igoMoney.BE.domain.Notification; import igoMoney.BE.domain.User; -import igoMoney.BE.dto.request.UserUpdateRequest; import igoMoney.BE.dto.response.NotificationResponse; import igoMoney.BE.dto.response.UserResponse; import igoMoney.BE.repository.NotificationRepository; @@ -13,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; @@ -33,12 +33,13 @@ public class UserService { public UserResponse getUser(Long userId) { User findUser = getUserOrThrow(userId); + String imageUrl = imageService.processImage(findUser.getImage()); UserResponse response = UserResponse.builder() .id(findUser.getId()) .email(findUser.getEmail()) .nickname(findUser.getNickname()) - .image(findUser.getImage()) + .image(imageUrl) .role(findUser.getRole()) .build(); @@ -56,26 +57,24 @@ public void checkNicknameDuplicate(String nickname) { } - // 회원정보 변경하기 (회원가입 시 애플은 빈칸으로 가입됨. 닉네임 설정 후 홈화면 나옴) - public void updateUser(UserUpdateRequest request) throws IOException { + // 회원 닉네임 변경하기 (회원가입 시 빈칸으로 가입됨. 닉네임 설정 후 홈화면 나옴) + public void changeUserNickname(Long userId, String nickname) { - User findUser = getUserOrThrow(request.getId()); - String image = null; - if (request.getImageChanged() == null){ - request.setImageChanged(false); - } + User findUser = getUserOrThrow(userId); + checkNicknameDuplicate(nickname); + findUser.updateNickname(nickname); + } - if (!request.getNickname().equals(findUser.getNickname())) { - checkNicknameDuplicate(request.getNickname()); - } - if(request.getImageChanged()){ - if(request.getImage().isEmpty() || request.getImage() == null){ - throw new CustomException(ErrorCode.SHOULD_EXIST_IMAGE); - } - image = imageService.uploadImage(request.getImage()); - } + // 회원 프로필 이미지 변경 + public void changeProfileImage(Long userId, MultipartFile image) throws IOException { - findUser.updateUser(request, image); + User findUser = getUserOrThrow(userId); + String uuid = null; + if(image.isEmpty()){ + throw new CustomException(ErrorCode.SHOULD_EXIST_IMAGE); + } + uuid = imageService.uploadImage(image); + findUser.updateProfileImage(uuid); } // 확인 안한 알림 목록 조회