private async Task <CommentRatingModel> GetCommentRatingModelFromDTO(CommentRatingDTO rating) { UserDTO user = await _userService.GetUserById(rating.ApplicationUserId); return(new CommentRatingModel { username = user.Username, givenRating = rating.GivenRating, commentId = rating.CommentId, id = rating.Id }); }
public async Task <IActionResult> CreateCommentRating([Required] string id, [FromBody] CommentRatingModel item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); CommentRatingDTO commentRating = await GetCommentRatingDTOFromModel(item, user.Id, id); commentRating = await _commentRatingService.Create(commentRating); return(Ok(await GetCommentRatingModelFromDTO(commentRating))); } return(BadRequest(ModelState)); }
public async Task <IActionResult> DeleteCommentRating([Required] string id) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); CommentRatingDTO commentRating = await _commentRatingService.GetById(id); if (commentRating == null) { return(NotFound()); } if (commentRating.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { CommentRatingDTO deletedCommentRating = await _commentRatingService.Delete(id); return(Ok(await GetCommentRatingModelFromDTO(deletedCommentRating))); } } return(BadRequest(ModelState)); }
public async Task <CommentRatingDTO> Update(CommentRatingDTO item) { CommentRating commentRating = await _database.CommentRatingRepository.Update(_mapper.Map <CommentRatingDTO, CommentRating>(item)); return(_mapper.Map <CommentRating, CommentRatingDTO>(commentRating)); }