From 6444b55cb32605b64eaf0cb62f5000095740f1eb Mon Sep 17 00:00:00 2001 From: Somin Yun Date: Mon, 4 Mar 2024 03:31:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#16=20product=20post,get=20api=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductController.java | 41 +++++++ .../dto/request/CreateProductRequest.java | 20 ++++ .../soullive_a/dto/response/AgeResponse.java | 9 ++ .../dto/response/BrandImageResponse.java | 7 ++ .../dto/response/CreateProductResponse.java | 62 ++++++++++ .../dto/response/GenderResponse.java | 9 ++ .../dto/response/ProductImageResponse.java | 7 ++ .../dto/response/RangeResponse.java | 7 ++ .../soullive_a/repository/AgeRepository.java | 12 ++ .../repository/BrandImageRepository.java | 12 ++ .../repository/GenderRepository.java | 12 ++ .../repository/ProductImageRepository.java | 12 ++ .../repository/ProductRepository.java | 9 ++ .../repository/RangeRepository.java | 12 ++ .../soullive_a/service/ProductService.java | 9 ++ .../service/impl/ProductServiceImpl.java | 107 ++++++++++++++++++ 16 files changed, 347 insertions(+) create mode 100644 src/main/java/com/backend/soullive_a/controller/ProductController.java create mode 100644 src/main/java/com/backend/soullive_a/dto/request/CreateProductRequest.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/AgeResponse.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/BrandImageResponse.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/CreateProductResponse.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/GenderResponse.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/ProductImageResponse.java create mode 100644 src/main/java/com/backend/soullive_a/dto/response/RangeResponse.java create mode 100644 src/main/java/com/backend/soullive_a/repository/AgeRepository.java create mode 100644 src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java create mode 100644 src/main/java/com/backend/soullive_a/repository/GenderRepository.java create mode 100644 src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java create mode 100644 src/main/java/com/backend/soullive_a/repository/ProductRepository.java create mode 100644 src/main/java/com/backend/soullive_a/repository/RangeRepository.java create mode 100644 src/main/java/com/backend/soullive_a/service/ProductService.java create mode 100644 src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java diff --git a/src/main/java/com/backend/soullive_a/controller/ProductController.java b/src/main/java/com/backend/soullive_a/controller/ProductController.java new file mode 100644 index 0000000..9319250 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/controller/ProductController.java @@ -0,0 +1,41 @@ +package com.backend.soullive_a.controller; + +import com.backend.soullive_a.dto.request.CreateProductRequest; +import com.backend.soullive_a.dto.response.CreateProductResponse; +import com.backend.soullive_a.exception.base.BaseResponse; +import com.backend.soullive_a.service.ProductService; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +@Slf4j +@RestController +@RequestMapping("/product") +@RequiredArgsConstructor +public class ProductController { + + private final ProductService productService; + + @PostMapping("") + public BaseResponse createProduct( + @RequestBody @Valid CreateProductRequest createProductRequest + ) { + return BaseResponse.builder() + .isSuccess(true) + .code(200) + .message("광고 상품을 등록했습니다") + .data(productService.createProduct(createProductRequest)) + .build(); + } + + @GetMapping("/{id}") + public BaseResponse getProduct(@PathVariable Long id) { + return BaseResponse.builder() + .isSuccess(true) + .code(200) + .message("productId = " + id + " 광고 상품을 불러옵니다") + .data(productService.getProduct(id)) + .build(); + } +} diff --git a/src/main/java/com/backend/soullive_a/dto/request/CreateProductRequest.java b/src/main/java/com/backend/soullive_a/dto/request/CreateProductRequest.java new file mode 100644 index 0000000..53ac691 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/request/CreateProductRequest.java @@ -0,0 +1,20 @@ +package com.backend.soullive_a.dto.request; + +import com.backend.soullive_a.constant.AgeType; +import com.backend.soullive_a.constant.GenderType; +import jakarta.validation.constraints.NotNull; + +import java.util.List; + +public record CreateProductRequest ( + @NotNull String company, + String brand, + @NotNull List brandImage, + @NotNull String product, + @NotNull String characteristic, + @NotNull List productImage, + @NotNull List gender, + @NotNull List age, + @NotNull List range +){ +} diff --git a/src/main/java/com/backend/soullive_a/dto/response/AgeResponse.java b/src/main/java/com/backend/soullive_a/dto/response/AgeResponse.java new file mode 100644 index 0000000..1e61cb4 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/AgeResponse.java @@ -0,0 +1,9 @@ +package com.backend.soullive_a.dto.response; + +import com.backend.soullive_a.constant.AgeType; + +public record AgeResponse( + Long ageId, + AgeType ageType +) { +} diff --git a/src/main/java/com/backend/soullive_a/dto/response/BrandImageResponse.java b/src/main/java/com/backend/soullive_a/dto/response/BrandImageResponse.java new file mode 100644 index 0000000..18837fe --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/BrandImageResponse.java @@ -0,0 +1,7 @@ +package com.backend.soullive_a.dto.response; + +public record BrandImageResponse ( + Long brandImageId, + String brandImage +){ +} diff --git a/src/main/java/com/backend/soullive_a/dto/response/CreateProductResponse.java b/src/main/java/com/backend/soullive_a/dto/response/CreateProductResponse.java new file mode 100644 index 0000000..ccb1e4a --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/CreateProductResponse.java @@ -0,0 +1,62 @@ +package com.backend.soullive_a.dto.response; + +import com.backend.soullive_a.entity.*; +import lombok.Builder; + +import java.util.List; +import java.util.stream.Collectors; + +@Builder +public record CreateProductResponse( + Long productId, + String company, + String brand, + List brandImages, + String product, + String characteristic, + List productImages, + List genders, + List ages, + List ranges +) { + public static CreateProductResponse fromProduct( + Product product, + List brandImage, + List productImage, + List genderType, + List ageType, + List rangeType) { + List brandImages = brandImage.stream() + .map(image -> new BrandImageResponse(image.getId(), image.getBrandImage())) + .collect(Collectors.toList()); + + List productImages = productImage.stream() + .map(image -> new ProductImageResponse(image.getId(), image.getProductImage())) + .collect(Collectors.toList()); + + List genders = genderType.stream() + .map(gender -> new GenderResponse(gender.getId(), gender.getGender())) + .collect(Collectors.toList()); + + List ages = ageType.stream() + .map(age -> new AgeResponse(age.getId(), age.getAge())) + .collect(Collectors.toList()); + + List ranges = rangeType.stream() + .map(range -> new RangeResponse(range.getId(), range.getRange())) + .collect(Collectors.toList()); + + return CreateProductResponse.builder() + .productId(product.getId()) + .company(product.getProduct()) + .brand(product.getBrand()) + .brandImages(brandImages) + .product(product.getProduct()) + .characteristic(product.getCharacteristic()) + .productImages(productImages) + .genders(genders) + .ages(ages) + .ranges(ranges) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/dto/response/GenderResponse.java b/src/main/java/com/backend/soullive_a/dto/response/GenderResponse.java new file mode 100644 index 0000000..528225d --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/GenderResponse.java @@ -0,0 +1,9 @@ +package com.backend.soullive_a.dto.response; + +import com.backend.soullive_a.constant.GenderType; + +public record GenderResponse ( + Long genderId, + GenderType gender +){ +} diff --git a/src/main/java/com/backend/soullive_a/dto/response/ProductImageResponse.java b/src/main/java/com/backend/soullive_a/dto/response/ProductImageResponse.java new file mode 100644 index 0000000..4f5887e --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/ProductImageResponse.java @@ -0,0 +1,7 @@ +package com.backend.soullive_a.dto.response; + +public record ProductImageResponse( + Long productImageId, + String productImage +) { +} diff --git a/src/main/java/com/backend/soullive_a/dto/response/RangeResponse.java b/src/main/java/com/backend/soullive_a/dto/response/RangeResponse.java new file mode 100644 index 0000000..033c7f2 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/dto/response/RangeResponse.java @@ -0,0 +1,7 @@ +package com.backend.soullive_a.dto.response; + +public record RangeResponse( + Long rangeId, + String range +) { +} diff --git a/src/main/java/com/backend/soullive_a/repository/AgeRepository.java b/src/main/java/com/backend/soullive_a/repository/AgeRepository.java new file mode 100644 index 0000000..1ed8b96 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/AgeRepository.java @@ -0,0 +1,12 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.Age; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface AgeRepository extends JpaRepository { + List findAllByProductId(Long productId); +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java b/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java new file mode 100644 index 0000000..1205189 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/BrandImageRepository.java @@ -0,0 +1,12 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.BrandImage; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BrandImageRepository extends JpaRepository { + List findAllByProductId(Long productId); +} diff --git a/src/main/java/com/backend/soullive_a/repository/GenderRepository.java b/src/main/java/com/backend/soullive_a/repository/GenderRepository.java new file mode 100644 index 0000000..b059aca --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/GenderRepository.java @@ -0,0 +1,12 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.Gender; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface GenderRepository extends JpaRepository { + List findAllByProductId(Long productId); +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java b/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java new file mode 100644 index 0000000..a61d09f --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/ProductImageRepository.java @@ -0,0 +1,12 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.ProductImage; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ProductImageRepository extends JpaRepository { + List findAllByProductId(Long productId); +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/repository/ProductRepository.java b/src/main/java/com/backend/soullive_a/repository/ProductRepository.java new file mode 100644 index 0000000..cf45a3c --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/ProductRepository.java @@ -0,0 +1,9 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.Product; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ProductRepository extends JpaRepository { +} diff --git a/src/main/java/com/backend/soullive_a/repository/RangeRepository.java b/src/main/java/com/backend/soullive_a/repository/RangeRepository.java new file mode 100644 index 0000000..e286147 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/repository/RangeRepository.java @@ -0,0 +1,12 @@ +package com.backend.soullive_a.repository; + +import com.backend.soullive_a.entity.Range; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface RangeRepository extends JpaRepository { + List findAllByProductId(Long productId); +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/service/ProductService.java b/src/main/java/com/backend/soullive_a/service/ProductService.java new file mode 100644 index 0000000..3860829 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/service/ProductService.java @@ -0,0 +1,9 @@ +package com.backend.soullive_a.service; + +import com.backend.soullive_a.dto.request.CreateProductRequest; +import com.backend.soullive_a.dto.response.CreateProductResponse; + +public interface ProductService { + CreateProductResponse createProduct(CreateProductRequest request); + CreateProductResponse getProduct(Long id); +} diff --git a/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000..0552877 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/service/impl/ProductServiceImpl.java @@ -0,0 +1,107 @@ +package com.backend.soullive_a.service.impl; + +import com.backend.soullive_a.constant.AgeType; +import com.backend.soullive_a.constant.GenderType; +import com.backend.soullive_a.dto.request.CreateProductRequest; +import com.backend.soullive_a.dto.response.CreateProductResponse; +import com.backend.soullive_a.entity.*; +import com.backend.soullive_a.repository.*; +import com.backend.soullive_a.service.ProductService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class ProductServiceImpl implements ProductService { + + private final ProductRepository productRepository; + private final BrandImageRepository brandImageRepository; + private final ProductImageRepository productImageRepository; + private final GenderRepository genderRepository; + private final AgeRepository ageRepository; + private final RangeRepository rangeRepository; + + @Override + @Transactional + public CreateProductResponse createProduct(CreateProductRequest request){ + log.info("productservice 들어옴"); + Product product = Product.builder() + .company(request.company()) + .brand(request.brand()) + .product(request.product()) + .characteristic(request.characteristic()) + .build(); + productRepository.save(product); + + List brandImages = new ArrayList<>(); + for (String image : request.brandImage()) { + BrandImage brandImage = BrandImage.builder() + .product(product) + .brandImage(image) + .build(); + brandImages.add(brandImage); + brandImageRepository.save(brandImage); + } + + List productImages = new ArrayList<>(); + for (String image : request.productImage()) { + ProductImage productImage = ProductImage.builder() + .product(product) + .productImage(image) + .build(); + productImages.add(productImage); + productImageRepository.save(productImage); + } + + List genders = new ArrayList<>(); + for (GenderType genderType : request.gender()) { + Gender gender = Gender.builder() + .product(product) + .gender(genderType) + .build(); + genders.add(gender); + genderRepository.save(gender); + } + + List ages = new ArrayList<>(); + for (AgeType ageType : request.age()) { + Age age = Age.builder() + .product(product) + .age(ageType) + .build(); + ages.add(age); + ageRepository.save(age); + } + + List ranges = new ArrayList<>(); + for (String rangeType : request.range()) { + Range range = Range.builder() + .product(product) + .range(rangeType) + .build(); + ranges.add(range); + rangeRepository.save(range); + } + return CreateProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges); + } + + @Override + @Transactional + public CreateProductResponse getProduct(Long productId) { + Product product=productRepository.findById(productId) + .orElseThrow(); + List brandImages = brandImageRepository.findAllByProductId(productId); + List productImages = productImageRepository.findAllByProductId(productId); + List genders = genderRepository.findAllByProductId(productId); + List ages = ageRepository.findAllByProductId(productId); + List ranges=rangeRepository.findAllByProductId(productId); + return CreateProductResponse.fromProduct(product, brandImages, productImages, genders, ages, ranges); + } +}