Skip to content

Commit

Permalink
Feature/like cart (#24)
Browse files Browse the repository at this point in the history
* feature: μ’‹μ•„μš”ν•œ μƒν’ˆ 쑰회

* feature: μž₯λ°”κ΅¬λ‹ˆ 도메인 및 μž₯λ°”κ΅¬λ‹ˆ 등둝/μ‚­μ œ

* docs: λ©”μ†Œλ“œλͺ… μˆ˜μ •
  • Loading branch information
mushroom1324 authored Mar 20, 2024
1 parent 6d33680 commit 99efdd5
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public SuccessResponse<ProductResponse> updateProduct(@PathVariable Long product
return SuccessResponse.createSuccess(productService.updateProduct(productId, patchProduct));
}

@GetMapping("/like")
public SuccessResponse<Boolean> likeProduct(@RequestParam Long productId) {
return SuccessResponse.createSuccess(productService.likeProduct(productId));
}

@GetMapping("/recommendation")
public SuccessResponse<List<GetProductThumbnail>> getMainPageRecommendation(
@Parameter(description = "쑰회 의λ₯˜ 성별") @RequestParam(required = false) String gender,
Expand Down Expand Up @@ -114,4 +109,23 @@ public SuccessResponse<List<GetProductThumbnail>> getHighestDiscountProduct(
return SuccessResponse.success(productService.getHighestDiscount(gender, category, styles, minPrice, maxPrice, brandNames, qualityRates, sizes, cursorId, pageSize));
}

@GetMapping("/like")
public SuccessResponse<Boolean> toggleLike(@RequestParam Long productId) {
return SuccessResponse.createSuccess(productService.toggleLike(productId));
}

@GetMapping("/liked")
public SuccessResponse<List<GetProductThumbnail>> getLikedProduct(
@Parameter(description = "μΉ΄ν…Œκ³ λ¦¬") @RequestParam(required = false) String category,
@Parameter(description = "1번째 νŽ˜μ΄μ§€ μ‘°νšŒμ‹œ null, " +
"2번째 이상 νŽ˜μ΄μ§€ μ‘°νšŒμ‹œ 직전 νŽ˜μ΄μ§€μ˜ λ§ˆμ§€λ§‰ episode id") @RequestParam(required = false) Long cursorId,
@Parameter(description = "ν•œ νŽ˜μ΄μ§€μ— κ°€μ Έμ˜¬ μ—ν”Όμ†Œλ“œ 개수, κΈ°λ³Έκ°’ 4") @RequestParam(required = false) Integer pageSize) {
return SuccessResponse.success(productService.getLiked(category, cursorId, pageSize));
}

@GetMapping("/cart")
public SuccessResponse<Boolean> toggleCart(@RequestParam Long productId) {
return SuccessResponse.createSuccess(productService.toggleCart(productId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.repick.domain.product.entity;

import com.example.repick.global.entity.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Entity @NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductCart extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long userId;
private Long productId;

@Builder
public ProductCart(Long userId, Long productId) {
this.userId = userId;
this.productId = productId;
}

public static ProductCart of(Long userId, Long productId) {
return ProductCart.builder()
.userId(userId)
.productId(productId)
.build();
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.repick.domain.product.entity;

import com.example.repick.global.entity.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -9,7 +10,7 @@
import lombok.NoArgsConstructor;

@Entity @NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductLike {
public class ProductLike extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.repick.domain.product.repository;

import com.example.repick.domain.product.entity.ProductCart;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ProductCartRepository extends JpaRepository<ProductCart, Long> {
Optional<ProductCart> findByUserIdAndProductId(Long userId, Long productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ List<GetProductThumbnail> findHighestDiscountProducts(
Long userId);

List<GetProductThumbnail> findMainPageRecommendation(Long cursorId, Integer pageSize, Long userId, Gender gender);

List<GetProductThumbnail> findLikedProducts(String category, Long cursorId, Integer pageSize, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ public List<GetProductThumbnail> findHighestDiscountProducts(
.collect(Collectors.toList());
}

@Override
public List<GetProductThumbnail> findLikedProducts(String category, Long cursorId, Integer pageSize, Long userId) {
return jpaQueryFactory
.select(Projections.constructor(GetProductThumbnail.class,
product.id,
product.thumbnailImageUrl,
product.productName,
product.price,
product.discountRate,
product.brandName,
product.qualityRate.stringValue(),
productLike.id.isNotNull()))
.from(product)
.leftJoin(productLike)
.on(product.id.eq(productLike.productId)
.and(productLike.userId.eq(userId)))
.leftJoin(productCategory)
.on(product.id.eq(productCategory.product.id))
.where(
likeFilter(userId),
categoryFilter(category),
ltProductId(cursorId),
deletedFilter())
.orderBy(product.discountRate.desc())
.limit(pageSize)
.fetch()
.stream()
.distinct()
.collect(Collectors.toList());
}

private BooleanExpression likeFilter(Long userId) {
return productLike.userId.eq(userId);
}

private BooleanExpression genderFilter(String gender) {
return gender != null ? product.gender.eq(Gender.fromValue(gender)) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ProductService {
private final S3UploadService s3UploadService;
private final UserRepository userRepository;
private final ProductLikeRepository productLikeRepository;
private final ProductCartRepository productCartRepository;

private String uploadImage(List<MultipartFile> images, Product product) {
String thumbnailGeneratedUrl = null;
Expand Down Expand Up @@ -148,16 +149,6 @@ public List<GetProductThumbnail> getMainPageRecommendation(String gender, Long c
return productRepository.findMainPageRecommendation(cursorId, pageSize, user.getId(), Gender.fromValue(gender));
}

public Boolean likeProduct(Long productId) {
User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new UsernameNotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));

productLikeRepository.findByUserIdAndProductId(user.getId(), productId)
.ifPresentOrElse(productLikeRepository::delete, () -> productLikeRepository.save(ProductLike.of(user.getId(), productId)));

return true;
}

public List<GetProductThumbnail> getLatest(String gender, String category, List<String> styles, Long minPrice, Long maxPrice, List<String> brandNames, List<String> qualityRates, List<String> sizes, Long cursorId, Integer pageSize) {
User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new UsernameNotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));
Expand Down Expand Up @@ -193,4 +184,33 @@ public List<GetProductThumbnail> getHighestDiscount(String gender, String catego

return productRepository.findHighestDiscountProducts(gender, category, styles, minPrice, maxPrice, brandNames, qualityRates, sizes, cursorId, pageSize, user.getId());
}

public Boolean toggleLike(Long productId) {
User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new UsernameNotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));

productLikeRepository.findByUserIdAndProductId(user.getId(), productId)
.ifPresentOrElse(productLikeRepository::delete, () -> productLikeRepository.save(ProductLike.of(user.getId(), productId)));

return true;
}

public List<GetProductThumbnail> getLiked(String category, Long cursorId, Integer pageSize) {
User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new UsernameNotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));

if (pageSize == null) pageSize = 4;

return productRepository.findLikedProducts(category, cursorId, pageSize, user.getId());
}

public Boolean toggleCart(Long productId) {
User user = userRepository.findByProviderId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new UsernameNotFoundException("μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));

productCartRepository.findByUserIdAndProductId(user.getId(), productId)
.ifPresentOrElse(productCartRepository::delete, () -> productCartRepository.save(ProductCart.of(user.getId(), productId)));

return true;
}
}

0 comments on commit 99efdd5

Please sign in to comment.