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 "상품등록 성공"; + } }