public async Task <IActionResult> TakeRatingAsync([FromBody] RatingPutViewModel model) { if (ModelState.IsValid) { model.AuthorId = User.FindFirst(ClaimTypes.NameIdentifier).Value; await _ratingService.CreateAsync(model); return(NoContent()); } else { return(BadRequest()); } }
public async Task CreateAsync(RatingPutViewModel model) { var rating = _unitOfWork.LectureNoteRatings.GetByUserAndPost(model.AuthorId, model.PostId); if (rating == null) { var newRating = new LectureNoteRating { LectureNote = _unitOfWork.LectureNotes.Get(model.PostId), User = _unitOfWork.Users.Get(model.AuthorId), Rating = model.Rating }; _unitOfWork.LectureNoteRatings.Add(newRating); } else { rating.Rating = model.Rating; _unitOfWork.LectureNoteRatings.Update(rating); } await _unitOfWork.SaveAsync(); await UpdateRatingAsync(model.PostId); }