Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 여행 후기(TripRecord)의 평점(averageRating) 계산 로직 추가 #146

Merged
merged 5 commits into from
Feb 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public EvaluateTripRecordReviewResponseDto saveRatingScore(

isAlreadyTripRecordReviewExists(loginMember, tripRecord);

tripRecord.calculateAverageRating(requestDto.ratingScore());
return EvaluateTripRecordReviewResponseDto
.fromEntity(tripRecordReviewRepository.save(requestDto.toEntity(loginMember, tripRecord)));
}
Expand Down Expand Up @@ -103,6 +104,9 @@ public void modifyTripRecordReview(
validateRightMemberAccess(loginMember, tripRecordReview);
isContentAlreadyRegistered(tripRecordReview);

TripRecord tripRecord = tripRecordReview.getTripRecord();
tripRecord.updateAverageRating(requestDto.ratingScore(), tripRecordReview);

tripRecordReview.update(requestDto, loginMember);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.haejwo.tripcometrue.domain.triprecord.entity;

import com.haejwo.tripcometrue.domain.member.entity.Member;
import com.haejwo.tripcometrue.domain.review.triprecordreview.entity.TripRecordReview;
import com.haejwo.tripcometrue.domain.store.entity.TripRecordStore;
import com.haejwo.tripcometrue.domain.triprecord.dto.request.TripRecordRequestDto;
import com.haejwo.tripcometrue.domain.triprecord.entity.type.ExpenseRangeType;
Expand Down Expand Up @@ -61,14 +62,16 @@ public class TripRecord extends BaseTimeEntity {
@OneToMany(mappedBy = "tripRecord", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private List<TripRecordStore> tripRecordStores = new ArrayList<>();

@OneToMany(mappedBy = "tripRecord", cascade = CascadeType.REMOVE)
private List<TripRecordReview> tripRecordReviews = new ArrayList<>();

@Builder
private TripRecord(Long id, String title, String content, ExpenseRangeType expenseRangeType,
String countries, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays,
Double averageRating, Integer viewCount, Integer storeCount, Integer reviewCount,
Integer commentCount, List<TripRecordSchedule> tripRecordSchedules,
List<TripRecordTag> tripRecordTags, List<TripRecordImage> tripRecordImages,
List<TripRecordStore> tripRecordStores, Member member) {
String countries, LocalDate tripStartDay, LocalDate tripEndDay, Integer totalDays,
Double averageRating, Integer viewCount, Integer storeCount, Integer reviewCount,
Integer commentCount, List<TripRecordSchedule> tripRecordSchedules,
List<TripRecordTag> tripRecordTags, List<TripRecordImage> tripRecordImages,
List<TripRecordStore> tripRecordStores, Member member) {
this.id = id;
this.title = title;
this.content = content;
Expand All @@ -95,48 +98,68 @@ public void update(TripRecordRequestDto requestDto) {
this.expenseRangeType = requestDto.expenseRangeType();
this.tripStartDay = requestDto.tripStartDay();
this.tripEndDay = requestDto.tripEndDay();
this.totalDays = (int)ChronoUnit.DAYS.between(this.tripStartDay, this.tripEndDay)+1;
this.totalDays = (int) ChronoUnit.DAYS.between(this.tripStartDay, this.tripEndDay) + 1;
this.countries = requestDto.countries();
}

public void incrementViewCount() {
if(this.viewCount == null) {
if (this.viewCount == null) {
this.viewCount = 1;
} else {
this.viewCount++;
}
}

public void incrementStoreCount() {
if(this.storeCount == null) {
if (this.storeCount == null) {
this.storeCount = 1;
} else {
this.storeCount++;
}
}

public void decrementStoreCount() {
if(this.storeCount > 0) {
if (this.storeCount > 0) {
this.storeCount--;
}
}

public void incrementReviewCount() {
if(this.reviewCount == null) {
if (this.reviewCount == null) {
this.reviewCount = 1;
} else {
this.reviewCount++;
}
}

public void incrementCommentCount() {
if(this.commentCount == null) {
if (this.commentCount == null) {
this.commentCount = 1;
} else {
this.commentCount++;
}
}

public void calculateAverageRating(double ratingScore) {
if (tripRecordReviews.isEmpty()) {
averageRating = ratingScore;
} else {
double totalRating = averageRating * tripRecordReviews.size();
totalRating += ratingScore;
averageRating = totalRating / (tripRecordReviews.size() + 1);
}
}

public void updateAverageRating(double newRatingScore, TripRecordReview tripRecordReview) {
double totalRating = averageRating * tripRecordReviews.size();
double previousRatingScore = tripRecordReview.getRatingScore();

totalRating -= previousRatingScore;
totalRating += newRatingScore;

averageRating = totalRating / tripRecordReviews.size();
}

public void decreaseCommentCount(int count) {
this.commentCount -= count;
}
Expand All @@ -159,6 +182,6 @@ private int calculateTotalDays() {
if (this.tripStartDay == null || this.tripEndDay == null) {
return 0;
}
return (int) ChronoUnit.DAYS.between(this.tripStartDay, this.tripEndDay)+1;
return (int) ChronoUnit.DAYS.between(this.tripStartDay, this.tripEndDay) + 1;
}
}
Loading