Skip to content

Commit

Permalink
Feat: 카테고리별 상품 조회 API(옵션은 추후 추가 예정) #29
Browse files Browse the repository at this point in the history
  • Loading branch information
tokyj515 committed Aug 1, 2023
1 parent 1367c91 commit 25ae0fe
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 112 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/example/neoul/controller/BoardController.java
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));
}



}
23 changes: 12 additions & 11 deletions src/main/java/com/example/neoul/controller/BrandController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
public class BrandController {
private final BrandService brandService;

// 브랜드 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));
}
//
// // 브랜드 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));
// }

}
20 changes: 11 additions & 9 deletions src/main/java/com/example/neoul/controller/StoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public class StoryController {
private final StoryService storyService;

// 스토리 list
@GetMapping("/list")
public ApiResponse<List<StoryRes.StoryListRes>> list(){
return new ApiResponse(storyService.list());
}
// @GetMapping("/list")
// public ApiResponse<List<StoryRes.StoryListRes>> list(){
// return new ApiResponse(storyService.list());
// }
//
// // 스토리 상세조회
// @GetMapping("/{storyId}")
// public ApiResponse<StoryRes.StoryInfoRes> storyInfo(@PathVariable("storyId") Long storyId){
// return new ApiResponse<>(storyService.info(storyId));
// }


// 스토리 상세조회
@GetMapping("/{storyId}")
public ApiResponse<StoryRes.StoryInfoRes> storyInfo(@PathVariable("storyId") Long storyId){
return new ApiResponse<>(storyService.info(storyId));
}
}

17 changes: 17 additions & 0 deletions src/main/java/com/example/neoul/dto/board/BoardRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public static class QABoardRes {
}


@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public static class CategoryBoardSimple {
private Long productId;
private Long categoryId;
private String brandName;
private String productName;
private Integer price;
//private String deliveryInfo;
private String productUrl;
}






Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/neoul/entity/brand/Brand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Brand extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vcategory_id")
private CategoryV brandVCategory;
private CategoryV categoryV;

@Column(name = "name")
private String name;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/neoul/entity/brand/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Product extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pcategory_id")
private CategoryP brandPCategory;
private CategoryP categoryP;

private String name;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/neoul/entity/brand/Story.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Story extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vcategory_id")
private CategoryV storyVCategory;
private CategoryV categoryV;

@Column(name = "img")
private String img;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class CategoryP {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pcatogery_id")
private Long pcategoryId;
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/example/neoul/entity/category/CategoryV.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class CategoryV {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "vcategory_id")
private Long vcategoryId;
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;
Expand All @@ -35,14 +35,14 @@ public class CategoryV {


//이건 그냥 리포지토리에서 ALL로 불러와도 될 듯!
// story 들에 대한 관계
@OneToMany(mappedBy = "storyVCategory")
private List<Story> stories = new ArrayList<Story>();

// brand 들에 대한 관계
@OneToMany(mappedBy = "brandVCategory")
private List<Brand> brands = new ArrayList<Brand>();

// // story 들에 대한 관계
// @OneToMany(mappedBy = "categoryV")
// private List<Story> stories = new ArrayList<Story>();
//
// // brand 들에 대한 관계
// @OneToMany(mappedBy = "categoryV")
// private List<Brand> brands = new ArrayList<Brand>();
//



Expand Down
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.example.neoul.entity.brand.Brand;
import com.example.neoul.entity.brand.Product;
import com.example.neoul.entity.category.CategoryP;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -12,4 +13,6 @@
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findAllByBrand(Brand brand);

List<Product> findAllByCategoryP(CategoryP categoryP);
}
54 changes: 54 additions & 0 deletions src/main/java/com/example/neoul/service/BoardService.java
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;
}
}
82 changes: 41 additions & 41 deletions src/main/java/com/example/neoul/service/BrandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@ public class BrandService {
private final BrandRepository brandRepository;
private final ProductRepository productRepository;

public List<BrandRes.BrandListRes> list(){
List<Brand> brandList= brandRepository.findAll();
return makeBrandList(brandList);
}

public List<BrandRes.BrandListRes> makeBrandList(List<Brand> brandList){
List<BrandRes.BrandListRes> responseList = new ArrayList<>();
for(Brand brand : brandList){
BrandRes.BrandListRes brandListRes = BrandRes.BrandListRes.builder()
.bid(brand.getBrandId())
.categoryVId(brand.getBrandVCategory().getVcategoryId())
.categoryVName(brand.getBrandVCategory().getName())
.bName(brand.getName())
.bIntro(brand.getIntro())
.bProfileImg(brand.getProfileImg())
.products(productRepository.findAllByBrand(brand))
.build();
responseList.add(brandListRes);
}
return responseList;
}

public BrandRes.BrandInfoRes info(Long brandId){
Brand brand = brandRepository.findById(brandId).get(); // 실패시 exception 발생
BrandRes.BrandInfoRes brandInfo = BrandRes.BrandInfoRes.builder()
.bid(brand.getBrandId())
.categoryVId(brand.getBrandVCategory().getVcategoryId())
.categoryVName(brand.getBrandVCategory().getName())
.bName(brand.getName())
.bIntro(brand.getIntro())
.bProfileImg(brand.getProfileImg())
.products(productRepository.findAllByBrand(brand))
// .hashTag() // 해시태그 내용
// .bCreatedAt()
// .bLikeCNT() // 찜개수
// .bHearted() // 찜 여부
.build();

return brandInfo;

}
// public List<BrandRes.BrandListRes> list(){
// List<Brand> brandList= brandRepository.findAll();
// return makeBrandList(brandList);
// }

// public List<BrandRes.BrandListRes> makeBrandList(List<Brand> brandList){
// List<BrandRes.BrandListRes> responseList = new ArrayList<>();
// for(Brand brand : brandList){
// BrandRes.BrandListRes brandListRes = BrandRes.BrandListRes.builder()
// .bid(brand.getBrandId())
// .categoryVId(brand.getCategoryV().getVcategoryId())
// .categoryVName(brand.getCategoryV().getName())
// .bName(brand.getName())
// .bIntro(brand.getIntro())
// .bProfileImg(brand.getProfileImg())
// .products(productRepository.findAllByBrand(brand))
// .build();
// responseList.add(brandListRes);
// }
// return responseList;
// }
//
// public BrandRes.BrandInfoRes info(Long brandId){
// Brand brand = brandRepository.findById(brandId).get(); // 실패시 exception 발생
// BrandRes.BrandInfoRes brandInfo = BrandRes.BrandInfoRes.builder()
// .bid(brand.getBrandId())
// .categoryVId(brand.getCategoryV().getVcategoryId())
// .categoryVName(brand.getCategoryV().getName())
// .bName(brand.getName())
// .bIntro(brand.getIntro())
// .bProfileImg(brand.getProfileImg())
// .products(productRepository.findAllByBrand(brand))
//// .hashTag() // 해시태그 내용
//// .bCreatedAt()
//// .bLikeCNT() // 찜개수
//// .bHearted() // 찜 여부
// .build();
//
// return brandInfo;
//
// }
}
Loading

0 comments on commit 25ae0fe

Please sign in to comment.