7
7
8
8
namespace TableBooking . Api . Services
9
9
{
10
+ using Microsoft . AspNetCore . Http . HttpResults ;
11
+
10
12
public class RatingService : IRatingService
11
13
{
12
14
private IUnitOfWork _unitOfWork ;
@@ -19,32 +21,52 @@ public RatingService(IUnitOfWork unitOfWork, IRatingConverter ratingConverter)
19
21
}
20
22
public async Task < IActionResult > CreateRatingAsync ( CreateRatingDto dto )
21
23
{
22
- var user = await _unitOfWork . UserRepository . GetByIdAsync ( dto . AppUserId ) ;
24
+ var user = await _unitOfWork . UserRepository . GetByIdAsync ( dto . AppUserId ! . Value ) ;
23
25
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 ) ;
25
39
40
+ if ( existingRating != null )
41
+ {
42
+ return new BadRequestObjectResult ( "You have already submitted a review for this restaurant." ) ;
43
+ }
44
+
26
45
var rating = new Rating
27
46
{
47
+ Id = Guid . NewGuid ( ) ,
28
48
RatingStars = dto . RatingStars ,
29
- Comment = dto . Comment ,
30
- DateOfRating = dto . DateOfRating ,
49
+ Comment = dto . Comment ?? string . Empty ,
50
+ DateOfRating = DateTime . UtcNow ,
31
51
RestaurantId = dto . RestaurantId ,
32
- AppUserId = dto . AppUserId
52
+ AppUserId = dto . AppUserId . Value
33
53
} ;
34
54
35
55
await _unitOfWork . RatingRepository . InsertAsync ( rating ) ;
36
56
await _unitOfWork . SaveChangesAsync ( ) ;
37
57
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 ;
40
60
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 ( ) ;
48
70
49
71
return new OkObjectResult ( _ratingConverter . RatingToRatingDto ( rating ) ) ;
50
72
}
0 commit comments