Skip to content

Commit

Permalink
Merge pull request #43 from Central-MakeUs/14-필터를-조회한다
Browse files Browse the repository at this point in the history
Feat(#41): �리뷰 조회할 때 필터값 열람 여부 추가
  • Loading branch information
tmddus2 authored Aug 11, 2024
2 parents 0a23242 + 1317d19 commit 1e543e9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public SuccessResponse<IOSFilterDetailDto> getIOSFilter(Long id, Long filterId)

@GetMapping("/{filterId}/reviews")
public SuccessResponse<FilterReviewDto> getReviews(Long id, Long filterId) {
return SuccessResponse.of(filterService.getReviews(filterId));
return SuccessResponse.of(filterService.getReviews(id, filterId));
}

@PostMapping("/{filterId}/likes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
public record FilterReviewDto(
@Schema(description = "평균 퓨어지수")
int avg,
@Schema(description = "사용자가 필터를 열람한 적 있는지 여부")
boolean hasViewed,
@Schema(description = "리뷰")
List<ReviewDto> reviews
) {
public static FilterReviewDto of(int avg, List<ReviewDto> reviews) {
public static FilterReviewDto of(int avg, boolean hasViewed, List<ReviewDto> reviews) {
return FilterReviewDto.builder()
.avg(avg)
.hasViewed(hasViewed)
.reviews(reviews)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.purithm.domain.filter.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class UserFilterLog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Column(name = "user_id")
Long userId;

@Column(name = "filter_id")
Long filterId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.purithm.domain.filter.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.purithm.domain.filter.entity.UserFilterLog;

@Repository
public interface UserFilterLogRepository extends JpaRepository<UserFilterLog, Long> {
boolean existsByFilterIdAndUserId(Long filterId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import com.example.purithm.domain.filter.entity.IOSFilterDetail;
import com.example.purithm.domain.filter.entity.OS;
import com.example.purithm.domain.filter.entity.Tag;
import com.example.purithm.domain.filter.entity.UserFilterLog;
import com.example.purithm.domain.filter.repository.AOSFilterDetailRepository;
import com.example.purithm.domain.filter.repository.FilterLikeRepository;
import com.example.purithm.domain.filter.repository.IOSFilterDetailRepository;
import com.example.purithm.domain.filter.repository.FilterRepository;
import com.example.purithm.domain.filter.repository.UserFilterLogRepository;
import com.example.purithm.domain.review.repository.ReviewRepository;
import com.example.purithm.domain.user.entity.User;
import com.example.purithm.domain.user.repository.UserRepository;
Expand All @@ -48,6 +50,7 @@ public class FilterService {
private final UserRepository userRepository;
private final FilterLikeRepository filterLikeRepository;
private final ReviewRepository reviewRepository;
private final UserFilterLogRepository userFilterLogRepository;


public FilterListDto getFilters(Long id, int page, int size, OS os, Tag tag, String sortedBy) {
Expand Down Expand Up @@ -117,24 +120,38 @@ public FilterDetailDto getFilterDetail(Long id, Long filterId) {
.build();
}

@Transactional
public AOSFilterDetailDto getFilterAOSDetail(Long filterId, Long userId) {
Filter filter = filterRepository.findById(filterId)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));
AOSFilterDetail aosFilterDetail = aOSFilterDetailRepository.findById(filterId)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));

saveUserFilterLog(filterId, userId);

return AOSFilterDetailDto.of(filter, isLike(filterId, userId), aosFilterDetail);
}

@Transactional
public IOSFilterDetailDto getFilterIOSDetail(Long filterId, Long userId) {
Filter filter = filterRepository.findById(filterId)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));
IOSFilterDetail iosFilterDetail = iOSFilterDetailRepository.findById(filterId)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));

saveUserFilterLog(filterId, userId);

return IOSFilterDetailDto.of(filter, isLike(filterId, userId), iosFilterDetail);
}

private void saveUserFilterLog(Long filterId, Long userId) {
if (!userFilterLogRepository.existsByFilterIdAndUserId(filterId, userId)) {
UserFilterLog log = UserFilterLog.builder()
.userId(userId).filterId(filterId).build();
userFilterLogRepository.save(log);
}
}

public void likeFilter(Long userId, Long filterId) {
if (!filterLikeRepository.existsByFilterIdAndUserId(filterId, userId)) {
Filter filter = filterRepository.findById(filterId)
Expand All @@ -155,12 +172,13 @@ public void dislikeFilter(Long userId, Long filterId) {
}

@Transactional
public FilterReviewDto getReviews(Long filterId) {
public FilterReviewDto getReviews(Long userId, Long filterId) {
Integer avg = reviewRepository.getAverage(filterId);
List<ReviewDto> reviews = reviewRepository.findAllByFilterId(filterId)
.stream().map(ReviewDto::of).toList();
boolean hasViewed = userFilterLogRepository.existsByFilterIdAndUserId(filterId, userId);

return FilterReviewDto.of(Optional.ofNullable(avg).orElse(0), reviews);
return FilterReviewDto.of(Optional.ofNullable(avg).orElse(0), hasViewed, reviews);
}

public FilterDescriptionDto getFilterDescriptions(Long filterId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.example.purithm.domain.review.entity.Review;

@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {
List<Review> findAllByFilterId(Long filterId);
@Query("select avg(r.pureDegree) as avg\n"
Expand Down

0 comments on commit 1e543e9

Please sign in to comment.