Skip to content

Commit

Permalink
[feat] 여행 후기 리뷰 평점 수정시, 이전 점수를 뺀 뒤 다시 평점 계산하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
meena2003 committed Feb 7, 2024
1 parent 0c5a572 commit 72a2734
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void modifyTripRecordReview(
validateRightMemberAccess(loginMember, tripRecordReview);
isContentAlreadyRegistered(tripRecordReview);

TripRecord tripRecord = getTripRecordById(tripRecordReview.getTripRecord().getId());
tripRecord.calculateAverageRating(requestDto.ratingScore());
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
Expand Up @@ -67,11 +67,11 @@ public class TripRecord extends BaseTimeEntity {

@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 @@ -98,42 +98,42 @@ 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++;
Expand All @@ -150,6 +150,16 @@ public void calculateAverageRating(double ratingScore) {
}
}

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 @@ -172,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;
}
}

0 comments on commit 72a2734

Please sign in to comment.