Skip to content

Commit

Permalink
[feat] 여행 후기 리뷰 작성 가능한 여부를 boolean 값(myRatingScore)으로 반환하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
meena2003 authored and BackInGone committed Jan 25, 2024
1 parent 059d303 commit ad7dc54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ public record EmptyTripRecordReviewResponseDto(

Long totalCount,
Long myTripRecordReviewId,
Float myRatingScore
Float myRatingScore,
boolean canRegisterContent

) implements LatestReviewResponseDto {
public static EmptyTripRecordReviewResponseDto fromData(Long totalCount, Long myTripRecordReviewId, Float myRatingScore) {
return new EmptyTripRecordReviewResponseDto(totalCount, myTripRecordReviewId, myRatingScore);
public static EmptyTripRecordReviewResponseDto fromData(Long totalCount, Long myTripRecordReviewId, Float myRatingScore, boolean canRegisterContent) {
return new EmptyTripRecordReviewResponseDto(totalCount, myTripRecordReviewId, myRatingScore, canRegisterContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ public record LatestTripRecordReviewResponseDto(
Long totalCount,
TripRecordReviewResponseDto latestTripRecordReview,
Long myTripRecordReviewId,
Float myRatingScore
Float myRatingScore,
boolean canRegisterContent

) implements LatestReviewResponseDto {
public static LatestTripRecordReviewResponseDto fromEntity(
Long totalCount,
TripRecordReview tripRecordReview,
Long myTripRecordReviewId,
Float myRatingScore) {
Float myRatingScore,
boolean canRegisterContent) {
return new LatestTripRecordReviewResponseDto(
totalCount,
TripRecordReviewResponseDto.fromEntity(tripRecordReview, false),
myTripRecordReviewId,
myRatingScore);
myRatingScore,
canRegisterContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.haejwo.tripcometrue.domain.review.triprecordreview.entity.TripRecordReview;
import com.haejwo.tripcometrue.domain.review.triprecordreview.exception.*;
import com.haejwo.tripcometrue.domain.review.triprecordreview.repository.TripRecordReviewRepository;
import com.haejwo.tripcometrue.domain.tripplan.entity.TripPlan;
import com.haejwo.tripcometrue.domain.tripplan.repository.TripPlanRepository;
import com.haejwo.tripcometrue.domain.triprecord.entity.TripRecord;
import com.haejwo.tripcometrue.domain.triprecord.exception.TripRecordNotFoundException;
import com.haejwo.tripcometrue.domain.triprecord.repository.triprecord.TripRecordRepository;
Expand All @@ -32,6 +34,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -45,6 +48,7 @@ public class TripRecordReviewService {
private final TripRecordReviewRepository tripRecordReviewRepository;
private final TripRecordRepository tripRecordRepository;
private final MemberRepository memberRepository;
private final TripPlanRepository tripPlanRepository;

// FIXME: 1/18/24 ratingScore @NotNull과 상충되는 부분 수정하기
@Transactional
Expand Down Expand Up @@ -181,20 +185,36 @@ public LatestReviewResponseDto getLatestReview(PrincipalDetails principalDetails
getTripRecordById(tripRecordId);
Member loginMember = getMember(principalDetails);

Optional<TripRecordReview> latestReview = tripRecordReviewRepository
.findTopByTripRecordIdOrderByCreatedAtDesc(tripRecordId);

Long totalCount = tripRecordReviewRepository.countByTripRecordId(tripRecordId);
Optional<TripRecordReview> myReview = tripRecordReviewRepository
.findByMemberAndTripRecordId(loginMember, tripRecordId);

String content = myReview.map(TripRecordReview::getContent).orElse(null);
Long myReviewId = myReview.map(TripRecordReview::getId).orElse(null);
Float myRatingScore = myReview.map(TripRecordReview::getRatingScore).orElse(0f);

Optional<TripRecordReview> latestReview = tripRecordReviewRepository
.findTopByTripRecordIdOrderByCreatedAtDesc(tripRecordId);
boolean isReviewable = canWriteReview(loginMember, tripRecordId, content, myRatingScore);

if (latestReview.isEmpty()) {
return EmptyTripRecordReviewResponseDto.fromData(totalCount, myReviewId, myRatingScore);
return EmptyTripRecordReviewResponseDto.fromData(totalCount, myReviewId, myRatingScore, isReviewable);
}
return LatestTripRecordReviewResponseDto.fromEntity(totalCount, latestReview.get(), myReviewId, myRatingScore);
return LatestTripRecordReviewResponseDto.fromEntity(totalCount, latestReview.get(), myReviewId, myRatingScore, isReviewable);
}

private boolean canWriteReview(Member loginMember, Long tripRecordId, String content, float score) {

if (content != null || score == 0f) {
return false;
}

Optional<TripPlan> latestTripPlan = tripPlanRepository.findByMemberIdAndTripRecordId(loginMember, tripRecordId);
return latestTripPlan.map(tripPlan ->
tripPlan.getTripEndDay().isBefore(LocalDate.now())
)
.orElse(false);
}

public SimpleTripRecordResponseDto getTripRecordReview(PrincipalDetails principalDetails, Long tripRecordReviewId) {
Expand Down Expand Up @@ -229,7 +249,6 @@ public TripRecordReviewListResponseDto getTripRecordReviewList(
return TripRecordReviewResponseDto.fromEntity(
tripRecordReview,
hasLiked

);
}
).toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.haejwo.tripcometrue.domain.tripplan.repository;

import com.haejwo.tripcometrue.domain.member.entity.Member;
import com.haejwo.tripcometrue.domain.tripplan.entity.TripPlan;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface TripPlanRepository extends JpaRepository<TripPlan, Long> {

Page<TripPlan> findByMemberId(Long memberId, Pageable pageable);

@Query("select tp from TripPlan tp where tp.member = :member and tp.referencedBy = :tripRecordId order by tp.tripEndDay asc limit 1")
Optional<TripPlan> findByMemberIdAndTripRecordId(@Param("member") Member loginMember, @Param("tripRecordId") Long tripRecordId);
}

0 comments on commit ad7dc54

Please sign in to comment.