From b7b58b0a958037b89098b00acf37d0e0afa4c73b Mon Sep 17 00:00:00 2001 From: Kim Seung-yeop Date: Tue, 13 Aug 2024 13:11:19 +0900 Subject: [PATCH] =?UTF-8?q?SCRUM-44=20feat:=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.java | 25 +++++++++++ .../domain/product/dto/ProductRequest.java | 17 ++++++- .../domain/product/entity/Product.java | 4 +- .../product/service/ProductService.java | 44 +++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kakaoteck/golagola/domain/product/controller/ProductController.java b/src/main/java/com/kakaoteck/golagola/domain/product/controller/ProductController.java index 503e99f..7f25933 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/product/controller/ProductController.java +++ b/src/main/java/com/kakaoteck/golagola/domain/product/controller/ProductController.java @@ -1,4 +1,29 @@ package com.kakaoteck.golagola.domain.product.controller; +import com.kakaoteck.golagola.domain.product.dto.ProductRequest; +import com.kakaoteck.golagola.domain.product.service.ProductService; +import com.kakaoteck.golagola.domain.seller.dto.SellerResponse; +import com.kakaoteck.golagola.domain.seller.entity.Seller; +import com.kakaoteck.golagola.global.common.ApiResponse; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/product") +@CrossOrigin("*") public class ProductController { + + private final ProductService productService; + + @Operation(summary = "상품 등록", description = "상품을 등록합니다. 판매자 전용") + @PostMapping() + public ApiResponse postProduct( + @AuthenticationPrincipal Seller seller, + @RequestBody ProductRequest request + ) { + return ApiResponse.onSuccess(productService.postProduct(seller, request)); + } } diff --git a/src/main/java/com/kakaoteck/golagola/domain/product/dto/ProductRequest.java b/src/main/java/com/kakaoteck/golagola/domain/product/dto/ProductRequest.java index e9370f9..7fb83b3 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/product/dto/ProductRequest.java +++ b/src/main/java/com/kakaoteck/golagola/domain/product/dto/ProductRequest.java @@ -1,4 +1,19 @@ package com.kakaoteck.golagola.domain.product.dto; -public record ProductRequest() { +import com.kakaoteck.golagola.global.common.enums.Category; +import com.kakaoteck.golagola.global.common.enums.DetailCategory; +import lombok.Builder; + +@Builder +public record ProductRequest( + String productName, + String productExplanation, + String productImage, + Long productPrice, + Long productInventory, + Category category, + DetailCategory detailCategory, + Long discount, + Long productQuantity +) { } diff --git a/src/main/java/com/kakaoteck/golagola/domain/product/entity/Product.java b/src/main/java/com/kakaoteck/golagola/domain/product/entity/Product.java index 3031f8f..0ec8649 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/product/entity/Product.java +++ b/src/main/java/com/kakaoteck/golagola/domain/product/entity/Product.java @@ -77,10 +77,10 @@ public class Product extends BaseEntity { @Column(nullable = false) private Long productQuantity; - @Column(nullable = false) + @Column() private Float predictReviewStar; - @Column(nullable = false) + @Column() private Float productStar; public static Product from(Long productId, Seller seller, Cart cart, List reviewList, diff --git a/src/main/java/com/kakaoteck/golagola/domain/product/service/ProductService.java b/src/main/java/com/kakaoteck/golagola/domain/product/service/ProductService.java index eb842d5..de30e34 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/product/service/ProductService.java +++ b/src/main/java/com/kakaoteck/golagola/domain/product/service/ProductService.java @@ -1,4 +1,48 @@ package com.kakaoteck.golagola.domain.product.service; +import com.kakaoteck.golagola.domain.product.dto.ProductRequest; +import com.kakaoteck.golagola.domain.product.entity.Product; +import com.kakaoteck.golagola.domain.product.repository.ProductRepository; +import com.kakaoteck.golagola.domain.seller.entity.Seller; +import com.kakaoteck.golagola.domain.seller.repository.SellerRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalTime; + +@Service +@RequiredArgsConstructor +@Transactional +@Slf4j public class ProductService { + + private final SellerRepository sellerRepository; + private final ProductRepository productRepository; + + public String postProduct(Seller seller, ProductRequest request) { + // Product 객체 생성 + Product product = Product.builder() + .seller(seller) + .productName(request.productName()) + .productExplanation(request.productExplanation()) + .productImage(request.productImage()) + .productPrice(request.productPrice()) + .productInventory(request.productInventory()) + .category(request.category()) + .detailCategory(request.detailCategory()) + .discount(request.discount()) + .createTime(LocalTime.now()) + .updateTime(LocalTime.now()) + .productQuantity(request.productQuantity()) + .predictReviewStar(0.0f) // 초기 예상 리뷰 별점 + .productStar(0.0f) // 초기 실제 리뷰 별점 + .build(); + + // Product 저장 + productRepository.save(product); + + return "상품등록 성공"; + } }