diff --git a/src/main/java/com/example/neoul/controller/CategoryController.java b/src/main/java/com/example/neoul/controller/CategoryController.java new file mode 100644 index 0000000..2767f0d --- /dev/null +++ b/src/main/java/com/example/neoul/controller/CategoryController.java @@ -0,0 +1,30 @@ +package com.example.neoul.controller; + +import com.example.neoul.entity.category.CategoryP; +import com.example.neoul.service.CategoryService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@Api(tags={"00.category"}) +@RequestMapping("/category") +public class CategoryController { + + private final CategoryService categoryService; + + /*@ApiOperation(value = "카테고리 반환", notes = "상품 카테고리 입니다") + @GetMapping("/now") + public BaseResponse pCategory() { + if(categoryService.getPCategory() != null){ + + } + }*/ + + + +} diff --git a/src/main/java/com/example/neoul/controller/ProductController.java b/src/main/java/com/example/neoul/controller/ProductController.java new file mode 100644 index 0000000..f1322dd --- /dev/null +++ b/src/main/java/com/example/neoul/controller/ProductController.java @@ -0,0 +1,80 @@ +package com.example.neoul.controller; + +import com.example.neoul.dto.product.ProductRes; +import com.example.neoul.global.entity.ApiResponse; +import com.example.neoul.service.ProductService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + + + +@RequiredArgsConstructor +@RestController +@Api(tags={"05. product"}) +@RequestMapping("/product") +public class ProductController { //🛍️ + + private final ProductService productService; + + + @ApiOperation(value = "상품 전체조회", notes = "상품 전체조회 api 입니다") + @GetMapping("/list") + public ApiResponse> getAllProducts() { + return new ApiResponse<>(productService.getAllProducts()); + } + + @ApiOperation(value = "상품 상세조회", notes = "상품 상세조회 api 입니다. {productId}에 {1} 처럼 상품 id를 넣고 요청을 보내면 상품을 상세조회할 수 있습니다.") + @GetMapping("/{productId}") + public ApiResponse getProductById(@PathVariable Long productId) { + ProductRes.ProductDetailRes product = productService.getProduct(productId); + return new ApiResponse<>(product); + } + + + + /*@GetMapping("/product/{pid}") + public ResponseEntity getProduct(@PathVariable Long pid) { + + return ResponseEntity.ok() + .body(productService.getProduct(pid).get()); + }*/ + + + /*// 브랜드 list + @GetMapping("/list") + public ApiResponse> list(){ + return new ApiResponse(brandService.list()); + } + + // 브랜드 상세조회 + @GetMapping("/{brandId}") + public ApiResponse brandInfo(@PathVariable("brandId") Long brandId){ + return new ApiResponse<>(brandService.info(brandId)); + }*/ + + + + + + + + /*//ReadAll + @GetMapping("/product") + public List readAllProduct(){ + return productRepository.findAll(); + } + + @GetMapping("/product/{pid}") + public ApiResponse> getProduct(@PathVariable Long productId, + @RequestParam(required = false) Integer option){ + return new ApiResponse<>(productService.getProduct(productId, 1)); + }*/ + + + + +} diff --git a/src/main/java/com/example/neoul/dto/product/ProductRes.java b/src/main/java/com/example/neoul/dto/product/ProductRes.java index 0f27128..811bc5c 100644 --- a/src/main/java/com/example/neoul/dto/product/ProductRes.java +++ b/src/main/java/com/example/neoul/dto/product/ProductRes.java @@ -1,6 +1,8 @@ package com.example.neoul.dto.product; import lombok.*; + +import java.time.LocalDate; import java.util.List; public class ProductRes { @@ -11,22 +13,22 @@ public class ProductRes { @AllArgsConstructor @Setter @Getter - // main - 상품조회 - public static class RecruitProductRes { - private Long pid; //상품 id - private Long bid; //브랜드 id - private Long cid; //카테고리 id - private String category; //상품 카테고리 - private String pName; //상품명 + // main - 상품 상세조회 + public static class ProductDetailRes { + private Long productId; //상품 id + private Long brandId; //브랜드 id + private Long categoryPId; //카테고리 id + private String categoryName; //상품 카테고리 + private String productName; //상품명 private Integer price; //상품금액 - private List pImgList; //상품사진 - private String pDeliveryInfo; //상품 배송정보 (무료배송 etc..) - private String pUrl; //상품상세 url + private List productImgList; //상품사진 + private String deliveryInfo; //상품 배송정보 (무료배송 etc..) + private String productUrl; //상품상세 url private Integer pLikeCNT; //상품찜 개수 private Boolean pHearted; //상품찜 여부 true false - private String pCreatedAt; //상품 업로드 일자 + private LocalDate createdAt; //상품 업로드 일자 - private String clickedAt; //상품 클릭 일시 + // private LocalDate clickedAt; //상품 클릭 일시 } diff --git a/src/main/java/com/example/neoul/repository/CategoryVRepository.java b/src/main/java/com/example/neoul/repository/CategoryVRepository.java index d70e25e..48f6bcf 100644 --- a/src/main/java/com/example/neoul/repository/CategoryVRepository.java +++ b/src/main/java/com/example/neoul/repository/CategoryVRepository.java @@ -2,6 +2,8 @@ import com.example.neoul.entity.category.CategoryV; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface CategoryVRepository extends JpaRepository { } diff --git a/src/main/java/com/example/neoul/repository/ProductImageRepository.java b/src/main/java/com/example/neoul/repository/ProductImageRepository.java index b38bae6..90413c6 100644 --- a/src/main/java/com/example/neoul/repository/ProductImageRepository.java +++ b/src/main/java/com/example/neoul/repository/ProductImageRepository.java @@ -1,9 +1,14 @@ package com.example.neoul.repository; +import com.example.neoul.entity.brand.Product; import com.example.neoul.entity.brand.ProductImage; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface ProductImageRepository extends JpaRepository { + + List findAllByProduct(Product product); } diff --git a/src/main/java/com/example/neoul/repository/ProductRepository.java b/src/main/java/com/example/neoul/repository/ProductRepository.java index 674ad67..d23faf0 100644 --- a/src/main/java/com/example/neoul/repository/ProductRepository.java +++ b/src/main/java/com/example/neoul/repository/ProductRepository.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository @@ -17,4 +18,6 @@ public interface ProductRepository extends JpaRepository { List findAllByCategoryP(CategoryP categoryP); + Optional findById(Long productId); + } diff --git a/src/main/java/com/example/neoul/service/CategoryService.java b/src/main/java/com/example/neoul/service/CategoryService.java new file mode 100644 index 0000000..36d9d34 --- /dev/null +++ b/src/main/java/com/example/neoul/service/CategoryService.java @@ -0,0 +1,42 @@ +package com.example.neoul.service; + +import com.example.neoul.entity.brand.Product; +import com.example.neoul.entity.category.CategoryP; +import com.example.neoul.entity.category.CategoryV; +import com.example.neoul.repository.CategoryPRepository; +import com.example.neoul.repository.CategoryVRepository; +import com.example.neoul.repository.ProductRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@RequiredArgsConstructor +@Service +public class CategoryService { + @Autowired + private CategoryPRepository categoryPRepository; + private CategoryVRepository categoryVRepository; + + public List getpCategoryList() { + List categoryP = categoryPRepository.findAll(); + + if(!categoryP.isEmpty()) return categoryPRepository.findAll(); + else throw new IllegalArgumentException("no such data"); + } + public List getvCategoryList() { + List categoryV = categoryVRepository.findAll(); + + if(!categoryV.isEmpty()) return categoryVRepository.findAll(); + else throw new IllegalArgumentException("no such data"); + } + + public CategoryP getPCategory(final Long cid) { + return categoryPRepository.findById(cid).orElseThrow(() -> new IllegalArgumentException("no such data")); + } + + public CategoryV getVCategory(final Long cid) { + return categoryVRepository.findById(cid).orElseThrow(() -> new IllegalArgumentException("no such data")); + } +} diff --git a/src/main/java/com/example/neoul/service/ProductService.java b/src/main/java/com/example/neoul/service/ProductService.java new file mode 100644 index 0000000..17d1542 --- /dev/null +++ b/src/main/java/com/example/neoul/service/ProductService.java @@ -0,0 +1,83 @@ +package com.example.neoul.service; + +import com.example.neoul.dto.product.ProductRes; +import com.example.neoul.entity.brand.Product; +import com.example.neoul.entity.brand.ProductImage; +import com.example.neoul.global.exception.NotFoundException; +import com.example.neoul.repository.ProductImageRepository; +import com.example.neoul.repository.ProductRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class ProductService { + + private final ProductRepository productRepository; + + private final ProductImageRepository productImageRepository; + + + // 상품 전체 리스트 + public List getAllProducts() { + List products = productRepository.findAll(); + List result = new ArrayList<>(); + + for(Product product : products){ + ProductRes.ProductDetailRes e = getProduct(product.getId()); + result.add(e); + } + + return result; + } + + public ProductRes.ProductDetailRes getProduct(Long productId) { + Product product = getProductByProductId(productId); + List productImages = productImageRepository.findAllByProduct(product); + List productImgList = new ArrayList<>(); + + for(ProductImage productImage : productImages){ + productImgList.add(productImage.getUrl()); + } + + return ProductRes.ProductDetailRes.builder() + .productId(product.getId()) + .brandId(product.getBrand().getId()) + .categoryPId(product.getCategoryP().getId()) + .categoryName(product.getCategoryP().getName()) //얘는 살려야함 + .productName(product.getName()) + .price(product.getPrice()) + .productImgList(productImgList) //얘는 살려야함 + .deliveryInfo(product.getDeliveryInfo()) + .productUrl(product.getProductUrl()) +// .pLikeCNT(product.getLikeCount()) +// .pHearted(product.isHearted()) + .createdAt(product.getCreatedAt().toLocalDate()) +// .clickedAt(product.getClickedAt()) + .build(); + } + + //상품 상세조회 + public Product getProductByProductId(Long productId){ + Optional optionalProduct = productRepository.findById(productId); + if(optionalProduct.isEmpty()) { + throw new NotFoundException("존재하지 않는 상품입니다"); + } + return optionalProduct.get(); + } + + + + /*public Product getProduct(final Long pid) { + return productRepository.findById(pid).orElseThrow(() -> new IllegalArgumentException("no such data")); + }*/ + + + + + +}