public RatingDTO CreateRating(RatingCreationDTO rating, int userId) { var user = _userMockRepository.GetUserByID(userId); if (user == null) { throw new NotFoundException("Not find user with that ID found..."); } if (_postMockRepository.GetPostById(rating.PostID) == null) { throw new NotFoundException("Post with that ID does not exist!"); } if (_ratingTypeRepository.GetRatingTypeByID(rating.RatingTypeID) == null) { throw new NotFoundException("Type of rating with that ID does not exist!"); } Rating entity = mapper.Map <Rating>(rating); entity.UserID = userId; entity.RatingDate = DateTime.Now; var post = _postMockRepository.GetPostById(rating.PostID); var userThatPostedId = post.UserID; if (_ratingRepository.CheckDidIBlockUser(userId, userThatPostedId)) { throw new BlockingException("You have blocked this user and you can not rate to his posts."); } if (!_ratingRepository.CheckDoIFollowUser(userId, userThatPostedId)) { throw new BlockingException("You are not following this user and you can not rate to his posts."); } if (_ratingRepository.CheckDidIAlreadyRate(userId, rating.PostID) != null) { throw new ErrorOccurException("You have already rate to this post."); } try { var rate = _ratingRepository.CreateRating(entity); _ratingRepository.SaveChanges(); return(mapper.Map <RatingDTO>(rate)); } catch (Exception ex) { throw new ErrorOccurException(ex.Message); } }
public ActionResult <RatingDTO> CreateRating([FromHeader] string key, [FromBody] RatingCreationDTO type, int userID) { if (!_authService.Authorize(key)) { return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!")); } try { var created = _ratingService.CreateRating(type, userID); string location = linkGenerator.GetPathByAction("GetRatingID", "Rating", new { ratingID = created.RatingID }); return(Created(location, created)); } catch (Exception ex) { logger.LogError(ex, "Error creating new raiting: " + ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }