Commit 8eb07a02 authored by Ondrej Sakači's avatar Ondrej Sakači 💯
Browse files

added overal rating for trips

parent e48e5da4
......@@ -116,6 +116,7 @@ public class DatabaseSeeder implements
Enrollment enrollment = author.getTravel_journal().getEnrollments().get(0);
TripReview tripReview = new TripReview("Really good trip, love it <3", LocalDateTime.now(), 5, author, enrollment.getTrip(),enrollment);
tripReviewDao.persist(tripReview);
updateTripRating(tripReview.getTrip(), tripReview.getRating());
}
//2.tripReview from Milan
......@@ -124,6 +125,7 @@ public class DatabaseSeeder implements
Enrollment enrollment = author.getTravel_journal().getEnrollments().get(1);
TripReview tripReview = new TripReview("it was good, but the whether was really bad :( ", LocalDateTime.now(), 3, author, enrollment.getTrip(),enrollment);
tripReviewDao.persist(tripReview);
updateTripRating(tripReview.getTrip(), tripReview.getRating());
}
//3.tripReview from Jan
......@@ -132,6 +134,7 @@ public class DatabaseSeeder implements
Enrollment enrollment = author.getTravel_journal().getEnrollments().get(0);
TripReview tripReview = new TripReview("it was the best trip of my entire life! Don't be afraid to enrol ;) ", LocalDateTime.now(), 3, author, enrollment.getTrip(),enrollment);
tripReviewDao.persist(tripReview);
updateTripRating(tripReview.getTrip(), tripReview.getRating());
}
}
......@@ -681,4 +684,17 @@ public class DatabaseSeeder implements
return enrollment;
}
private void updateTripRating(Trip trip, double rating) {
long noReviews;
if(trip.getTripReviews() == null) {
noReviews = 1;
}
else {
noReviews = trip.getTripReviews().size();
}
double currentRating = trip.getRating();
trip.setRating((currentRating*(noReviews) + rating)/(noReviews+1));
tripDao.update(trip);
}
}
......@@ -8,6 +8,7 @@ import cz.cvut.fel.rsp.travelandwork.exception.NotAllowedException;
import cz.cvut.fel.rsp.travelandwork.exception.NotFoundException;
import cz.cvut.fel.rsp.travelandwork.exception.UnauthorizedException;
import cz.cvut.fel.rsp.travelandwork.model.Enrollment;
import cz.cvut.fel.rsp.travelandwork.model.Trip;
import cz.cvut.fel.rsp.travelandwork.model.TripReview;
import cz.cvut.fel.rsp.travelandwork.model.TripSession;
import cz.cvut.fel.rsp.travelandwork.security.SecurityUtils;
......@@ -58,11 +59,29 @@ public class TripReviewService {
tripReview.setAuthor(SecurityUtils.getCurrentUser());
tripReview.setEnrollment(enrollment);
tripReviewDao.persist(tripReview);
Trip trip = enrollment.getTrip();
long noReviews = trip.getTripReviews().size();
double currentRating = trip.getRating();
trip.setRating((currentRating*(noReviews-1) + tripReview.getRating())/noReviews);
tripDao.update(trip);
}
@Transactional
public void update(TripReview tripReview) {
Objects.requireNonNull(tripReview);
TripReview old = tripReviewDao.find(tripReview.getId());
double oldRating = old.getRating();
double newRating = tripReview.getRating();
Trip trip = old.getTrip();
double currentRating = trip.getRating();
long noReviews = trip.getTripReviews().size();
trip.setRating((currentRating*(noReviews) + newRating - oldRating)/noReviews);
tripDao.update(trip);
tripReviewDao.update(tripReview);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment