Example #1
0
        public IActionResult AddReactionToProduct([FromHeader(Name = "CommunicationKey")] string key, [FromBody] ReactionCreateDto reaction, [FromQuery] int userID)
        {
            if (productMockRepository.GetProductByID(reaction.ProductID) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Product with given ID does not exist"));
            }

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

            Reactions reactionEntity = mapper.Map <Reactions>(reaction);
            var       product        = productMockRepository.GetProductByID(reaction.ProductID);
            var       sellerID       = product.SellerID;

            if (!reactionRepository.CheckDoIFollowSeller(userID, sellerID))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, String.Format("You are not following user with id {0} and you can not add reaction to his products", sellerID)));
            }

            if (reactionRepository.CheckUserWithProductID(userID, reaction.ProductID) != null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User can add only one reaction to specific product."));
            }

            reactionEntity.UserID = userID;

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

                return(StatusCode(StatusCodes.Status201Created, "Reaction is successfully created!"));
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", String.Format("Reaction with ID {0} not created, message: {1}", reactionEntity.ReactionID, ex.Message), null);

                return(StatusCode(StatusCodes.Status500InternalServerError, "Create error"));
            }
        }