Skip to content

Commit

Permalink
Merge pull request #169 from BETTER-iTER/feature/168
Browse files Browse the repository at this point in the history
[Feature/168] 프로필 수정 기능 구현
  • Loading branch information
luke0408 authored Feb 20, 2024
2 parents 90a4e80 + bbe5d35 commit d3af6e3
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.example.betteriter.fo_domain.mypage.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.betteriter.fo_domain.mypage.converter.MypageResponseConverter;
import com.example.betteriter.fo_domain.mypage.dto.MypageRequest;
import com.example.betteriter.fo_domain.mypage.dto.MypageResponse;
import com.example.betteriter.fo_domain.mypage.service.MypageService;
import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.dto.ReviewResponse;
import com.example.betteriter.fo_domain.user.domain.Users;
import com.example.betteriter.global.common.response.ResponseDto;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "MypageControllers", description = "Mypage API")
@Slf4j
Expand Down Expand Up @@ -143,4 +149,20 @@ public ResponseDto<MypageResponse.PointDetailDto> getPointDetail() {
Integer totalScrapCount = mypageService.getTotalScrapCount(user);
return ResponseDto.onSuccess(MypageResponseConverter.toPointDetailDto(user, totalLikeCount, totalScrapCount));
}

/**
* user profile 수정
*
* @param request 수정할 user 정보
* @return void
*/
@PutMapping("/profile")
public ResponseDto<Void> updateUserProfile(
@RequestPart(value = "files") MultipartFile image,
@Valid @RequestPart(value = "key") MypageRequest.UpdateProfileRequest request
) {
Users user = mypageService.getCurrentUser();
mypageService.updateUserProfile(user, request, image);
return ResponseDto.onSuccess(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package com.example.betteriter.fo_domain.mypage.dto;

import com.example.betteriter.global.constant.Job;

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

public class MypageRequest {

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UpdateProfileRequest {
private String nickname;
private Job job;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package com.example.betteriter.fo_domain.mypage.service;

import static com.example.betteriter.global.common.code.status.ErrorStatus.*;

import com.example.betteriter.fo_domain.follow.service.FollowService;
import com.example.betteriter.fo_domain.mypage.converter.MypageResponseConverter;
import com.example.betteriter.fo_domain.mypage.dto.MypageRequest;
import com.example.betteriter.fo_domain.mypage.dto.MypageResponse;
import com.example.betteriter.fo_domain.mypage.exception.MypageHandler;
import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.service.ReviewService;
import com.example.betteriter.fo_domain.user.domain.Users;
import com.example.betteriter.fo_domain.user.domain.UsersDetail;
import com.example.betteriter.fo_domain.user.service.UserService;
import com.example.betteriter.global.common.code.status.ErrorStatus;
import com.example.betteriter.infra.s3.S3Service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -22,10 +31,11 @@
public class MypageService {

private static final int SIZE = 10;

private final UserService userService;

private final ReviewService reviewService;
private final FollowService followService;
private final S3Service s3Service;

@Transactional(readOnly = true)
public Page<Review> getMyReviewList(int page) {
Expand Down Expand Up @@ -106,4 +116,24 @@ public Page<Review> getUserReviewList(Long userId, int page) {
Users targetUser = userService.getUserById(userId);
return reviewService.getReviewList(targetUser, page, SIZE);
}

public void updateUserProfile(Users user, MypageRequest.UpdateProfileRequest request, MultipartFile image) {
// 1. 프로필 이미지 업로드
String profileImageUrl = this.uploadProfileImage(user, image);

// 2. 프로필 정보 수정
UsersDetail detail = user.getUsersDetail();
detail.updateProfile(request, profileImageUrl);
}

private String uploadProfileImage(Users user, MultipartFile image) {
this.checkUploadProfileImageRequestValidation(image);
return s3Service.uploadImage(image, user);
}

private void checkUploadProfileImageRequestValidation(MultipartFile image) {
if(image == null || image.isEmpty()) {
throw new MypageHandler(_IMAGE_FILE_UPLOAD_REQUEST_IS_NOT_VALID);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.betteriter.fo_domain.user.domain;

import com.example.betteriter.fo_domain.mypage.dto.MypageRequest;
import com.example.betteriter.global.common.entity.BaseEntity;
import com.example.betteriter.global.constant.Job;
import lombok.AccessLevel;
Expand Down Expand Up @@ -44,4 +45,10 @@ private UsersDetail(String nickName, Job job, String profileImage,
this.point = point;
this.quizCount = quizCount;
}

public void updateProfile(MypageRequest.UpdateProfileRequest request, String profileImageUrl) {
this.nickName = request.getNickname();
this.job = request.getJob();
this.profileImage = profileImageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import com.example.betteriter.global.common.code.status.ErrorStatus;
import com.example.betteriter.global.util.RedisUtil;
import com.example.betteriter.global.util.SecurityUtil;
import com.example.betteriter.infra.s3.S3Service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.stream.Collectors;
Expand All @@ -30,6 +33,7 @@ public class UserService {
private final UsersWithdrawReasonRepository usersWithdrawReasonRepository;
private final RedisUtil redisUtil;
private final SecurityUtil securityUtil;
private final S3Service s3Service;

/* 로그아웃 */
@Transactional
Expand Down Expand Up @@ -94,4 +98,5 @@ public Users getUserByEmail(String email) {
return this.usersRepository.findByEmail(email)
.orElseThrow(() -> new UserHandler(ErrorStatus._USER_NOT_FOUND));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.domain.ReviewImage;
import com.example.betteriter.fo_domain.user.domain.Users;

import org.springframework.web.multipart.MultipartFile;

public interface ImageUploadService {

ReviewImage uploadImage(MultipartFile image, Review review, int orderNum);

String uploadImage(MultipartFile image, Users user);

void updateImage(MultipartFile multipartFile, ReviewImage reviewImage);
}
25 changes: 25 additions & 0 deletions src/main/java/com/example/betteriter/infra/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.domain.ReviewImage;
import com.example.betteriter.fo_domain.review.exception.ReviewHandler;
import com.example.betteriter.fo_domain.user.domain.Users;

import java.io.InputStream;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -59,6 +61,29 @@ public ReviewImage uploadImage(MultipartFile image, Review review, int orderNum)
.build();
}

@Override
public String uploadImage(MultipartFile image, Users user) {
String originalFilename = Optional.ofNullable(image.getOriginalFilename())
.orElseThrow(() -> new ReviewHandler(_IMAGE_FILE_NAME_IS_NOT_EXIST));

String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString();
String key = FOLDER + "/" + user.getEmail() + "/" + user.getId().toString() + "/" + fileName + fileExtension;

ObjectMetadata objectMetaData = new ObjectMetadata();
objectMetaData.setContentType(image.getContentType());

try (InputStream inputStream = image.getInputStream()) {

s3Client.putObject(new PutObjectRequest(bucketName, key, inputStream, objectMetaData));

} catch (Exception e) {
throw new ReviewHandler(_IMAGE_FILE_UPLOAD_FAILED);
}

return getImageUrl(key);
}

@Override
public void updateImage(MultipartFile multipartFile, ReviewImage reviewImage) {
validateImageFileExists(multipartFile);
Expand Down

0 comments on commit d3af6e3

Please sign in to comment.