-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 상품 상세 조회 및 전체 조회 #18
- Loading branch information
Showing
8 changed files
with
259 additions
and
12 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/com/example/neoul/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
80
src/main/java/com/example/neoul/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}*/ | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/example/neoul/repository/ProductImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/example/neoul/service/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
src/main/java/com/example/neoul/service/ProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}*/ | ||
|
||
|
||
|
||
|
||
|
||
} |