Skip to content

Commit

Permalink
Feat: 브랜드 카테고리 중복 가능, 상품 조회 시 찜 여부 추가
Browse files Browse the repository at this point in the history
Feat: 브랜드 카테고리 중복 가능, 상품 조회 시 찜 여부 추가
  • Loading branch information
tokyj515 committed Aug 23, 2023
2 parents e1be8ba + 346ba1b commit 0774935
Show file tree
Hide file tree
Showing 17 changed files with 2,741 additions and 16 deletions.
2,465 changes: 2,465 additions & 0 deletions Api Test.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class BoardController {


@ApiOperation(value = "카테고리별로 상품 조회하기", notes = "의류(1), 소품(2), 악세사리(3), 잡화(4), 기타(5) | " +
"추천순(1 -> 추후 추가 예정), 인기순(2), 신상품순(3), 낮은 가격순(4), 높은 가격순(5)")
"추천순(1 -> 추후 수정 예정), 인기순(2), 신상품순(3), 낮은 가격순(4), 높은 가격순(5)" +
"테스트 시 7, 1 넣어서 해보시면 확인 가능합니다")
@GetMapping("/product/category/{categoryId}")
public Object getCategoryList(@PathVariable Long categoryId,
@RequestParam(required = false) int option){
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/example/neoul/controller/DataController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.neoul.controller;

import com.example.neoul.global.entity.ApiResponse;
import com.example.neoul.global.exception.BadRequestException;
import com.example.neoul.service.BrandService;
import com.example.neoul.service.DataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags={"06.data"})
@RequestMapping("/data")

public class DataController {
private final DataService dataService;

@ApiOperation(value = "brand data 수신", notes = "brand data 수신")
@PostMapping("/saveBrands")
public void saveBrands(@RequestBody List<Map<String, Object>> brandDataList) {
dataService.saveBrands(brandDataList);
return;
}
@ApiOperation(value = "Product data 수신", notes = "Product data 수신")
@PostMapping("/saveProducts")
public void saveProducts(@RequestBody List<Map<String, Object>> productDataList) {
dataService.saveProducts(productDataList);
}

}
2 changes: 2 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 @@ -43,6 +43,7 @@ public static class CategoryProduct { //todo 상품 찜 여부 추가
private Integer price;
private Integer discountedRatio; //상품 할인률
private List<String> productImgList; //상품사진
private boolean liked;
//private String deliveryInfo;
//private String productUrl;
private LocalDateTime createdAt;
Expand All @@ -69,6 +70,7 @@ public static class CategoryProductOrderByLikes implements Comparable<CategoryPr
//private String deliveryInfo;
//private String productUrl;
private int likes;
private boolean liked;
private LocalDateTime createdAt;

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/example/neoul/dto/brand/BrandRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class BrandRes {
// brand (전체)
public static class BrandListRes {
private Long brandId; //브랜드 id
private Long categoryVId; //카테고리 id
private String categoryVName; // 후원 카테고리 이름
private List<Long> categoryVId; //카테고리 id
private List<String> categoryVName; // 후원 카테고리 이름
private String name; //브랜드 이름
private String intro; //한 줄소개
private String profileImg; //브랜드 프로필 이미지
Expand Down Expand Up @@ -56,8 +56,8 @@ public static class ProductListRes {
// brand/상세
public static class BrandInfoRes {
private Long brandId; //브랜드 id
private Long categoryVId; //카테고리 id
private String categoryVName; // 후원 카테고리 이름
private List<Long> categoryVId; //카테고리 id
private List<String> categoryVName; // 후원 카테고리 이름
private String brandName; //브랜드 이름
private String intro; //한 줄소개
private String profileImg; //브랜드 프로필 이미지
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/example/neoul/entity/brand/BrandCategoryV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.neoul.entity.brand;

import com.example.neoul.entity.category.CategoryV;
import com.example.neoul.entity.user.User;
import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;

@DynamicInsert
@DynamicUpdate
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "brand_category_v")
@Entity
public class BrandCategoryV {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@JoinColumn(name = "brand_id")
@ManyToOne(fetch = FetchType.LAZY)
private Brand brand;

@JoinColumn(name = "categoryv_id")
@ManyToOne(fetch = FetchType.LAZY)
private CategoryV categoryV;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/test/**").permitAll()
.antMatchers("/user/signup", "/user/login").permitAll()
.antMatchers("/oauth/**").permitAll()
.antMatchers("/data/**").permitAll()



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private ApiKey apiKey() {
return new ApiKey("Authorization", "Bearer", "header");
}


private ApiInfo apiInfo() {
String title = "NEOUL";
String description = "NEOUL API Documentation";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.neoul.repository;

import com.example.neoul.entity.brand.Brand;
import com.example.neoul.entity.brand.BrandCategoryV;
import com.example.neoul.entity.category.CategoryV;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BrandCategoryVRepository extends JpaRepository<BrandCategoryV, Long> {

List<BrandCategoryV> findAllByBrand(Brand brand);
List<BrandCategoryV> findAllByCategoryV(CategoryV categoryV);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
public interface BrandRepository extends JpaRepository<Brand, Long> {
List<Brand> findAllByCategoryV(CategoryV categoryV);
// List<Brand> findAllByLikedUser(User user);
Brand findByName(String name);
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public interface CategoryPRepository extends JpaRepository<CategoryP, Long> {

Optional<CategoryP> findById(Long pcategoryId);
Optional<CategoryP> findByName(String name);

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

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
Optional<Product> findByName(String name);
List<Product> findAllByBrand(Brand brand);

List<Product> findAllByCategoryP(CategoryP categoryP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public interface UserLikedProductRepository extends JpaRepository<UserLikedProdu
List<UserLikedProduct> findAllByUser(User user);

Optional<UserLikedProduct> findByUserAndProduct(User user, Product product);

boolean existsByUserAndProduct(User user, Product product);
int countAllByProduct(Product product);
}
35 changes: 35 additions & 0 deletions src/main/java/com/example/neoul/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.neoul.entity.brand.Product;
import com.example.neoul.entity.brand.ProductImage;
import com.example.neoul.entity.category.CategoryP;
import com.example.neoul.entity.user.User;
import com.example.neoul.entity.user.UserLikedProduct;
import com.example.neoul.global.exception.NotFoundException;
import com.example.neoul.repository.CategoryPRepository;
import com.example.neoul.repository.ProductImageRepository;
Expand All @@ -27,6 +29,8 @@ public class BoardService {

private final UserLikedProductRepository userLikedProductRepository;

private final UserService userService;

public CategoryP getCategoryPByCategoryId(Long categoryId){
Optional<CategoryP> optionalCategoryP = categoryPRepository.findById(categoryId);
if(optionalCategoryP.isEmpty()) {
Expand All @@ -39,18 +43,23 @@ public CategoryP getCategoryPByCategoryId(Long categoryId){

//TODO 추천순은 추후 수정 예정
public List<BoardRes.CategoryProduct> getCategoryProductListOrderByRecommendation(Long categoryId) {
User user = userService.findNowLoginUser();
CategoryP categoryP = getCategoryPByCategoryId(categoryId);
List<Product> productList = productRepository.findAllByCategoryP(categoryP);
List<BoardRes.CategoryProduct> result = new ArrayList<>();

for(Product product : productList){
//상품 이미지
List<ProductImage> images = productImageRepository.findAllByProduct(product);
List<String> productImgList = new ArrayList<>();

for(ProductImage productImage : images){
productImgList.add(productImage.getUrl());
}

//유저 좋아요 여부
boolean liked = userLikedProductRepository.existsByUserAndProduct(user, product);

BoardRes.CategoryProduct e = BoardRes.CategoryProduct.builder()
.productId(product.getId())
.categoryId(categoryId)
Expand All @@ -59,6 +68,7 @@ public List<BoardRes.CategoryProduct> getCategoryProductListOrderByRecommendatio
.price(product.getPrice())
.discountedRatio(product.getDiscountedRatio())
.productImgList(productImgList)
.liked(liked)
.createdAt(product.getCreatedAt())
.build();

Expand All @@ -72,20 +82,25 @@ public List<BoardRes.CategoryProduct> getCategoryProductListOrderByRecommendatio


public List<BoardRes.CategoryProductOrderByLikes> getCategoryProductListOrderByLikes(Long categoryId) {
User user = userService.findNowLoginUser();
CategoryP categoryP = getCategoryPByCategoryId(categoryId);
List<Product> productList = productRepository.findAllByCategoryP(categoryP);
List<BoardRes.CategoryProductOrderByLikes> result = new ArrayList<>();

for(Product product : productList){
int likes = userLikedProductRepository.countAllByProduct(product);

//상품 이미지
List<ProductImage> images = productImageRepository.findAllByProduct(product);
List<String> productImgList = new ArrayList<>();

for(ProductImage productImage : images){
productImgList.add(productImage.getUrl());
}

//유저 좋아요 여부
boolean liked = userLikedProductRepository.existsByUserAndProduct(user, product);

BoardRes.CategoryProductOrderByLikes e = BoardRes.CategoryProductOrderByLikes.builder()
.productId(product.getId())
.categoryId(categoryId)
Expand All @@ -95,6 +110,7 @@ public List<BoardRes.CategoryProductOrderByLikes> getCategoryProductListOrderByL
.discountedRatio(product.getDiscountedRatio())
.productImgList(productImgList)
.likes(likes)
.liked(liked)
.createdAt(product.getCreatedAt())
.build();

Expand All @@ -107,18 +123,23 @@ public List<BoardRes.CategoryProductOrderByLikes> getCategoryProductListOrderByL
}

public List<BoardRes.CategoryProduct> getCategoryProductListByCreatedAtDesc(Long categoryId) {
User user = userService.findNowLoginUser();
CategoryP categoryP = getCategoryPByCategoryId(categoryId);
List<Product> productList = productRepository.findAllByCategoryPOrderByCreatedAtDesc(categoryP);
List<BoardRes.CategoryProduct> result = new ArrayList<>();

for(Product product : productList){
//상품 이미지
List<ProductImage> images = productImageRepository.findAllByProduct(product);
List<String> productImgList = new ArrayList<>();

for(ProductImage productImage : images){
productImgList.add(productImage.getUrl());
}

//유저 좋아요 여부
boolean liked = userLikedProductRepository.existsByUserAndProduct(user, product);

BoardRes.CategoryProduct e = BoardRes.CategoryProduct.builder()
.productId(product.getId())
.categoryId(categoryId)
Expand All @@ -127,6 +148,7 @@ public List<BoardRes.CategoryProduct> getCategoryProductListByCreatedAtDesc(Long
.price(product.getPrice())
.discountedRatio(product.getDiscountedRatio())
.productImgList(productImgList)
.liked(liked)
.createdAt(product.getCreatedAt())
.build();

Expand All @@ -138,18 +160,23 @@ public List<BoardRes.CategoryProduct> getCategoryProductListByCreatedAtDesc(Long
}

public List<BoardRes.CategoryProduct> getCategoryProductListByPriceAsc(Long categoryId) {
User user = userService.findNowLoginUser();
CategoryP categoryP = getCategoryPByCategoryId(categoryId);
List<Product> productList = productRepository.findAllByCategoryPOrderByPriceAsc(categoryP);
List<BoardRes.CategoryProduct> result = new ArrayList<>();

for(Product product : productList){
//상품 이미지
List<ProductImage> images = productImageRepository.findAllByProduct(product);
List<String> productImgList = new ArrayList<>();

for(ProductImage productImage : images){
productImgList.add(productImage.getUrl());
}

//유저 좋아요 여부
boolean liked = userLikedProductRepository.existsByUserAndProduct(user, product);

BoardRes.CategoryProduct e = BoardRes.CategoryProduct.builder()
.productId(product.getId())
.categoryId(categoryId)
Expand All @@ -159,6 +186,7 @@ public List<BoardRes.CategoryProduct> getCategoryProductListByPriceAsc(Long cate
.discountedRatio(product.getDiscountedRatio())
.productImgList(productImgList)
.createdAt(product.getCreatedAt())
.liked(liked)
.build();

result.add(e);
Expand All @@ -169,18 +197,24 @@ public List<BoardRes.CategoryProduct> getCategoryProductListByPriceAsc(Long cate
}

public List<BoardRes.CategoryProduct> getCategoryProductListByPriceDesc(Long categoryId) {
User user = userService.findNowLoginUser();
CategoryP categoryP = getCategoryPByCategoryId(categoryId);
List<Product> productList = productRepository.findAllByCategoryPOrderByPriceDesc(categoryP);
List<BoardRes.CategoryProduct> result = new ArrayList<>();

for(Product product : productList){
//상품 이미지
List<ProductImage> images = productImageRepository.findAllByProduct(product);
List<String> productImgList = new ArrayList<>();

for(ProductImage productImage : images){
productImgList.add(productImage.getUrl());
}

//유저 좋아요 여부
boolean liked = userLikedProductRepository.existsByUserAndProduct(user, product);


BoardRes.CategoryProduct e = BoardRes.CategoryProduct.builder()
.productId(product.getId())
.categoryId(categoryId)
Expand All @@ -190,6 +224,7 @@ public List<BoardRes.CategoryProduct> getCategoryProductListByPriceDesc(Long cat
.discountedRatio(product.getDiscountedRatio())
.productImgList(productImgList)
.createdAt(product.getCreatedAt())
.liked(liked)
.build();

result.add(e);
Expand Down
Loading

0 comments on commit 0774935

Please sign in to comment.