예제 #1
0
        public void UpdateRating(RatingModifyingDTO rating, Guid ratingID)
        {
            if (_ratingRepository.GetRatingByID(ratingID) == null)
            {
                throw new NotFoundException("Rating with that ID does not exist!");
            }

            if (_ratingTypeRepository.GetRatingTypeByID(rating.RatingTypeID) == null)
            {
                throw new NotFoundException("Type of rating with that ID does not exist!");
            }

            var oldRate = _ratingRepository.GetRatingByID(ratingID);
            var newRate = mapper.Map <Rating>(rating);

            if (oldRate.PostID != newRate.PostID)
            {
                throw new ErrorOccurException("Post ID can not be changed!");
            }

            try
            {
                newRate.UserID = oldRate.UserID;

                mapper.Map(newRate, oldRate);
                _ratingRepository.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException("Error updating reaction: " + ex.Message);
            }
        }
예제 #2
0
        public IActionResult UpdateRating([FromHeader] string key, [FromBody] RatingModifyingDTO updatedType, Guid ratingID)
        {
            if (!_authService.Authorize(key))
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!"));
            }

            var newType = mapper.Map <Rating>(updatedType);

            try
            {
                _ratingService.UpdateRating(updatedType, ratingID);
                var res = mapper.Map <Rating>(newType);
                res.RatingID = ratingID;

                return(Ok(res));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error updating rating: " + ex.Message);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }