public async Task <IActionResult> AddTvSeriesRating(int tvSeriesId, [FromBody] AddTvSeriesRatingBindingModel tvSeriesRatingBindingModel) { var user = User.Identity.Name; var result = await _ratingService.AddTvSeriesUserRating(user, tvSeriesId, tvSeriesRatingBindingModel); if (result.ErrorOccurred) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <ResponseDto <BaseModelDto> > AddTvSeriesUserRating(string userLogged, int tvSeriesId, AddTvSeriesRatingBindingModel tvSeriesRatingBindingModel) { var response = new ResponseDto <BaseModelDto>(); var tvSeriesExists = await _tvSeriesRepository.ExistAsync(x => x.Id == tvSeriesId); if (!tvSeriesExists) { response.AddError(Model.TvShow, Error.tvShow_NotFound); return(response); } var ratingExists = await _tvSeriesUserRatingRepository.ExistAsync(x => x.UserId == userLogged && x.TvShowId == tvSeriesId); if (ratingExists) { response.AddError(Model.Rating, Error.rating_Already_Added); return(response); } var userRating = _mapper.Map <TvSeriesUserRating>(tvSeriesRatingBindingModel); userRating.Average = (userRating.Effects + userRating.Music + userRating.Story) / 3; userRating.TvShowId = tvSeriesId; userRating.UserId = userLogged; var result = await _tvSeriesUserRatingRepository.AddAsync(userRating); if (!result) { response.AddError(Model.Rating, Error.rating_Adding); return(response); } else { if (userLogged != null) { AddNotificationBindingModel addNotificationBindingModel = new AddNotificationBindingModel(); addNotificationBindingModel.Type = "ratedTvSeries"; await _notificationService.AddNotification(addNotificationBindingModel, tvSeriesId, userLogged); } } response = await AddTvSeriesRating(userRating, response); return(response); }