コード例 #1
0
        public IActionResult UpdateReaction([FromHeader(Name = "CommunicationKey")] string key, [FromBody] ReactionUpdateDto reaction)
        {
            if (reactionRepository.GetReactionByID(reaction.ReactionID) == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "There is no reaction with given ID!"));
            }

            if (typeOfReactionRepository.GetTypeOfReactionByID(reaction.TypeOfReactionID) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Type of reaction with given ID does not exist!"));
            }
            try
            {
                Reactions newReaction = mapper.Map <Reactions>(reaction);

                Reactions oldReaction = reactionRepository.GetReactionByID(reaction.ReactionID);

                if (newReaction.ProductID != oldReaction.ProductID)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, "Product ID can not be changed!"));
                }
                newReaction.UserID = oldReaction.UserID;

                mapper.Map(newReaction, oldReaction);

                reactionRepository.SaveChanges();
                logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", String.Format("Successfully updated reaction with ID {0} in database", reaction.ReactionID), null);

                return(StatusCode(StatusCodes.Status200OK));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", String.Format("Error while updating reaction with ID {0}, message: {1}", reaction.ReactionID, ex.Message), null);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Update error"));
            }
        }