public async Task <Result <RatingModel, Error> > Add(Guid activityId, CreateRatingModel model)
        {
            model.UserId = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "userId").Value);

            var activity = await _activitiesRepository.GetById(activityId);

            if (activity == null)
            {
                return(Result.Failure <RatingModel, Error>(ErrorsList.UnavailableActivity));
            }

            var rating = _mapper.Map <Rating>(model);

            activity.AddRating(rating);

            var user = await _userRepository.GetById(rating.UserId);

            var notification = new Notification(activityId,
                                                DateTime.Now,
                                                $"{user.Username} has rated activity {activity.Name} with {rating.Stars} stars."
                                                );

            activity.AddNotification(notification);

            _activitiesRepository.Update(activity);

            await _activitiesRepository.SaveChanges();

            return(Result.Success <RatingModel, Error>(_mapper.Map <RatingModel>(rating)));
        }
        public async Task <IActionResult> Post([FromRoute] Guid activityId, [FromBody] CreateRatingModel model)
        {
            var(_, isFailure, value, error) = await _ratingsService.Add(activityId, model);

            if (isFailure)
            {
                return(BadRequest(error));
            }

            return(Created(value.Id.ToString(), null));
        }
        public async Task AddRatingToInvalidActivity()
        {
            var createRatingModel = new CreateRatingModel
            {
                Stars = 5
            };

            //Act
            var response = await HttpClient.PostAsJsonAsync($"api/v1/activities/{new Guid()}/ratings",
                                                            createRatingModel);

            //Assert
            response.IsSuccessStatusCode.Should().BeFalse();
        }
        public async Task AddRatingToActivity()
        {
            var activityType = ActivityTypeFactory.Default();
            var activity     = ActivityFactory.Default(CityId, activityType.Id);

            await ExecuteDatabaseAction(async (doFestContext) =>
            {
                await doFestContext.ActivityTypes.AddAsync(activityType);
                await doFestContext.Activities.AddAsync(activity);
                await doFestContext.SaveChangesAsync();
            });

            var createRatingModel = new CreateRatingModel
            {
                Stars = 5
            };

            //Act
            var response = await HttpClient.PostAsJsonAsync($"api/v1/activities/{activity.Id}/ratings",
                                                            createRatingModel);

            //Assert
            response.IsSuccessStatusCode.Should().BeTrue();

            var    createdRatingId = response.Headers.Location.OriginalString;
            Rating existingRating  = null;

            await ExecuteDatabaseAction(async (doFestContext) =>
            {
                var existingActivity = await doFestContext.Activities
                                       .Include(entity => entity.Ratings)
                                       .FirstAsync(entity => entity.Id == activity.Id);

                existingRating = existingActivity.Ratings.FirstOrDefault();
            });

            existingRating.Should().NotBeNull();
            existingRating.Id.Should().Be(createdRatingId);
        }
 public static CreateRatingModel WithStars(this CreateRatingModel model, int stars)
 {
     model.Stars = stars;
     return(model);
 }