Skip to content

Commit

Permalink
Type: 닉네임변경, 프로필이미지 변경 코드 수정 (#59)
Browse files Browse the repository at this point in the history
[Fix] image 관련 회원정보 수정, 조회 API 수정
  • Loading branch information
pingowl authored Oct 24, 2023
2 parents fe89b53 + 46be3f7 commit 898025c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 37 deletions.
19 changes: 14 additions & 5 deletions src/main/java/igoMoney/BE/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,15 +42,23 @@ public ResponseEntity<Void> checkNicknameDuplicate(@PathVariable("nickname") Str
return new ResponseEntity(HttpStatus.OK);
}

// 회원정보 수정
@PatchMapping("")
public ResponseEntity<Void> updateUser(@Valid UserUpdateRequest request) throws IOException {
// 회원 닉네임 수정
@PatchMapping("nickname")
public ResponseEntity<Void> changeUserNickname(@Valid NicknameChangeRequest request) {

userService.updateUser(request);
userService.changeUserNickname(request.getUserId(), request.getNickname());

return new ResponseEntity(HttpStatus.OK);
}

// 회원 프로필 이미지 수정
@PatchMapping("profile-image")
public ResponseEntity<Void> changeUserNickname(@Valid ProfileImageChangeRequest request) throws IOException {

userService.changeProfileImage(request.getUserId(), request.getImage());
return new ResponseEntity(HttpStatus.OK);
}

// 확인 안한 알림 목록 조회
@GetMapping("notification/{userId}/")
public ResponseEntity<List<NotificationResponse>> getUncheckedNotificationList(@PathVariable("userId") Long userId){
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/igoMoney/BE/domain/User.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/igoMoney/BE/dto/request/NicknameChangeRequest.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; // 프로필 이미지 변경 여부
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
37 changes: 18 additions & 19 deletions src/main/java/igoMoney/BE/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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);
}

// 확인 안한 알림 목록 조회
Expand Down

0 comments on commit 898025c

Please sign in to comment.