-
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: 카테고리별 상품 조회 API(옵션은 추후 추가 예정) #29
- Loading branch information
Showing
14 changed files
with
232 additions
and
112 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/neoul/controller/BoardController.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,32 @@ | ||
package com.example.neoul.controller; | ||
|
||
|
||
import com.example.neoul.dto.board.BoardRes; | ||
import com.example.neoul.global.entity.ApiResponse; | ||
import com.example.neoul.global.exception.BadRequestException; | ||
import com.example.neoul.service.BoardService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Api(tags={"04.board"}) | ||
@RequestMapping("/board") | ||
public class BoardController { | ||
|
||
private final BoardService boardService; | ||
|
||
@ApiOperation(value = "카테고리별로 상품 조회하기", notes = "의류(1), 소품(2), 악세사리(3), 잡화(4), 기타(5) | 추천순, 인기순, 신상품순, 낮은 가격순, 높은 가격순") | ||
@GetMapping("/{categoryId}") | ||
public ApiResponse<List<BoardRes.CategoryBoardSimple>> getCategoryList(@PathVariable Long categoryId, | ||
@RequestParam(required = false) Integer option){ | ||
return new ApiResponse<>(boardService.getCategoryList(categoryId, 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
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
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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/neoul/repository/CategoryPRepository.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,12 @@ | ||
package com.example.neoul.repository; | ||
|
||
import com.example.neoul.entity.category.CategoryP; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CategoryPRepository extends JpaRepository<CategoryP, Long> { | ||
|
||
Optional<CategoryP> findById(Long pcategoryId); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.example.neoul.service; | ||
|
||
import com.example.neoul.dto.board.BoardRes; | ||
import com.example.neoul.entity.brand.Product; | ||
import com.example.neoul.entity.category.CategoryP; | ||
import com.example.neoul.global.exception.NotFoundException; | ||
import com.example.neoul.repository.CategoryPRepository; | ||
import com.example.neoul.repository.ProductRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.cfg.CreateKeySecondPass; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BoardService { | ||
|
||
private final ProductRepository productRepository; | ||
private final CategoryPRepository categoryPRepository; | ||
|
||
public CategoryP getCategoryPByCategoryId(Long categoryId){ | ||
Optional<CategoryP> optionalCategoryP = categoryPRepository.findById(categoryId); | ||
if(optionalCategoryP.isEmpty()) { | ||
throw new NotFoundException("존재하지 않는 브랜드 카테고리입니다"); | ||
} | ||
return optionalCategoryP.get(); | ||
} | ||
|
||
|
||
public List<BoardRes.CategoryBoardSimple> getCategoryList(Long categoryId, int option) { | ||
CategoryP categoryP = getCategoryPByCategoryId(categoryId); | ||
List<Product> productList = productRepository.findAllByCategoryP(categoryP); | ||
List<BoardRes.CategoryBoardSimple> result = new ArrayList<>(); | ||
|
||
for(Product product : productList){ | ||
BoardRes.CategoryBoardSimple e = BoardRes.CategoryBoardSimple.builder() | ||
.productId(product.getProductId()) | ||
.categoryId(categoryId) | ||
.brandName(product.getBrand().getName()) | ||
.productName(product.getName()) | ||
.price(product.getPrice()) | ||
.productUrl(product.getProductUrl()) | ||
.build(); | ||
|
||
result.add(e); | ||
} | ||
|
||
|
||
return result; | ||
} | ||
} |
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
Oops, something went wrong.