Skip to content

Commit

Permalink
feat:[kakao-tech-campus-2nd-step3#84]- add userImage
Browse files Browse the repository at this point in the history
유저 이미지 url 추가
  • Loading branch information
yooonwodyd committed Nov 6, 2024
1 parent f011449 commit 1f79ee2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.helpmeCookies.user.controller;

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand All @@ -13,14 +16,18 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.helpmeCookies.global.jwt.JwtUser;
import com.helpmeCookies.product.dto.FileUploadResponse;
import com.helpmeCookies.product.dto.ProductImageResponse;
import com.helpmeCookies.user.controller.apiDocs.UserApiDocs;
import com.helpmeCookies.user.dto.response.UserCommonInfoRes;
import com.helpmeCookies.user.dto.request.UserReq;
import com.helpmeCookies.user.dto.response.UserDetailsInfoRes;
import com.helpmeCookies.user.dto.UserTypeDto;
import com.helpmeCookies.user.dto.response.UserFollowingRes;
import com.helpmeCookies.user.dto.response.UserImageResponse;
import com.helpmeCookies.user.service.UserService;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -52,13 +59,20 @@ public ResponseEntity<UserTypeDto> getUserType(
return ResponseEntity.ok(userService.getUserType(jwtUser.getId()));
}

@PostMapping("/images")
public ResponseEntity<UserImageResponse> uploadImages(List<MultipartFile> files) throws
IOException {
List<FileUploadResponse> responses = userService.uploadMultiFiles(files);
return ResponseEntity.ok(new UserImageResponse(responses.stream().map(FileUploadResponse::photoUrl).toList()));
}

@PutMapping("/v1/users")
public String updateUser(
@AuthenticationPrincipal JwtUser jwtUser,
@RequestBody UserReq userReq
) {
// UserInfoDto를 통해서 유저 정보를 수정한다.
userService.updateUser(userReq, jwtUser.getId());
userService.updateUser(userReq.toUserCommonInfoDto(), userReq.toUserInfoDto(), jwtUser.getId());
return "ok";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
public interface UserRepository extends JpaRepository<User, Long>, UserCustomRepository {
Optional<User> findById(Long id);
Optional<User> findByUserInfoEmail(String email);
boolean existsByNicknameAndIdNot(String nickname, Long userId);
}
30 changes: 21 additions & 9 deletions src/main/java/com/helpmeCookies/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.helpmeCookies.user.service;

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

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

import com.helpmeCookies.global.exception.user.ResourceNotFoundException;
import com.helpmeCookies.global.utils.AwsS3FileUtils;
import com.helpmeCookies.product.dto.FileUploadResponse;
import com.helpmeCookies.user.dto.UserCommonInfoDto;
import com.helpmeCookies.user.dto.UserDto;
import com.helpmeCookies.user.dto.UserInfoDto;
import com.helpmeCookies.user.dto.UserTypeDto;
Expand All @@ -28,6 +35,7 @@ public class UserService {
private final UserRepository userRepository;
private final ArtistInfoRepository artistInfoRepository;
private final SocialRepository socialRepository;
private final AwsS3FileUtils awsS3FileUtils;


@Transactional
Expand All @@ -39,25 +47,29 @@ public UserDto getUserInfo(Long userId) {
}

@Transactional
public UserDto updateUser(UserReq userReq, Long userId) {
public UserDto updateUser(UserCommonInfoDto userCommonInfoDto, UserInfoDto userInfoDto, Long userId) {

User existingUser = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다."));

existingUser.updateUserCommonInfo(userReq.nickname(), userReq.userImageUrl());
UserInfo userInfo = UserInfo.builder().name(userReq.name())
.email(userReq.email())
.birthdate(userReq.birthdate())
.phone(userReq.phone())
.address(userReq.address())
.hashTags(userReq.hashTags())
.build();
if (userRepository.existsByNicknameAndIdNot(userCommonInfoDto.nickname(), existingUser.getId())) {
throw new DuplicateRequestException("이미 존재하는 닉네임입니다.");
}

existingUser.updateUserCommonInfo(userCommonInfoDto.nickname(), userCommonInfoDto.ImageUrl());

UserInfo userInfo = userInfoDto.toEntity();
existingUser.updateUserInfo(userInfo);

return UserDto.fromEntity(userRepository.save(existingUser));
}

@Transactional
public List<FileUploadResponse> uploadMultiFiles(List<MultipartFile> files) throws IOException {
return awsS3FileUtils.uploadMultiImages(files);
}


@Transactional
public UserTypeDto getUserType(Long userId) {
String usertype = artistInfoRepository.findByUserId(userId)
Expand Down

0 comments on commit 1f79ee2

Please sign in to comment.