From da64130519bf375d009b98696f611f36305f88c6 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Sun, 29 Oct 2023 21:24:25 +0900 Subject: [PATCH 01/10] =?UTF-8?q?[feat]=20=EC=9C=A0=EC=A0=80=20=EB=8B=89?= =?UTF-8?q?=EB=84=A4=EC=9E=84=20=EC=83=9D=EC=84=B1=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 21 +++++++++++++++---- .../dto/request/ProduceNicknameRequest.java | 14 +++++++++++++ .../user/repository/UserRepository.java | 3 +++ .../farmususer/user/service/UserService.java | 14 +++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/main/java/modernfarmer/server/farmususer/user/dto/request/ProduceNicknameRequest.java diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index 60eed70..f16e786 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -1,17 +1,16 @@ package modernfarmer.server.farmususer.user.controller; import lombok.RequiredArgsConstructor; +import lombok.Value; import lombok.extern.slf4j.Slf4j; +import modernfarmer.server.farmususer.user.dto.request.ProduceNicknameRequest; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; import modernfarmer.server.farmususer.user.service.AuthService; import modernfarmer.server.farmususer.user.service.UserService; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; @@ -25,6 +24,18 @@ public class UserController { private final JwtTokenProvider jwtTokenProvider; + + @PostMapping(value = "/nickname") + public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ + + String userId = jwtTokenProvider.getUserId(request); + + ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName()); + + return responseDto; + } + + @DeleteMapping("/logout") public ResponseDto logout(HttpServletRequest request) { @@ -50,4 +61,6 @@ public TokenResponseDto reissueToken(HttpServletRequest request) { return reissueTokenResponseDto; } + + } diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/request/ProduceNicknameRequest.java b/src/main/java/modernfarmer/server/farmususer/user/dto/request/ProduceNicknameRequest.java new file mode 100644 index 0000000..a71b618 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/request/ProduceNicknameRequest.java @@ -0,0 +1,14 @@ +package modernfarmer.server.farmususer.user.dto.request; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class ProduceNicknameRequest { + + private String nickName; +} diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java index cacff0b..cc134bf 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java @@ -17,6 +17,9 @@ public interface UserRepository extends JpaRepository { @Query("SELECT a.role FROM User AS a WHERE a.id = :userId") String findUserRole(@Param("userId") Long userId); + @Query("update User as u set u = :nickName where u.id = :userId") + void updateUserNickname(@Param("nickName") String nickName, @Param("userId") Long userId); + diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index b3cc99f..7e15a56 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -3,6 +3,7 @@ import modernfarmer.server.farmususer.global.exception.notfound.NotFoundRefreshTokenException; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; +import modernfarmer.server.farmususer.user.entity.User; import modernfarmer.server.farmususer.user.repository.UserRepository; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; import org.springframework.beans.factory.annotation.Autowired; @@ -32,6 +33,19 @@ public UserService(WebClient webClient, UserRepository userRepository, JwtTokenP } + public ResponseDto produceNickname(Long userId, String nickName){ + + userRepository.updateUserNickname(nickName, userId); + + ResponseDto responseDto = ResponseDto.builder() + .code(200) + .message("OK") + .build(); + + return responseDto; + } + + public ResponseDto logout(Long userId) { deleteValueByKey(String.valueOf(userId)); From e335fa0b1ef1bec66e17f1cdeba7610d4f46d0c6 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Sun, 29 Oct 2023 21:27:43 +0900 Subject: [PATCH 02/10] =?UTF-8?q?[feat]=20=EC=9C=A0=EC=A0=80=20=EB=8B=89?= =?UTF-8?q?=EB=84=A4=EC=9E=84=20=EC=88=98=EC=A0=95=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../farmususer/user/controller/UserController.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index f16e786..6ebc5ab 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -35,6 +35,16 @@ public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRe return responseDto; } + @PutMapping(value = "/nickname") + public ResponseDto updateNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ + + String userId = jwtTokenProvider.getUserId(request); + + ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName()); + + return responseDto; + } + @DeleteMapping("/logout") public ResponseDto logout(HttpServletRequest request) { From 10473aee57b9709ddd2329d9aa67d441de2e7dc9 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Sun, 29 Oct 2023 21:36:49 +0900 Subject: [PATCH 03/10] =?UTF-8?q?[feat]=20=EC=9C=A0=EC=A0=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20API=20=EB=B0=8F=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20put=20->=20patch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../farmususer/user/controller/UserController.java | 13 ++++++++++++- .../farmususer/user/repository/UserRepository.java | 3 +++ .../server/farmususer/user/service/UserService.java | 13 +++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index 6ebc5ab..6e421c9 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -24,6 +24,17 @@ public class UserController { private final JwtTokenProvider jwtTokenProvider; + @DeleteMapping(value = "/delete") + public ResponseDto deleteUser(HttpServletRequest request){ + + String userId = jwtTokenProvider.getUserId(request); + + ResponseDto responseDto = userService.deleteUser(Long.valueOf(userId)); + + return responseDto; + } + + @PostMapping(value = "/nickname") public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ @@ -35,7 +46,7 @@ public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRe return responseDto; } - @PutMapping(value = "/nickname") + @PatchMapping(value = "/nickname") public ResponseDto updateNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ String userId = jwtTokenProvider.getUserId(request); diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java index cc134bf..b9072b4 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java @@ -20,6 +20,9 @@ public interface UserRepository extends JpaRepository { @Query("update User as u set u = :nickName where u.id = :userId") void updateUserNickname(@Param("nickName") String nickName, @Param("userId") Long userId); + @Query("delete from User as u where u.id = : userId") + void deleteUser(@Param("userId") Long userId); + diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 7e15a56..11c58e4 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -32,6 +32,19 @@ public UserService(WebClient webClient, UserRepository userRepository, JwtTokenP this.redisTemplate = redisTemplate; } + public ResponseDto deleteUser(Long userId){ + + userRepository.deleteUser(userId); + + ResponseDto responseDto = ResponseDto + .builder() + .code(200) + .message("OK") + .build(); + + return responseDto; + } + public ResponseDto produceNickname(Long userId, String nickName){ From be6b4b1b7136f503d08888a18c4e0a3daa6127c9 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Sun, 29 Oct 2023 21:59:18 +0900 Subject: [PATCH 04/10] =?UTF-8?q?[feat]=20=EC=98=A8=EB=B3=B4=EB=94=A9=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=20=EB=9E=9C=EB=8D=94=EB=A7=81=20API,=20Respo?= =?UTF-8?q?nseDto=20=EC=83=81=EC=86=8D=EA=B5=AC=EC=A1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 13 +++++++++++ .../dto/response/ProfileImageResponseDto.java | 14 +++++++++++ .../user/dto/response/ResponseDto.java | 1 - .../user/dto/response/TokenResponseDto.java | 9 +++----- .../user/repository/UserRepository.java | 3 +++ .../farmususer/user/service/UserService.java | 23 +++++++++++++++++++ 6 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/main/java/modernfarmer/server/farmususer/user/dto/response/ProfileImageResponseDto.java diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index 6e421c9..ec84c50 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -4,6 +4,7 @@ import lombok.Value; import lombok.extern.slf4j.Slf4j; import modernfarmer.server.farmususer.user.dto.request.ProduceNicknameRequest; +import modernfarmer.server.farmususer.user.dto.response.ProfileImageResponseDto; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; @@ -24,6 +25,18 @@ public class UserController { private final JwtTokenProvider jwtTokenProvider; + + @GetMapping(value = "/profileImage") + public ProfileImageResponseDto selectProfileImage(HttpServletRequest request){ + + String userId = jwtTokenProvider.getUserId(request); + + ProfileImageResponseDto profileImageResponseDto = userService.selectProfileImage(Long.valueOf(userId)); + + return profileImageResponseDto; + } + + @DeleteMapping(value = "/delete") public ResponseDto deleteUser(HttpServletRequest request){ diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/ProfileImageResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ProfileImageResponseDto.java new file mode 100644 index 0000000..1ae6158 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ProfileImageResponseDto.java @@ -0,0 +1,14 @@ +package modernfarmer.server.farmususer.user.dto.response; + + +import lombok.*; +import lombok.experimental.SuperBuilder; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@SuperBuilder +public class ProfileImageResponseDto extends ResponseDto{ + + private String profileImage; +} diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java index 49a0746..c78237c 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java @@ -6,7 +6,6 @@ @NoArgsConstructor @AllArgsConstructor -@Data @SuperBuilder public class ResponseDto { private int code; diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/TokenResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/TokenResponseDto.java index d992472..beba6e7 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/dto/response/TokenResponseDto.java +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/TokenResponseDto.java @@ -2,17 +2,14 @@ import lombok.*; +import lombok.experimental.SuperBuilder; @NoArgsConstructor @AllArgsConstructor @ToString -@Builder +@SuperBuilder @Getter -public class TokenResponseDto { - - private int code; - - private String message; +public class TokenResponseDto extends ResponseDto{ private String accessToken; diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java index b9072b4..5dec76e 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java @@ -23,6 +23,9 @@ public interface UserRepository extends JpaRepository { @Query("delete from User as u where u.id = : userId") void deleteUser(@Param("userId") Long userId); + @Query("select u.profileImage from User as u where u.id = :userId") + String selectUserProfileImage(@Param("userId") Long userId); + diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 11c58e4..88f3b8e 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -1,6 +1,7 @@ package modernfarmer.server.farmususer.user.service; import modernfarmer.server.farmususer.global.exception.notfound.NotFoundRefreshTokenException; +import modernfarmer.server.farmususer.user.dto.response.ProfileImageResponseDto; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; import modernfarmer.server.farmususer.user.entity.User; @@ -32,6 +33,28 @@ public UserService(WebClient webClient, UserRepository userRepository, JwtTokenP this.redisTemplate = redisTemplate; } + public ProfileImageResponseDto selectProfileImage(Long userId){ + + String userProfileImage = userRepository.selectUserProfileImage(userId); + + + ProfileImageResponseDto profileImageResponseDto = ProfileImageResponseDto + .builder() + .code(200) + .message("OK") + .profileImage(userProfileImage) + .build(); + + + return profileImageResponseDto; + } + + + + + + + public ResponseDto deleteUser(Long userId){ userRepository.deleteUser(userId); From c533aea8f97d7e44fa48ebd259d492a7693ae95a Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 02:12:33 +0900 Subject: [PATCH 05/10] =?UTF-8?q?[feat]=20=ED=94=84=EB=A1=9C=ED=95=84=20?= =?UTF-8?q?=EC=82=AC=EC=A7=84=20=EC=97=85=EB=A1=9C=EB=93=9C=20api=20?= =?UTF-8?q?=EB=AF=B8=EC=99=84=EC=84=B1,=20s3=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../config/s3/AmazonS3ResourceStorage.java | 34 ++++++++++++++ .../global/config/s3/FileDetail.java | 34 ++++++++++++++ .../global/config/s3/MultipartUtil.java | 45 +++++++++++++++++++ .../farmususer/global/config/s3/S3Config.java | 28 ++++++++++++ .../user/controller/UserController.java | 16 +++++-- .../farmususer/user/service/UserService.java | 35 ++++++++++++--- 7 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java create mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java create mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java create mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Config.java diff --git a/build.gradle b/build.gradle index a8fb8ae..0fe7026 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,8 @@ dependencies { implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2' + + implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' } tasks.named('test') { diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java new file mode 100644 index 0000000..402cb8c --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java @@ -0,0 +1,34 @@ +package modernfarmer.server.farmususer.global.config.s3; + +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.PutObjectRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; + +@Component +@RequiredArgsConstructor +public class AmazonS3ResourceStorage { + @Value("${cloud.aws.s3.bucket}") + private String bucket; + private final AmazonS3Client amazonS3Client; + + public void store(String fullPath, MultipartFile multipartFile) { + File file = new File(MultipartUtil.getLocalHomeDirectory(), fullPath); + try { + multipartFile.transferTo(file); + amazonS3Client.putObject(new PutObjectRequest(bucket, fullPath, file) + .withCannedAcl(CannedAccessControlList.PublicRead)); + } catch (Exception e) { + throw new RuntimeException(); + } finally { + if (file.exists()) { + file.delete(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java new file mode 100644 index 0000000..f777dfd --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java @@ -0,0 +1,34 @@ +package modernfarmer.server.farmususer.global.config.s3; + +import lombok.*; +import org.springframework.web.multipart.MultipartFile; + +import java.time.LocalDateTime; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@Builder +public class FileDetail { + private String id; + private String name; + private String format; + private String path; + private long bytes; + + @Builder.Default + private LocalDateTime createdAt = LocalDateTime.now(); + + public static FileDetail multipartOf(MultipartFile multipartFile) { + final String fileId = MultipartUtil.createFileId(); + final String format = MultipartUtil.getFormat(multipartFile.getContentType()); + return FileDetail.builder() + .id(fileId) + .name(multipartFile.getOriginalFilename()) + .format(format) + .path(MultipartUtil.createPath(fileId, format)) + .bytes(multipartFile.getSize()) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java new file mode 100644 index 0000000..f60f54b --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java @@ -0,0 +1,45 @@ +package modernfarmer.server.farmususer.global.config.s3; + +import org.springframework.util.StringUtils; + +import java.util.UUID; + +public final class MultipartUtil { + private static final String BASE_DIR = "images"; + + /** + * 로컬에서의 사용자 홈 디렉토리 경로를 반환합니다. + */ + public static String getLocalHomeDirectory() { + return System.getProperty("user.home"); + } + + /** + * 새로운 파일 고유 ID를 생성합니다. + * @return 36자리의 UUID + */ + public static String createFileId() { + return UUID.randomUUID().toString(); + } + + /** + * Multipart 의 ContentType 값에서 / 이후 확장자만 잘라냅니다. + * @param contentType ex) image/png + * @return ex) png + */ + public static String getFormat(String contentType) { + if (StringUtils.hasText(contentType)) { + return contentType.substring(contentType.lastIndexOf('/') + 1); + } + return null; + } + + /** + * 파일의 전체 경로를 생성합니다. + * @param fileId 생성된 파일 고유 ID + * @param format 확장자 + */ + public static String createPath(String fileId, String format) { + return String.format("%s/%s.%s", BASE_DIR, fileId, format); + } +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Config.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Config.java new file mode 100644 index 0000000..9d58626 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Config.java @@ -0,0 +1,28 @@ +package modernfarmer.server.farmususer.global.config.s3; + +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class S3Config { + @Value("${cloud.aws.credentials.access-key}") + private String accessKey; + @Value("${cloud.aws.credentials.secret-key}") + private String secretKey; + @Value("${cloud.aws.region.static}") + private String region; + + @Bean + public AmazonS3Client amazonS3Client() { + BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey); + return (AmazonS3Client) AmazonS3ClientBuilder.standard() + .withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index ec84c50..5d1abd6 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -1,17 +1,16 @@ package modernfarmer.server.farmususer.user.controller; import lombok.RequiredArgsConstructor; -import lombok.Value; import lombok.extern.slf4j.Slf4j; import modernfarmer.server.farmususer.user.dto.request.ProduceNicknameRequest; import modernfarmer.server.farmususer.user.dto.response.ProfileImageResponseDto; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; -import modernfarmer.server.farmususer.user.service.AuthService; import modernfarmer.server.farmususer.user.service.UserService; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; @@ -24,7 +23,16 @@ public class UserController { private final UserService userService; private final JwtTokenProvider jwtTokenProvider; + @PostMapping(value = "/profileImage") + public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile){ + String userId = jwtTokenProvider.getUserId(request); + + ResponseDto responseDto = userService.produceProfileImage(Long.valueOf(userId), multipartFile); + + return responseDto; + + } @GetMapping(value = "/profileImage") public ProfileImageResponseDto selectProfileImage(HttpServletRequest request){ @@ -50,7 +58,7 @@ public ResponseDto deleteUser(HttpServletRequest request){ @PostMapping(value = "/nickname") - public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ + public ResponseDto produceNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){ String userId = jwtTokenProvider.getUserId(request); @@ -60,7 +68,7 @@ public ResponseDto produceNickname(HttpServletRequest request, ProduceNicknameRe } @PatchMapping(value = "/nickname") - public ResponseDto updateNickname(HttpServletRequest request, ProduceNicknameRequest produceNicknameRequest){ + public ResponseDto updateNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){ String userId = jwtTokenProvider.getUserId(request); diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 88f3b8e..7a31e47 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -1,36 +1,57 @@ package modernfarmer.server.farmususer.user.service; +import lombok.extern.slf4j.Slf4j; +import modernfarmer.server.farmususer.global.config.s3.AmazonS3ResourceStorage; +import modernfarmer.server.farmususer.global.config.s3.FileDetail; import modernfarmer.server.farmususer.global.exception.notfound.NotFoundRefreshTokenException; import modernfarmer.server.farmususer.user.dto.response.ProfileImageResponseDto; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; import modernfarmer.server.farmususer.user.dto.response.TokenResponseDto; -import modernfarmer.server.farmususer.user.entity.User; import modernfarmer.server.farmususer.user.repository.UserRepository; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.reactive.function.client.WebClient; -import java.util.Objects; - +@Slf4j @Service public class UserService { public JwtTokenProvider jwtTokenProvider; public RedisTemplate redisTemplate; - public UserRepository userRepository; + private final AmazonS3ResourceStorage amazonS3ResourceStorage; - private final WebClient webClient; @Autowired - public UserService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate) { - this.webClient = webClient; + public UserService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate, AmazonS3ResourceStorage amazonS3ResourceStoraget) { + this.userRepository = userRepository; this.jwtTokenProvider = jwtTokenProvider; this.redisTemplate = redisTemplate; + this.amazonS3ResourceStorage = amazonS3ResourceStoraget; + } + + public ResponseDto produceProfileImage(Long userId, MultipartFile multipartFile){ + + + FileDetail fileDetail = FileDetail.multipartOf(multipartFile); + amazonS3ResourceStorage.store(fileDetail.getPath(), multipartFile); + + log.info(String.valueOf(fileDetail)); + + + ResponseDto responseDto = ResponseDto + .builder() + .code(200) + .message("OK") + .build(); + + return responseDto; } public ProfileImageResponseDto selectProfileImage(Long userId){ From cc0d0938649d1ddf75dc266820d188dee85fee17 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 02:18:49 +0900 Subject: [PATCH 06/10] =?UTF-8?q?[fix]=20ci/cd=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modernfarmer/server/farmususer/user/service/UserService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 7a31e47..8043a44 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -26,7 +26,6 @@ public class UserService { public UserRepository userRepository; private final AmazonS3ResourceStorage amazonS3ResourceStorage; - @Autowired public UserService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate, AmazonS3ResourceStorage amazonS3ResourceStoraget) { From e86eb104f2109f8d925ec8420b90090efb4c5467 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 02:44:44 +0900 Subject: [PATCH 07/10] =?UTF-8?q?[fix]=20ci/cd=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index e1c3968..9f0778b 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -2,7 +2,7 @@ name: ModernFarmer CI/CD on: push: - branches: ["feature_13/카카오,-구글-로그인-수정"] + branches: ["feature_14/-유저-관련-API-구현"] pull_request: branches: ["dev"] From 66518d85379c441434a061b9a61f2fed1e83a97d Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 02:52:16 +0900 Subject: [PATCH 08/10] =?UTF-8?q?[fix]=20ci/cd=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modernfarmer/server/farmususer/user/service/UserService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 8043a44..0def733 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -35,6 +35,7 @@ public UserService(WebClient webClient, UserRepository userRepository, JwtTokenP this.amazonS3ResourceStorage = amazonS3ResourceStoraget; } + public ResponseDto produceProfileImage(Long userId, MultipartFile multipartFile){ From 32546c0a4faaae2a6c583b70daad0dae9ad66047 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 14:45:23 +0900 Subject: [PATCH 09/10] =?UTF-8?q?[fix]=20form-data=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/farmususer/user/controller/UserController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index 5d1abd6..c24faee 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -9,6 +9,7 @@ import modernfarmer.server.farmususer.user.service.UserService; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -23,7 +24,7 @@ public class UserController { private final UserService userService; private final JwtTokenProvider jwtTokenProvider; - @PostMapping(value = "/profileImage") + @PostMapping(value = "/profileImage", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile){ String userId = jwtTokenProvider.getUserId(request); From 9066ea365a775ff9ec71629ddf63fe9a2fd2f4d9 Mon Sep 17 00:00:00 2001 From: seungryeol Date: Mon, 30 Oct 2023 16:44:36 +0900 Subject: [PATCH 10/10] =?UTF-8?q?[feat]=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20API=20=EB=B0=8F=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20early=20=EB=B3=80=EC=88=98=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/s3/AmazonS3ResourceStorage.java | 34 ---------- .../global/config/s3/FileDetail.java | 34 ---------- .../global/config/s3/MultipartUtil.java | 45 ------------ .../global/config/s3/S3Uploader.java | 68 +++++++++++++++++++ .../user/controller/UserController.java | 24 +++---- .../user/dto/response/ResponseDto.java | 1 + .../user/repository/UserRepository.java | 13 ++-- .../farmususer/user/service/AuthService.java | 4 +- .../farmususer/user/service/UserService.java | 40 ++++++----- 9 files changed, 107 insertions(+), 156 deletions(-) delete mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java delete mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java delete mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java create mode 100644 src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Uploader.java diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java deleted file mode 100644 index 402cb8c..0000000 --- a/src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java +++ /dev/null @@ -1,34 +0,0 @@ -package modernfarmer.server.farmususer.global.config.s3; - -import com.amazonaws.services.s3.AmazonS3Client; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.PutObjectRequest; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; - -@Component -@RequiredArgsConstructor -public class AmazonS3ResourceStorage { - @Value("${cloud.aws.s3.bucket}") - private String bucket; - private final AmazonS3Client amazonS3Client; - - public void store(String fullPath, MultipartFile multipartFile) { - File file = new File(MultipartUtil.getLocalHomeDirectory(), fullPath); - try { - multipartFile.transferTo(file); - amazonS3Client.putObject(new PutObjectRequest(bucket, fullPath, file) - .withCannedAcl(CannedAccessControlList.PublicRead)); - } catch (Exception e) { - throw new RuntimeException(); - } finally { - if (file.exists()) { - file.delete(); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java deleted file mode 100644 index f777dfd..0000000 --- a/src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java +++ /dev/null @@ -1,34 +0,0 @@ -package modernfarmer.server.farmususer.global.config.s3; - -import lombok.*; -import org.springframework.web.multipart.MultipartFile; - -import java.time.LocalDateTime; - -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor(access = AccessLevel.PROTECTED) -@Builder -public class FileDetail { - private String id; - private String name; - private String format; - private String path; - private long bytes; - - @Builder.Default - private LocalDateTime createdAt = LocalDateTime.now(); - - public static FileDetail multipartOf(MultipartFile multipartFile) { - final String fileId = MultipartUtil.createFileId(); - final String format = MultipartUtil.getFormat(multipartFile.getContentType()); - return FileDetail.builder() - .id(fileId) - .name(multipartFile.getOriginalFilename()) - .format(format) - .path(MultipartUtil.createPath(fileId, format)) - .bytes(multipartFile.getSize()) - .build(); - } -} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java deleted file mode 100644 index f60f54b..0000000 --- a/src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -package modernfarmer.server.farmususer.global.config.s3; - -import org.springframework.util.StringUtils; - -import java.util.UUID; - -public final class MultipartUtil { - private static final String BASE_DIR = "images"; - - /** - * 로컬에서의 사용자 홈 디렉토리 경로를 반환합니다. - */ - public static String getLocalHomeDirectory() { - return System.getProperty("user.home"); - } - - /** - * 새로운 파일 고유 ID를 생성합니다. - * @return 36자리의 UUID - */ - public static String createFileId() { - return UUID.randomUUID().toString(); - } - - /** - * Multipart 의 ContentType 값에서 / 이후 확장자만 잘라냅니다. - * @param contentType ex) image/png - * @return ex) png - */ - public static String getFormat(String contentType) { - if (StringUtils.hasText(contentType)) { - return contentType.substring(contentType.lastIndexOf('/') + 1); - } - return null; - } - - /** - * 파일의 전체 경로를 생성합니다. - * @param fileId 생성된 파일 고유 ID - * @param format 확장자 - */ - public static String createPath(String fileId, String format) { - return String.format("%s/%s.%s", BASE_DIR, fileId, format); - } -} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Uploader.java b/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Uploader.java new file mode 100644 index 0000000..f020922 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Uploader.java @@ -0,0 +1,68 @@ +package modernfarmer.server.farmususer.global.config.s3; + +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.PutObjectRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Optional; +import java.util.UUID; + +@Slf4j +@Component +public class S3Uploader { + private final AmazonS3Client amazonS3Client; + + @Value("${cloud.aws.s3.bucket}") + private String bucket; + + public S3Uploader(AmazonS3Client amazonS3Client) { + this.amazonS3Client = amazonS3Client; + } + + public String uploadFiles( + MultipartFile multipartFile, String dirName) throws IOException { + File uploadFile = convert(multipartFile).orElseThrow(() -> + new IllegalArgumentException("error: MultipartFile -> File convert fail")); + return upload(uploadFile, dirName); + } + + public String upload(File uploadFile, String filePath) { + String fileName = filePath + "/" + UUID.randomUUID() + uploadFile.getName(); + String uploadImageUrl = putS3(uploadFile, fileName); + removeNewFile(uploadFile); + return uploadImageUrl; + } + + private String putS3(File uploadFile, String fileName) { + amazonS3Client.putObject( + new PutObjectRequest(bucket, fileName, uploadFile) + .withCannedAcl(CannedAccessControlList.PublicRead)); + return amazonS3Client.getUrl(bucket, fileName).toString(); + } + + private void removeNewFile(File targetFile) { + if (targetFile.delete()) { + System.out.println("File delete success"); + return; + } + System.out.println("File delete fail"); + } + + private Optional convert(MultipartFile file) throws IOException { + File convertFile = new File(System.getProperty("user.dir") + "/" + file.getOriginalFilename()); + if (convertFile.createNewFile()) { + try (FileOutputStream fileOutputStream = new FileOutputStream(convertFile)) { + fileOutputStream.write(file.getBytes()); + } + return Optional.of(convertFile); + } + return Optional.empty(); + } +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java index c24faee..f5587cf 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java +++ b/src/main/java/modernfarmer/server/farmususer/user/controller/UserController.java @@ -14,6 +14,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; +import java.io.IOException; @Slf4j @RestController @@ -24,12 +25,12 @@ public class UserController { private final UserService userService; private final JwtTokenProvider jwtTokenProvider; - @PostMapping(value = "/profileImage", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}) - public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile){ + @PostMapping(value = "/profileImage", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile) throws IOException { String userId = jwtTokenProvider.getUserId(request); - - ResponseDto responseDto = userService.produceProfileImage(Long.valueOf(userId), multipartFile); + log.info(userId); + ResponseDto responseDto = userService.emitProfileImage(Long.valueOf(userId), multipartFile); return responseDto; @@ -40,6 +41,7 @@ public ProfileImageResponseDto selectProfileImage(HttpServletRequest request){ String userId = jwtTokenProvider.getUserId(request); + ProfileImageResponseDto profileImageResponseDto = userService.selectProfileImage(Long.valueOf(userId)); return profileImageResponseDto; @@ -59,21 +61,11 @@ public ResponseDto deleteUser(HttpServletRequest request){ @PostMapping(value = "/nickname") - public ResponseDto produceNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){ - - String userId = jwtTokenProvider.getUserId(request); - - ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName()); - - return responseDto; - } - - @PatchMapping(value = "/nickname") - public ResponseDto updateNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){ + public ResponseDto emitNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){ String userId = jwtTokenProvider.getUserId(request); - ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName()); + ResponseDto responseDto = userService.emitNickname(Long.valueOf(userId), produceNicknameRequest.getNickName()); return responseDto; } diff --git a/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java index c78237c..d4169cb 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java +++ b/src/main/java/modernfarmer/server/farmususer/user/dto/response/ResponseDto.java @@ -7,6 +7,7 @@ @NoArgsConstructor @AllArgsConstructor @SuperBuilder +@Getter public class ResponseDto { private int code; private String message; diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java index 5dec76e..33c6fc2 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java @@ -3,6 +3,7 @@ import io.lettuce.core.dynamic.annotation.Param; import modernfarmer.server.farmususer.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -11,20 +12,22 @@ @Repository public interface UserRepository extends JpaRepository { - - Optional findByUsernumber(String usernumber); @Query("SELECT a.role FROM User AS a WHERE a.id = :userId") String findUserRole(@Param("userId") Long userId); - @Query("update User as u set u = :nickName where u.id = :userId") + @Modifying + @Query("update User as u set u.nickname = :nickName where u.id = :userId") void updateUserNickname(@Param("nickName") String nickName, @Param("userId") Long userId); - - @Query("delete from User as u where u.id = : userId") + @Modifying + @Query("delete from User as u where u.id = :userId") void deleteUser(@Param("userId") Long userId); @Query("select u.profileImage from User as u where u.id = :userId") String selectUserProfileImage(@Param("userId") Long userId); + @Modifying + @Query("update User as u set u.profileImage = :profileImage where u.id = :userId") + void emitUserProfileImage(@Param("userId") Long userId, @Param("profileImage") String profileImage); diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java index 1b73a5c..9978c2a 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java @@ -28,7 +28,7 @@ public class AuthService{ public UserRepository userRepository; private final WebClient webClient; - private boolean early = false; + @Autowired public AuthService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate) { @@ -41,6 +41,7 @@ public AuthService(WebClient webClient, UserRepository userRepository, JwtTokenP public TokenResponseDto googleLogin(String accessToken) { User user; + boolean early = false; Mono userInfoMono = getUserGoogleInfo(accessToken); GoogleUserResponseDto userInfo = userInfoMono.block(); @@ -88,6 +89,7 @@ public TokenResponseDto googleLogin(String accessToken) { public TokenResponseDto kakaoLogin(String accessToken) { User user; + boolean early = false; Mono userInfoMono = getUserKakaoInfo(accessToken); KakaoUserResponseDto userInfo = userInfoMono.block(); diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java index 0def733..6cec29c 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/UserService.java @@ -1,8 +1,7 @@ package modernfarmer.server.farmususer.user.service; import lombok.extern.slf4j.Slf4j; -import modernfarmer.server.farmususer.global.config.s3.AmazonS3ResourceStorage; -import modernfarmer.server.farmususer.global.config.s3.FileDetail; +import modernfarmer.server.farmususer.global.config.s3.S3Uploader; import modernfarmer.server.farmususer.global.exception.notfound.NotFoundRefreshTokenException; import modernfarmer.server.farmususer.user.dto.response.ProfileImageResponseDto; import modernfarmer.server.farmususer.user.dto.response.ResponseDto; @@ -10,12 +9,14 @@ import modernfarmer.server.farmususer.user.repository.UserRepository; import modernfarmer.server.farmususer.user.util.JwtTokenProvider; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.reactive.function.client.WebClient; +import java.io.IOException; + @Slf4j @Service @@ -24,26 +25,27 @@ public class UserService { public JwtTokenProvider jwtTokenProvider; public RedisTemplate redisTemplate; public UserRepository userRepository; - private final AmazonS3ResourceStorage amazonS3ResourceStorage; + public final S3Uploader s3Uploader; + @Autowired - public UserService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate, AmazonS3ResourceStorage amazonS3ResourceStoraget) { + public UserService(UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate redisTemplate, + S3Uploader s3Uploader + ) { this.userRepository = userRepository; this.jwtTokenProvider = jwtTokenProvider; this.redisTemplate = redisTemplate; - this.amazonS3ResourceStorage = amazonS3ResourceStoraget; - } - + this.s3Uploader = s3Uploader; - public ResponseDto produceProfileImage(Long userId, MultipartFile multipartFile){ - - - FileDetail fileDetail = FileDetail.multipartOf(multipartFile); - amazonS3ResourceStorage.store(fileDetail.getPath(), multipartFile); + } + @Transactional + public ResponseDto emitProfileImage(Long userId, MultipartFile multipartFile) throws IOException { - log.info(String.valueOf(fileDetail)); + String imageUrl = s3Uploader.uploadFiles(multipartFile, "userprofileimage"); + log.info(imageUrl); + userRepository.emitUserProfileImage(userId, imageUrl); ResponseDto responseDto = ResponseDto .builder() @@ -70,12 +72,7 @@ public ProfileImageResponseDto selectProfileImage(Long userId){ return profileImageResponseDto; } - - - - - - + @Transactional public ResponseDto deleteUser(Long userId){ userRepository.deleteUser(userId); @@ -90,7 +87,8 @@ public ResponseDto deleteUser(Long userId){ } - public ResponseDto produceNickname(Long userId, String nickName){ + @Transactional + public ResponseDto emitNickname(Long userId, String nickName){ userRepository.updateUserNickname(nickName, userId);