Skip to content

Commit

Permalink
Merge pull request #221 from HyunJunSon/refator/like
Browse files Browse the repository at this point in the history
refactor:: 즐겨찾기 목록 요청 리팩토링
  • Loading branch information
linglong67 authored Feb 29, 2024
2 parents 9614bd1 + 637b360 commit 6e261b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import com.kernel360.likes.entity.Like;
import com.kernel360.likes.repository.LikeRepository;
import com.kernel360.member.service.MemberService;
import com.kernel360.product.code.ProductsErrorCode;
import com.kernel360.product.dto.ProductDto;
import com.kernel360.product.entity.Product;
import com.kernel360.product.repository.ProductRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;


@Service
Expand All @@ -28,10 +29,8 @@ public class LikeService {
@Transactional
public void heartOn(Long productNo, String token) {
Long memberNo = memberService.findMemberByToken(token).memberNo();
Product product = productRepository.findById(productNo)
.orElseThrow(() -> new BusinessException(ProductsErrorCode.NOT_FOUND_PRODUCT));

likeRepository.save(Like.of(memberNo, product.getProductNo()));
likeRepository.save(Like.of(memberNo, productNo));
}

@Transactional
Expand All @@ -46,11 +45,14 @@ public void heartOff(Long productNo, String token) {
@Transactional(readOnly = true)
public Page<ProductDto> findAllLikes(String token, Pageable pageable) {
Long memberNo = memberService.findMemberByToken(token).memberNo();

return likeRepository.findAllByMemberNo(memberNo, pageable)
.map(like -> productRepository.findById(like.getId())
.orElseThrow(() -> new BusinessException(ProductsErrorCode.NOT_FOUND_PRODUCT)))
.map(ProductDto::from);

Page<Like> likesPage = likeRepository.findAllByMemberNo(memberNo, pageable);
List<ProductDto> productDtos = likesPage.getContent().stream()
.map(like -> productRepository.findById(like.getProductNo()))
.filter(Optional::isPresent)
.map(Optional::get)
.map(ProductDto::from)
.toList();

return new PageImpl<>(productDtos, pageable, productDtos.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

@Getter
@Entity
@Table(name = "likes")
@Table(name = "likes", uniqueConstraints = {
@UniqueConstraint(columnNames = {"member_no", "product_no"})
})
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Like extends BaseEntity {
@Id
Expand Down

0 comments on commit 6e261b8

Please sign in to comment.