77
88namespace TableBooking . Api . Services
99{
10+ using Microsoft . AspNetCore . Http . HttpResults ;
11+
1012 public class RatingService : IRatingService
1113 {
1214 private IUnitOfWork _unitOfWork ;
@@ -19,32 +21,52 @@ public RatingService(IUnitOfWork unitOfWork, IRatingConverter ratingConverter)
1921 }
2022 public async Task < IActionResult > CreateRatingAsync ( CreateRatingDto dto )
2123 {
22- var user = await _unitOfWork . UserRepository . GetByIdAsync ( dto . AppUserId ) ;
24+ var user = await _unitOfWork . UserRepository . GetByIdAsync ( dto . AppUserId ! . Value ) ;
2325 var restaurant = await _unitOfWork . RestaurantRepository . GetByIdAsync ( dto . RestaurantId ) ;
24- if ( user == null || restaurant == null ) return new BadRequestObjectResult ( string . Empty ) ;
26+
27+ if ( user == null )
28+ return new NotFoundObjectResult ( $ "User with id { dto . AppUserId . Value } not found.") ;
29+
30+ if ( restaurant == null )
31+ return new NotFoundObjectResult ( $ "Restaurant with id { dto . RestaurantId } not found.") ;
32+
33+ if ( dto . RatingStars < 1 || dto . RatingStars > 5 )
34+ {
35+ return new BadRequestObjectResult ( "Rating must be between 1 and 5." ) ;
36+ }
37+
38+ var existingRating = await _unitOfWork . RatingRepository . GetRatingByUserIdAsync ( dto . AppUserId . Value , dto . RestaurantId ) ;
2539
40+ if ( existingRating != null )
41+ {
42+ return new BadRequestObjectResult ( "You have already submitted a review for this restaurant." ) ;
43+ }
44+
2645 var rating = new Rating
2746 {
47+ Id = Guid . NewGuid ( ) ,
2848 RatingStars = dto . RatingStars ,
29- Comment = dto . Comment ,
30- DateOfRating = dto . DateOfRating ,
49+ Comment = dto . Comment ?? string . Empty ,
50+ DateOfRating = DateTime . UtcNow ,
3151 RestaurantId = dto . RestaurantId ,
32- AppUserId = dto . AppUserId
52+ AppUserId = dto . AppUserId . Value
3353 } ;
3454
3555 await _unitOfWork . RatingRepository . InsertAsync ( rating ) ;
3656 await _unitOfWork . SaveChangesAsync ( ) ;
3757 var ratings = await _unitOfWork . RatingRepository . GetRatingsAsync ( dto . RestaurantId ) ;
38- var numberOfRaitings = ratings . Count ( ) ;
39- var result = 0d ;
58+ var enumerable = ratings . ToList ( ) ;
59+ var numberOfRatings = enumerable . Count ;
4060
41- if ( numberOfRaitings > 0 && numberOfRaitings % 5 == 0 )
42- {
43- result = ratings . Select ( x => x . RatingStars ) . Average ( ) ;
44- restaurant . Rating = result ;
45- await _unitOfWork . RestaurantRepository . Update ( restaurant ) ;
46- await _unitOfWork . SaveChangesAsync ( ) ;
47- }
61+ if ( numberOfRatings <= 0 ) return new OkObjectResult ( _ratingConverter . RatingToRatingDto ( rating ) ) ;
62+
63+ var averageRating = enumerable . Select ( x => x . RatingStars ) . Average ( ) ;
64+ var roundedRating = Math . Round ( averageRating , 0 , MidpointRounding . AwayFromZero ) ;
65+
66+
67+ restaurant . Rating = ( int ) roundedRating ;
68+ await _unitOfWork . RestaurantRepository . Update ( restaurant ) ;
69+ await _unitOfWork . SaveChangesAsync ( ) ;
4870
4971 return new OkObjectResult ( _ratingConverter . RatingToRatingDto ( rating ) ) ;
5072 }
0 commit comments