From a69772000d9f30c6055d3db584331654fc06e325 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Wed, 16 Oct 2024 22:40:50 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20[#45]=20=EA=B0=9C=EB=B3=84=20?= =?UTF-8?q?=EC=83=81=ED=92=88=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EB=A7=81=ED=81=AC=20=ED=8F=AC=ED=95=A8?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 5 +++-- .../com/helpmeCookies/product/entity/ProductImage.java | 5 +++++ .../helpmeCookies/product/service/ProductImageService.java | 7 ++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/helpmeCookies/product/controller/ProductController.java b/src/main/java/com/helpmeCookies/product/controller/ProductController.java index c655ee7..379b545 100644 --- a/src/main/java/com/helpmeCookies/product/controller/ProductController.java +++ b/src/main/java/com/helpmeCookies/product/controller/ProductController.java @@ -30,7 +30,7 @@ public ResponseEntity saveProduct(@RequestBody ProductRequest productReque } @PostMapping("/{productId}/images") - public ResponseEntity uploadImages(@PathVariable("productId") Long productId, List files) throws IOException { + public ResponseEntity uploadImages(@PathVariable("productId") Long productId, List files) { List responses = productImageService.uploadMultiFiles(productId,files); return ResponseEntity.ok(new ProductImageResponse(responses.stream().map(FileUploadResponse::photoUrl).toList())); } @@ -38,7 +38,8 @@ public ResponseEntity uploadImages(@PathVariable("productI @GetMapping("/{productId}") public ResponseEntity getProductInfo(@PathVariable("productId") Long productId) { Product product = productService.find(productId); - return ResponseEntity.ok(ProductResponse.from(product)); + List urls = productImageService.getImages(productId); + return ResponseEntity.ok(ProductResponse.from(product,urls)); } @PutMapping("/{productId}") diff --git a/src/main/java/com/helpmeCookies/product/entity/ProductImage.java b/src/main/java/com/helpmeCookies/product/entity/ProductImage.java index 0a23f4f..ed6cf36 100644 --- a/src/main/java/com/helpmeCookies/product/entity/ProductImage.java +++ b/src/main/java/com/helpmeCookies/product/entity/ProductImage.java @@ -25,4 +25,9 @@ public ProductImage(String photoUrl, Long productId, String uuid) { this.productId = productId; this.uuid = uuid; } + + public String getPhotoUrl() { + return photoUrl; + } } + diff --git a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java index 25215c5..10bfe36 100644 --- a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java +++ b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java @@ -2,6 +2,7 @@ import com.helpmeCookies.global.utils.AwsS3FileUtils; import com.helpmeCookies.product.dto.FileUploadResponse; +import com.helpmeCookies.product.entity.ProductImage; import com.helpmeCookies.product.repository.ProductImageRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -18,7 +19,7 @@ public class ProductImageService { private final ProductImageRepository productImageRepository; @Transactional - public List uploadMultiFiles(Long productId, List files) throws IOException { + public List uploadMultiFiles(Long productId, List files) { List uploadResponses = awsS3FileUtils.uploadMultiImages(files); uploadResponses.forEach(response -> productImageRepository.save(response.toEntity(productId))); @@ -32,4 +33,8 @@ public void editImages(Long productId, List files) throws IOExcep productImageRepository.deleteAllByProductId(productId); uploadMultiFiles(productId, files); } + + public List getImages(Long productId) { + return productImageRepository.findAllByProductId(productId).stream().map(ProductImage::getPhotoUrl).toList(); + } } From f735928ebc148521c7664be39f71c8077299885c Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 02:34:55 +0900 Subject: [PATCH 02/13] =?UTF-8?q?refactor:=20[#61]=20s3=EC=97=90=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=EC=8B=9C=20=ED=95=98=EB=82=98?= =?UTF-8?q?=EC=94=A9=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/utils/AwsS3FileUtils.java | 32 ++++++++----------- ...leUploadResponse.java => ImageUpload.java} | 2 +- .../product/service/ProductImageService.java | 23 ++++++++----- 3 files changed, 29 insertions(+), 28 deletions(-) rename src/main/java/com/helpmeCookies/product/dto/{FileUploadResponse.java => ImageUpload.java} (91%) diff --git a/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java b/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java index afc9043..ed16def 100644 --- a/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java +++ b/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java @@ -4,7 +4,7 @@ import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; -import com.helpmeCookies.product.dto.FileUploadResponse; +import com.helpmeCookies.product.dto.ImageUpload; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; @@ -14,7 +14,6 @@ import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; @@ -28,26 +27,21 @@ public class AwsS3FileUtils { private String bucket; //다중파일 업로드후 url 반환 - public List uploadMultiImages(List multipartFiles) { - List fileList = new ArrayList<>(); + public ImageUpload uploadMultiImages(MultipartFile multipartFile) { - multipartFiles.forEach(file -> { - String fileName = createFileName(file.getOriginalFilename()); //파일 이름 난수화 - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentLength(file.getSize()); - objectMetadata.setContentType(file.getContentType()); + String fileName = createFileName(multipartFile.getOriginalFilename()); //파일 이름 난수화 + ObjectMetadata objectMetadata = new ObjectMetadata(); + objectMetadata.setContentLength(multipartFile.getSize()); + objectMetadata.setContentType(multipartFile.getContentType()); - try (InputStream inputStream = file.getInputStream()) { - amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) - .withCannedAcl(CannedAccessControlList.PublicRead)); - } catch (IOException e) { - throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드 실패" + fileName); - } - - fileList.add(new FileUploadResponse(amazonS3.getUrl(bucket,fileName).toString(),fileName)); - }); + try (InputStream inputStream = multipartFile.getInputStream()) { + amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) + .withCannedAcl(CannedAccessControlList.PublicRead)); + } catch (IOException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드 실패" + fileName); + } - return fileList; + return new ImageUpload(amazonS3.getUrl(bucket,fileName).toString(),fileName); } public String createFileName(String fileName) { diff --git a/src/main/java/com/helpmeCookies/product/dto/FileUploadResponse.java b/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java similarity index 91% rename from src/main/java/com/helpmeCookies/product/dto/FileUploadResponse.java rename to src/main/java/com/helpmeCookies/product/dto/ImageUpload.java index ad03aff..83f29f9 100644 --- a/src/main/java/com/helpmeCookies/product/dto/FileUploadResponse.java +++ b/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java @@ -2,7 +2,7 @@ import com.helpmeCookies.product.entity.ProductImage; -public record FileUploadResponse( +public record ImageUpload( String photoUrl, String uuid ) { diff --git a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java index 10bfe36..d537a23 100644 --- a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java +++ b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java @@ -1,15 +1,16 @@ package com.helpmeCookies.product.service; import com.helpmeCookies.global.utils.AwsS3FileUtils; -import com.helpmeCookies.product.dto.FileUploadResponse; +import com.helpmeCookies.product.dto.ImageUpload; +import com.helpmeCookies.product.dto.ProductRequest; import com.helpmeCookies.product.entity.ProductImage; import com.helpmeCookies.product.repository.ProductImageRepository; +import java.util.ArrayList; import lombok.RequiredArgsConstructor; 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; @Service @@ -19,15 +20,21 @@ public class ProductImageService { private final ProductImageRepository productImageRepository; @Transactional - public List uploadMultiFiles(Long productId, List files) { - List uploadResponses = awsS3FileUtils.uploadMultiImages(files); - uploadResponses.forEach(response -> - productImageRepository.save(response.toEntity(productId))); - return uploadResponses; + public List uploadMultiFiles(Long productId, List files) { + List imageUploads = new ArrayList<>(); + for (MultipartFile multipartFile:files) { + imageUploads.add(awsS3FileUtils.uploadMultiImages(multipartFile)); + } + return imageUploads; } @Transactional - public void editImages(Long productId, List files) throws IOException { + public void saveImages(Long productId,ProductRequest productRequest) { + productRequest.productImages().forEach(image -> productImageRepository.save(image.toEntity(productId))); + } + + @Transactional + public void editImages(Long productId, List files) { //우선은 전부 삭제하고 다시 업로드 //추후에 개선 예정 productImageRepository.deleteAllByProductId(productId); From a0da26fd42f09cd6bcaf62eace4b92f5ebc35b45 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 02:46:42 +0900 Subject: [PATCH 03/13] =?UTF-8?q?refactor:=20[#61]=20=EC=82=AC=EC=A7=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EC=82=AC=EC=A7=84=20=EC=88=98=EC=A0=95=EC=8B=9C=20s3=EC=97=90?= =?UTF-8?q?=20=EB=A8=BC=EC=A0=80=20=EC=97=85=EB=A1=9C=EB=93=9C=20=ED=9B=84?= =?UTF-8?q?=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 18 ++++++++++-------- .../product/service/ProductImageService.java | 9 ++++----- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/helpmeCookies/product/controller/ProductController.java b/src/main/java/com/helpmeCookies/product/controller/ProductController.java index 379b545..333d8d4 100644 --- a/src/main/java/com/helpmeCookies/product/controller/ProductController.java +++ b/src/main/java/com/helpmeCookies/product/controller/ProductController.java @@ -1,6 +1,6 @@ package com.helpmeCookies.product.controller; -import com.helpmeCookies.product.dto.FileUploadResponse; +import com.helpmeCookies.product.dto.ImageUpload; import com.helpmeCookies.product.dto.ProductImageResponse; import com.helpmeCookies.product.dto.ProductRequest; import com.helpmeCookies.product.dto.ProductResponse; @@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; import java.util.List; @RestController @@ -25,14 +24,15 @@ public class ProductController { @PostMapping public ResponseEntity saveProduct(@RequestBody ProductRequest productRequest) { - productService.save(productRequest); + Product product = productService.save(productRequest); + productImageService.saveImages(product.getId(),productRequest.productImages()); return ResponseEntity.ok().build(); } - @PostMapping("/{productId}/images") - public ResponseEntity uploadImages(@PathVariable("productId") Long productId, List files) { - List responses = productImageService.uploadMultiFiles(productId,files); - return ResponseEntity.ok(new ProductImageResponse(responses.stream().map(FileUploadResponse::photoUrl).toList())); + @PostMapping("/images") + public ResponseEntity uploadImages(List files) { + List responses = productImageService.uploadMultiFiles(files); + return ResponseEntity.ok(new ProductImageResponse(responses.stream().map(ImageUpload::photoUrl).toList())); } @GetMapping("/{productId}") @@ -50,8 +50,10 @@ public ResponseEntity editProductInfo(@PathVariable("productId") Long prod } @PutMapping("/{productId}/images") - public ResponseEntity editImages(@PathVariable("productId") Long productId, List files) throws IOException { + public ResponseEntity editImages(@PathVariable("productId") Long productId, List files) { productImageService.editImages(productId, files); + List images = productImageService.uploadMultiFiles(files); + productImageService.saveImages(productId,images); return ResponseEntity.ok().build(); } diff --git a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java index d537a23..2553acd 100644 --- a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java +++ b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java @@ -2,7 +2,6 @@ import com.helpmeCookies.global.utils.AwsS3FileUtils; import com.helpmeCookies.product.dto.ImageUpload; -import com.helpmeCookies.product.dto.ProductRequest; import com.helpmeCookies.product.entity.ProductImage; import com.helpmeCookies.product.repository.ProductImageRepository; import java.util.ArrayList; @@ -20,7 +19,7 @@ public class ProductImageService { private final ProductImageRepository productImageRepository; @Transactional - public List uploadMultiFiles(Long productId, List files) { + public List uploadMultiFiles(List files) { List imageUploads = new ArrayList<>(); for (MultipartFile multipartFile:files) { imageUploads.add(awsS3FileUtils.uploadMultiImages(multipartFile)); @@ -29,16 +28,16 @@ public List uploadMultiFiles(Long productId, List fi } @Transactional - public void saveImages(Long productId,ProductRequest productRequest) { - productRequest.productImages().forEach(image -> productImageRepository.save(image.toEntity(productId))); + public void saveImages(Long productId,List files) { + files.forEach(image -> productImageRepository.save(image.toEntity(productId))); } @Transactional public void editImages(Long productId, List files) { //우선은 전부 삭제하고 다시 업로드 //추후에 개선 예정 + //TODO s3서버에서 기존 사진들을 제거하는 기능 productImageRepository.deleteAllByProductId(productId); - uploadMultiFiles(productId, files); } public List getImages(Long productId) { From ec89e148d4efc122fc9c28100813d40637362740 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 15:27:20 +0900 Subject: [PATCH 04/13] =?UTF-8?q?fix=20:=20[#37]=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EC=88=98=EC=A0=95=20=EC=8B=9C=20artistInf?= =?UTF-8?q?o=20=EA=B4=80=EB=A0=A8=20=EC=BD=94=EB=93=9C=20=EB=B3=B4?= =?UTF-8?q?=EC=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/dto/ProductRequest.java | 4 +++- .../product/dto/ProductResponse.java | 22 ++----------------- .../product/service/ProductService.java | 13 +++++++---- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java b/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java index e1dfa2d..a187b82 100644 --- a/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java +++ b/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java @@ -15,7 +15,9 @@ public record ProductRequest( String description, String preferredLocation, List hashTags, - Long artistInfo + Long artistInfoId, + List productImages + ) { public Product toEntity(ArtistInfo artistInfo) { return Product.builder() diff --git a/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java b/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java index 36b6e95..80c78bf 100644 --- a/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java +++ b/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java @@ -16,26 +16,10 @@ public record ProductResponse( List hashTags, ArtistInfo artistInfo ) { - public static class ArtistInfo { - private final Long artistId; - private final String name; - - public ArtistInfo(Long artistId, String name) { - this.artistId = artistId; - this.name = name; - } - - public Long getArtistId() { - return artistId; - } - - public String getName() { - return name; - } + public record ArtistInfo(Long artistId, String artistName) { } public static ProductResponse from(Product product) { - //TODO artistInfo 코드 개발 이후 수정 예정 return new ProductResponse( product.getId(), product.getName(), @@ -45,9 +29,7 @@ public static ProductResponse from(Product product) { product.getDescription(), product.getPreferredLocation(), product.getHashTags(), - new ArtistInfo( - 1L, "임시" - ) + new ArtistInfo(product.getArtistInfo().getId(),product.getArtistInfo().getNickname()) ); } } diff --git a/src/main/java/com/helpmeCookies/product/service/ProductService.java b/src/main/java/com/helpmeCookies/product/service/ProductService.java index c9b22d4..b288c53 100644 --- a/src/main/java/com/helpmeCookies/product/service/ProductService.java +++ b/src/main/java/com/helpmeCookies/product/service/ProductService.java @@ -5,6 +5,8 @@ import com.helpmeCookies.product.entity.Product; import com.helpmeCookies.product.repository.ProductImageRepository; import com.helpmeCookies.product.repository.ProductRepository; +import com.helpmeCookies.user.entity.ArtistInfo; +import com.helpmeCookies.user.repository.ArtistInfoRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -14,10 +16,12 @@ public class ProductService { private final ProductRepository productRepository; private final ProductImageRepository productImageRepository; + private final ArtistInfoRepository artistInfoRepository; public Product save(ProductRequest productSaveRequest) { - //TODO ArtistInfo 코드 병합시 수정 예정 - Product product = productSaveRequest.toEntity(null); + ArtistInfo artistInfo = artistInfoRepository.findById(productSaveRequest.artistInfoId()) + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 작가 정보입니다.")); + Product product = productSaveRequest.toEntity(artistInfo); productRepository.save(product); return product; } @@ -29,7 +33,8 @@ public Product find(Long productId) { @Transactional public void edit(Long productId, ProductRequest productRequest) { Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 id입니다")); - //TODO ArtistInfo 코드 병합시 수정 예정 + ArtistInfo artistInfo = artistInfoRepository.findById(productRequest.artistInfoId()) + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 작가 정보입니다.")); product.update( productRequest.name(), Category.fromString(productRequest.category()), @@ -38,7 +43,7 @@ public void edit(Long productId, ProductRequest productRequest) { productRequest.description(), productRequest.preferredLocation(), productRequest.hashTags(), - null); + artistInfo); } public void delete(Long productId) { From dcd12d180771b67d661da21c461144886161c0ba Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 15:34:13 +0900 Subject: [PATCH 05/13] =?UTF-8?q?feat=20:=20[#45]=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=20=EC=A1=B0=ED=9A=8C=EC=8B=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20url=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/helpmeCookies/product/dto/ProductResponse.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java b/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java index 80c78bf..4e927e6 100644 --- a/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java +++ b/src/main/java/com/helpmeCookies/product/dto/ProductResponse.java @@ -14,12 +14,13 @@ public record ProductResponse( String description, String preferredLocation, List hashTags, - ArtistInfo artistInfo + ArtistInfo artistInfo, + List imageUrls ) { public record ArtistInfo(Long artistId, String artistName) { } - public static ProductResponse from(Product product) { + public static ProductResponse from(Product product, List urls) { return new ProductResponse( product.getId(), product.getName(), @@ -29,7 +30,8 @@ public static ProductResponse from(Product product) { product.getDescription(), product.getPreferredLocation(), product.getHashTags(), - new ArtistInfo(product.getArtistInfo().getId(),product.getArtistInfo().getNickname()) + new ArtistInfo(product.getArtistInfo().getId(),product.getArtistInfo().getNickname()), + urls ); } } From 84381f1cbd3c0c76f40027b124b6e869f0c23b66 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 16:10:23 +0900 Subject: [PATCH 06/13] =?UTF-8?q?feat=20:=20[#61]=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=A0=80=EC=9E=A5=EC=8B=9C=20url=EB=A7=8C?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=20uuid=EB=A1=9C=20url=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=96=88=EA=B8=B0=EC=97=90=20uuid=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=ED=95=A0=20=ED=95=84=EC=9A=94=20=EC=97=86=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/helpmeCookies/global/utils/AwsS3FileUtils.java | 2 +- src/main/java/com/helpmeCookies/product/dto/ImageUpload.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java b/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java index ed16def..f6d76d3 100644 --- a/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java +++ b/src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.java @@ -41,7 +41,7 @@ public ImageUpload uploadMultiImages(MultipartFile multipartFile) { throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드 실패" + fileName); } - return new ImageUpload(amazonS3.getUrl(bucket,fileName).toString(),fileName); + return new ImageUpload(amazonS3.getUrl(bucket,fileName).toString()); } public String createFileName(String fileName) { diff --git a/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java b/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java index 83f29f9..fa847b3 100644 --- a/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java +++ b/src/main/java/com/helpmeCookies/product/dto/ImageUpload.java @@ -3,14 +3,12 @@ import com.helpmeCookies.product.entity.ProductImage; public record ImageUpload( - String photoUrl, - String uuid + String photoUrl ) { public ProductImage toEntity(Long productId) { return ProductImage.builder() .productId(productId) .photoUrl(photoUrl) - .uuid(uuid) .build(); } } From 9aa22810498ca5513491738573a51782a07e832f Mon Sep 17 00:00:00 2001 From: bokyeong Date: Tue, 22 Oct 2024 16:11:50 +0900 Subject: [PATCH 07/13] =?UTF-8?q?refactor=20:=20[#61]=20image=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=20=EB=B3=80=ED=99=94=EB=A1=9C=20DTO=20=EB=B3=80?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helpmeCookies/product/controller/ProductController.java | 5 +++-- .../java/com/helpmeCookies/product/dto/ProductRequest.java | 2 +- .../helpmeCookies/product/service/ProductImageService.java | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/helpmeCookies/product/controller/ProductController.java b/src/main/java/com/helpmeCookies/product/controller/ProductController.java index 333d8d4..e16082e 100644 --- a/src/main/java/com/helpmeCookies/product/controller/ProductController.java +++ b/src/main/java/com/helpmeCookies/product/controller/ProductController.java @@ -25,7 +25,7 @@ public class ProductController { @PostMapping public ResponseEntity saveProduct(@RequestBody ProductRequest productRequest) { Product product = productService.save(productRequest); - productImageService.saveImages(product.getId(),productRequest.productImages()); + productImageService.saveImages(product.getId(),productRequest.imageUrls()); return ResponseEntity.ok().build(); } @@ -52,7 +52,8 @@ public ResponseEntity editProductInfo(@PathVariable("productId") Long prod @PutMapping("/{productId}/images") public ResponseEntity editImages(@PathVariable("productId") Long productId, List files) { productImageService.editImages(productId, files); - List images = productImageService.uploadMultiFiles(files); + List images = productImageService.uploadMultiFiles(files).stream() + .map(ImageUpload::photoUrl).toList(); productImageService.saveImages(productId,images); return ResponseEntity.ok().build(); } diff --git a/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java b/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java index a187b82..b0f73d5 100644 --- a/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java +++ b/src/main/java/com/helpmeCookies/product/dto/ProductRequest.java @@ -16,7 +16,7 @@ public record ProductRequest( String preferredLocation, List hashTags, Long artistInfoId, - List productImages + List imageUrls ) { public Product toEntity(ArtistInfo artistInfo) { diff --git a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java index 2553acd..42948a1 100644 --- a/src/main/java/com/helpmeCookies/product/service/ProductImageService.java +++ b/src/main/java/com/helpmeCookies/product/service/ProductImageService.java @@ -28,7 +28,9 @@ public List uploadMultiFiles(List files) { } @Transactional - public void saveImages(Long productId,List files) { + public void saveImages(Long productId,List urls) { + //DTO 변환 + List files = urls.stream().map(ImageUpload::new).toList(); files.forEach(image -> productImageRepository.save(image.toEntity(productId))); } From 369c5633546aa29829b2ede000dfca650a023188 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Wed, 23 Oct 2024 23:45:50 +0900 Subject: [PATCH 08/13] =?UTF-8?q?feat=20:=20[#62]=20=EA=B0=90=EC=83=81?= =?UTF-8?q?=ED=8F=89=20=EC=97=94=ED=8B=B0=ED=8B=B0=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{product => review}/entity/Review.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) rename src/main/java/com/helpmeCookies/{product => review}/entity/Review.java (67%) diff --git a/src/main/java/com/helpmeCookies/product/entity/Review.java b/src/main/java/com/helpmeCookies/review/entity/Review.java similarity index 67% rename from src/main/java/com/helpmeCookies/product/entity/Review.java rename to src/main/java/com/helpmeCookies/review/entity/Review.java index 854fdbb..b3035d3 100644 --- a/src/main/java/com/helpmeCookies/product/entity/Review.java +++ b/src/main/java/com/helpmeCookies/review/entity/Review.java @@ -1,5 +1,6 @@ -package com.helpmeCookies.product.entity; +package com.helpmeCookies.review.entity; +import com.helpmeCookies.product.entity.Product; import com.helpmeCookies.user.entity.User; import jakarta.persistence.Column; @@ -9,6 +10,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; +import lombok.Builder; @Entity public class Review { @@ -27,4 +29,14 @@ public class Review { @ManyToOne @JoinColumn(name = "product_id", nullable = false) private Product product; + + @Builder + public Review(Long id, String content, User writer, Product product) { + this.id = id; + this.content = content; + this.writer = writer; + this.product = product; + } + + public Review() {} } From 3e0fc6b7f08c4f3c3a4596f773d77234b3c9bbdf Mon Sep 17 00:00:00 2001 From: bokyeong Date: Thu, 24 Oct 2024 01:27:38 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat=20:=20[#62]=20=EA=B0=90=EC=83=81?= =?UTF-8?q?=ED=8F=89=20=EC=9E=91=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=EA=B0=80=20=EC=9E=91=ED=92=88=EC=9D=84=20?= =?UTF-8?q?=EA=B5=AC=EB=A7=A4=ED=95=98=EC=98=80=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8X?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 30 +++++++++++++++++++ .../review/dto/ReviewRequest.java | 15 ++++++++++ .../review/repository/ReviewRepository.java | 9 ++++++ .../review/service/ReviewService.java | 25 ++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/main/java/com/helpmeCookies/review/controller/ReviewController.java create mode 100644 src/main/java/com/helpmeCookies/review/dto/ReviewRequest.java create mode 100644 src/main/java/com/helpmeCookies/review/repository/ReviewRepository.java create mode 100644 src/main/java/com/helpmeCookies/review/service/ReviewService.java diff --git a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java new file mode 100644 index 0000000..d4c944a --- /dev/null +++ b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java @@ -0,0 +1,30 @@ +package com.helpmeCookies.review.controller; + +import com.helpmeCookies.review.dto.ReviewRequest; +import com.helpmeCookies.review.service.ReviewService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/v1/reviews") +@RequiredArgsConstructor +public class ReviewController { + + private final ReviewService reviewService; + + //TODO 감상평 등록 + //TODO 감상평 수정 + //TODO 감상평 삭제 + //TODO 해당 상품의 감상평 조회 + //TODO 내 감상평 조회 + + @PostMapping("/{productId}") + public ResponseEntity postReview(@RequestBody ReviewRequest request, Long productId) { + reviewService.saveReview(request,productId); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/com/helpmeCookies/review/dto/ReviewRequest.java b/src/main/java/com/helpmeCookies/review/dto/ReviewRequest.java new file mode 100644 index 0000000..17a2600 --- /dev/null +++ b/src/main/java/com/helpmeCookies/review/dto/ReviewRequest.java @@ -0,0 +1,15 @@ +package com.helpmeCookies.review.dto; + +import com.helpmeCookies.product.entity.Product; +import com.helpmeCookies.review.entity.Review; +import com.helpmeCookies.user.entity.User; + +public record ReviewRequest(Long writerId, String content) { + public Review toEntity(User writer, Product product) { + return Review.builder() + .content(content) + .writer(writer) + .product(product) + .build(); + } +} diff --git a/src/main/java/com/helpmeCookies/review/repository/ReviewRepository.java b/src/main/java/com/helpmeCookies/review/repository/ReviewRepository.java new file mode 100644 index 0000000..a64c2ff --- /dev/null +++ b/src/main/java/com/helpmeCookies/review/repository/ReviewRepository.java @@ -0,0 +1,9 @@ +package com.helpmeCookies.review.repository; + +import com.helpmeCookies.review.entity.Review; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReviewRepository extends JpaRepository { +} diff --git a/src/main/java/com/helpmeCookies/review/service/ReviewService.java b/src/main/java/com/helpmeCookies/review/service/ReviewService.java new file mode 100644 index 0000000..885e461 --- /dev/null +++ b/src/main/java/com/helpmeCookies/review/service/ReviewService.java @@ -0,0 +1,25 @@ +package com.helpmeCookies.review.service; + +import com.helpmeCookies.product.entity.Product; +import com.helpmeCookies.product.repository.ProductRepository; +import com.helpmeCookies.review.dto.ReviewRequest; +import com.helpmeCookies.review.repository.ReviewRepository; +import com.helpmeCookies.user.entity.User; +import com.helpmeCookies.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ReviewService { + private final ReviewRepository reviewRepository; + private final UserRepository userRepository; + private final ProductRepository productRepository; + + public void saveReview(ReviewRequest request, Long productId) { + User writer = userRepository.findById(request.writerId()).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 writerID입니다.")); + Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 productID입니다.")); + + reviewRepository.save(request.toEntity(writer,product)); + } + } From 2b89c6b05a37d14a566d650b95432f9c50665b20 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Thu, 24 Oct 2024 17:20:59 +0900 Subject: [PATCH 10/13] =?UTF-8?q?fix=20:=20[#62]=20=EA=B0=90=EC=83=81?= =?UTF-8?q?=ED=8F=89=20=EB=93=B1=EB=A1=9D=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/helpmeCookies/review/controller/ReviewController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java index d4c944a..9dec78c 100644 --- a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java +++ b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java @@ -4,6 +4,7 @@ import com.helpmeCookies.review.service.ReviewService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,14 +17,13 @@ public class ReviewController { private final ReviewService reviewService; - //TODO 감상평 등록 //TODO 감상평 수정 //TODO 감상평 삭제 //TODO 해당 상품의 감상평 조회 //TODO 내 감상평 조회 @PostMapping("/{productId}") - public ResponseEntity postReview(@RequestBody ReviewRequest request, Long productId) { + public ResponseEntity postReview(@RequestBody ReviewRequest request, @PathVariable Long productId) { reviewService.saveReview(request,productId); return ResponseEntity.ok().build(); } From 6aa7250463187d97e34f23d1e9792b824c52c98f Mon Sep 17 00:00:00 2001 From: bokyeong Date: Thu, 24 Oct 2024 17:32:36 +0900 Subject: [PATCH 11/13] =?UTF-8?q?fix=20:=20[#62]=20=EA=B0=90=EC=83=81?= =?UTF-8?q?=ED=8F=89=20=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EA=B0=90?= =?UTF-8?q?=EC=83=81=ED=8F=89=EC=9D=80=20content=EB=A7=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EA=B0=80=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 10 ++++++++-- .../java/com/helpmeCookies/review/entity/Review.java | 4 ++++ .../helpmeCookies/review/service/ReviewService.java | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java index 9dec78c..d225e1f 100644 --- a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java +++ b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java @@ -6,6 +6,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -17,14 +18,19 @@ public class ReviewController { private final ReviewService reviewService; - //TODO 감상평 수정 //TODO 감상평 삭제 //TODO 해당 상품의 감상평 조회 //TODO 내 감상평 조회 @PostMapping("/{productId}") public ResponseEntity postReview(@RequestBody ReviewRequest request, @PathVariable Long productId) { - reviewService.saveReview(request,productId); + reviewService.saveReview(request, productId); + return ResponseEntity.ok().build(); + } + + @PutMapping("/{productId}/{reviewId}") + public ResponseEntity editReview(@RequestBody ReviewRequest request, @PathVariable Long productId, @PathVariable Long reviewId) { + reviewService.editReview(request, productId, reviewId); return ResponseEntity.ok().build(); } } diff --git a/src/main/java/com/helpmeCookies/review/entity/Review.java b/src/main/java/com/helpmeCookies/review/entity/Review.java index b3035d3..da4f5d8 100644 --- a/src/main/java/com/helpmeCookies/review/entity/Review.java +++ b/src/main/java/com/helpmeCookies/review/entity/Review.java @@ -39,4 +39,8 @@ public Review(Long id, String content, User writer, Product product) { } public Review() {} + + public void updateContent(String content) { + this.content = content; + } } diff --git a/src/main/java/com/helpmeCookies/review/service/ReviewService.java b/src/main/java/com/helpmeCookies/review/service/ReviewService.java index 885e461..4cf9c0e 100644 --- a/src/main/java/com/helpmeCookies/review/service/ReviewService.java +++ b/src/main/java/com/helpmeCookies/review/service/ReviewService.java @@ -3,6 +3,7 @@ import com.helpmeCookies.product.entity.Product; import com.helpmeCookies.product.repository.ProductRepository; import com.helpmeCookies.review.dto.ReviewRequest; +import com.helpmeCookies.review.entity.Review; import com.helpmeCookies.review.repository.ReviewRepository; import com.helpmeCookies.user.entity.User; import com.helpmeCookies.user.repository.UserRepository; @@ -22,4 +23,9 @@ public void saveReview(ReviewRequest request, Long productId) { reviewRepository.save(request.toEntity(writer,product)); } + + public void editReview(ReviewRequest request, Long productId, Long reviewId) { + Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 reviewId 입니다.")); + review.updateContent(request.content()); + } } From f38dd448df949fe6b72be717595b884b7e546566 Mon Sep 17 00:00:00 2001 From: bokyeong Date: Fri, 1 Nov 2024 18:19:44 +0900 Subject: [PATCH 12/13] =?UTF-8?q?fix=20:=20[#67]=20api=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/helpmeCookies/product/controller/ProductController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/helpmeCookies/product/controller/ProductController.java b/src/main/java/com/helpmeCookies/product/controller/ProductController.java index e16082e..81afa0a 100644 --- a/src/main/java/com/helpmeCookies/product/controller/ProductController.java +++ b/src/main/java/com/helpmeCookies/product/controller/ProductController.java @@ -15,7 +15,7 @@ import java.util.List; @RestController -@RequestMapping("/api/v1/products") +@RequestMapping("/v1/products") @RequiredArgsConstructor public class ProductController { From b3ed71bd924e43f050a9a9669ec4963a062c1a2d Mon Sep 17 00:00:00 2001 From: bokyeong Date: Fri, 1 Nov 2024 18:21:35 +0900 Subject: [PATCH 13/13] =?UTF-8?q?feat=20:=20[#62]=20=EA=B0=90=EC=83=81?= =?UTF-8?q?=ED=8F=89=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 9 +++++++++ .../helpmeCookies/review/dto/ReviewResponse.java | 11 +++++++++++ .../com/helpmeCookies/review/entity/Review.java | 16 ++++++++++++++++ .../review/service/ReviewService.java | 4 ++++ 4 files changed, 40 insertions(+) create mode 100644 src/main/java/com/helpmeCookies/review/dto/ReviewResponse.java diff --git a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java index d225e1f..9987947 100644 --- a/src/main/java/com/helpmeCookies/review/controller/ReviewController.java +++ b/src/main/java/com/helpmeCookies/review/controller/ReviewController.java @@ -1,9 +1,12 @@ package com.helpmeCookies.review.controller; import com.helpmeCookies.review.dto.ReviewRequest; +import com.helpmeCookies.review.dto.ReviewResponse; +import com.helpmeCookies.review.entity.Review; import com.helpmeCookies.review.service.ReviewService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -33,4 +36,10 @@ public ResponseEntity editReview(@RequestBody ReviewRequest request, @Path reviewService.editReview(request, productId, reviewId); return ResponseEntity.ok().build(); } + + @GetMapping("/{reviewId}") + public ResponseEntity getReview(@PathVariable Long reviewId) { + Review response = reviewService.getReview(reviewId); + return ResponseEntity.ok(ReviewResponse.fromEntity(response)); + } } diff --git a/src/main/java/com/helpmeCookies/review/dto/ReviewResponse.java b/src/main/java/com/helpmeCookies/review/dto/ReviewResponse.java new file mode 100644 index 0000000..044f00f --- /dev/null +++ b/src/main/java/com/helpmeCookies/review/dto/ReviewResponse.java @@ -0,0 +1,11 @@ +package com.helpmeCookies.review.dto; + +import com.helpmeCookies.product.entity.Product; +import com.helpmeCookies.review.entity.Review; +import com.helpmeCookies.user.entity.User; + +public record ReviewResponse(Long id, String content,User writer, Product product) { + public static ReviewResponse fromEntity(Review review) { + return new ReviewResponse(review.getId(), review.getContent(),review.getWriter(), review.getProduct()); + } +} diff --git a/src/main/java/com/helpmeCookies/review/entity/Review.java b/src/main/java/com/helpmeCookies/review/entity/Review.java index da4f5d8..1c9af76 100644 --- a/src/main/java/com/helpmeCookies/review/entity/Review.java +++ b/src/main/java/com/helpmeCookies/review/entity/Review.java @@ -43,4 +43,20 @@ public Review() {} public void updateContent(String content) { this.content = content; } + + public Long getId() { + return id; + } + + public String getContent() { + return content; + } + + public User getWriter() { + return writer; + } + + public Product getProduct() { + return product; + } } diff --git a/src/main/java/com/helpmeCookies/review/service/ReviewService.java b/src/main/java/com/helpmeCookies/review/service/ReviewService.java index 4cf9c0e..a7f873a 100644 --- a/src/main/java/com/helpmeCookies/review/service/ReviewService.java +++ b/src/main/java/com/helpmeCookies/review/service/ReviewService.java @@ -28,4 +28,8 @@ public void editReview(ReviewRequest request, Long productId, Long reviewId) { Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 reviewId 입니다.")); review.updateContent(request.content()); } + + public Review getReview(Long reviewId) { + return reviewRepository.findById(reviewId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 reviewId 입니다.")); + } }