Skip to content

Commit

Permalink
Feat: 상품 상세 조회 및 전체 조회 #18
Browse files Browse the repository at this point in the history
Feat: 상품 상세 조회 및 전체 조회 #18
  • Loading branch information
tokyj515 authored Aug 2, 2023
2 parents df39b95 + b7b8376 commit 7a9d2e8
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/example/neoul/controller/CategoryController.java
Original file line number Diff line number Diff line change
@@ -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<CategoryP> pCategory() {
if(categoryService.getPCategory() != null){
}
}*/



}
80 changes: 80 additions & 0 deletions src/main/java/com/example/neoul/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -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<List<ProductRes.ProductDetailRes>> getAllProducts() {
return new ApiResponse<>(productService.getAllProducts());
}

@ApiOperation(value = "상품 상세조회", notes = "상품 상세조회 api 입니다. {productId}에 {1} 처럼 상품 id를 넣고 요청을 보내면 상품을 상세조회할 수 있습니다.")
@GetMapping("/{productId}")
public ApiResponse<ProductRes.ProductDetailRes> getProductById(@PathVariable Long productId) {
ProductRes.ProductDetailRes product = productService.getProduct(productId);
return new ApiResponse<>(product);
}



/*@GetMapping("/product/{pid}")
public ResponseEntity<Product> getProduct(@PathVariable Long pid) {
return ResponseEntity.ok()
.body(productService.getProduct(pid).get());
}*/


/*// 브랜드 list
@GetMapping("/list")
public ApiResponse<List<BrandRes.BrandListRes>> list(){
return new ApiResponse(brandService.list());
}
// 브랜드 상세조회
@GetMapping("/{brandId}")
public ApiResponse<BrandRes.BrandInfoRes> brandInfo(@PathVariable("brandId") Long brandId){
return new ApiResponse<>(brandService.info(brandId));
}*/







/*//ReadAll
@GetMapping("/product")
public List<Product> readAllProduct(){
return productRepository.findAll();
}
@GetMapping("/product/{pid}")
public ApiResponse<List<ProductRes.RecruitProductRes>> getProduct(@PathVariable Long productId,
@RequestParam(required = false) Integer option){
return new ApiResponse<>(productService.getProduct(productId, 1));
}*/




}
26 changes: 14 additions & 12 deletions src/main/java/com/example/neoul/dto/product/ProductRes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.neoul.dto.product;

import lombok.*;

import java.time.LocalDate;
import java.util.List;

public class ProductRes {
Expand All @@ -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<String> pImgList; //상품사진
private String pDeliveryInfo; //상품 배송정보 (무료배송 etc..)
private String pUrl; //상품상세 url
private List<String> 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; //상품 클릭 일시
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CategoryV, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<ProductImage, Long> {

List<ProductImage> findAllByProduct(Product product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;


@Repository
Expand All @@ -17,4 +18,6 @@ public interface ProductRepository extends JpaRepository<Product, Long> {

List<Product> findAllByCategoryP(CategoryP categoryP);

Optional<Product> findById(Long productId);

}
42 changes: 42 additions & 0 deletions src/main/java/com/example/neoul/service/CategoryService.java
Original file line number Diff line number Diff line change
@@ -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<CategoryP> getpCategoryList() {
List<CategoryP> categoryP = categoryPRepository.findAll();

if(!categoryP.isEmpty()) return categoryPRepository.findAll();
else throw new IllegalArgumentException("no such data");
}
public List<CategoryV> getvCategoryList() {
List<CategoryV> 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"));
}
}
83 changes: 83 additions & 0 deletions src/main/java/com/example/neoul/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -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<ProductRes.ProductDetailRes> getAllProducts() {
List<Product> products = productRepository.findAll();
List<ProductRes.ProductDetailRes> 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<ProductImage> productImages = productImageRepository.findAllByProduct(product);
List<String> 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<Product> 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"));
}*/





}

0 comments on commit 7a9d2e8

Please sign in to comment.