Review Update Service

In this lesson, we'll implement the functionality to update restaurant reviews in our service layer.

Implementing the Update Service

The update functionality requires careful consideration of several aspects:

  • User ownership verification
  • Time-based edit restrictions
  • Photo handling
  • Rating recalculation

Let's examine the implementation of the updateReview method in our ReviewServiceImpl:

@Override public Review updateReview(User user, String restaurantId, String reviewId, ReviewCreateUpdateRequest updatedReview) { // Get the restaurant or throw an exception if not found Restaurant restaurant = getRestaurantOrThrow(restaurantId); String currentUserId = user.getId(); // Find the review and verify ownership List<Review> reviews = restaurant.getReviews(); Review existingReview = reviews.stream() .filter(r -> r.getId().equals(reviewId) && r.getWrittenBy().getId().equals(currentUserId)) .findFirst() .orElseThrow(() -> new ResourceNotFoundException("Review not found")); // Verify the 48-hour edit window if (LocalDateTime.now().isAfter(existingReview.getDatePosted().plusHours(48))) { throw new ReviewNotAllowedException("Review can no longer be edited (48-hour limit exceeded)"); } // Update the review content existingReview.setContent(updatedReview.getContent()); existingReview.setRating(updatedReview.getRating()); existingReview.setLastEdited(LocalDateTime.now()); // Update photos existingReview.setPhotos(updatedReview.getPhotoIds().stream() .map(url -> { Photo photo = new Photo(); photo.setUrl(url); photo.setUploadDate(LocalDateTime.now()); return photo; }).collect(Collectors.toList())); // Recalculate restaurant's average rating updateRestaurantAverageRating(restaurant); // Save and return the updated review Restaurant savedRestaurant = restaurantRepository.save(restaurant); return savedRestaurant.getReviews().stream() .filter(r -> r.getId().equals(reviewId)) .findFirst() .orElseThrow(() -> new RuntimeException("Error retrieving updated review")); }

Summary

  • Service implements review updates with ownership verification and time restrictions
  • Reviews can only be edited within 48 hours of posting
  • Photos and ratings are updated along with review content
  • Restaurant average rating is recalculated after updates
© 2026 Devtiro Ltd. All rights reserved