public ActionResult <ReactionReadDto> Create([FromBody] ReactionCreateDto request)
        {
            if (_userRepository.Get(request.UserId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User doesn't exist."));
            }

            if (_postRepository.Get(request.PostId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Post doesn't exist."));
            }

            if (_reactionTypeRepository.Get(request.ReactionTypeId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Reaction type doesn't exist."));
            }

            Reaction newEntity = _mapper.Map <Reaction>(request);

            newEntity.DateTime = DateTime.UtcNow;

            Reaction result = _reactionRepository.Create(newEntity);

            return(StatusCode(StatusCodes.Status201Created, _mapper.Map <ReactionReadDto>(result)));
        }
示例#2
0
        public ActionResult <ReactionReadDto> CreateReaction(ReactionCreateDto _ReactionCreateDto)
        {
            var commentModel = _mapper.Map <Reaction>(_ReactionCreateDto);

            _repository.createReaction(commentModel);
            _repository.SaveChanges();
            var commentItem = _mapper.Map <ReactionReadDto>(commentModel);

            return(Ok(commentItem));
        }
示例#3
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"));
            }
        }