예제 #1
0
        public async Task <IActionResult> PartiallyUpdateUserPostForUser(int userId, int userPostId,
                                                                         JsonPatchDocument <UserPostForUpdateDto> patchDocument)
        {
            bool userExists = await Mediator.Send(new UserExistsQuery(userId));

            if (!userExists)
            {
                return(NotFound());
            }

            var userPostFromQuery = await Mediator.Send(new GetUserPostByUserAndPostIdQuery(userId, userPostId));

            //If does not exists create new one , add db
            if (userPostFromQuery == null)
            {
                var userPostDto = new UserPostForUpdateDto();

                //Add Validation
                patchDocument.ApplyTo(userPostDto, ModelState);

                if (!TryValidateModel(userPostDto))
                {
                    return(ValidationProblem(ModelState));
                }

                var userPostToAdd = _mapper.Map <UserPost>(userPostDto);

                _postRepository.AddUserPost(userId, userPostToAdd);

                await _postRepository.SaveChangesAsync();

                var userPostToReturn = _mapper.Map <UserPostDto>(userPostToAdd);

                return(CreatedAtRoute("GetUserPostsForUser",
                                      new { userId = userId, userPostId = userPostToReturn.Id },
                                      userPostToReturn));
            }

            var userPostToPatch = _mapper.Map <UserPostForUpdateDto>(userPostFromQuery);

            //Add validation
            patchDocument.ApplyTo(userPostToPatch, ModelState);

            if (!TryValidateModel(userPostToPatch))
            {
                return(ValidationProblem(ModelState));
            }

            _mapper.Map(userPostToPatch, userPostFromQuery);

            _postRepository.UpdateUserPost(userPostFromQuery);

            await _postRepository.SaveChangesAsync();

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> UpdateUserPostForUser(int userId, int userPostId, UserPostForUpdateDto userPost)
        {
            bool userExists = await Mediator.Send(new UserExistsQuery(userId));

            if (!userExists)
            {
                return(NotFound());
            }

            var userPostFromQuery = await Mediator.Send(new GetUserPostByUserAndPostIdQuery(userId, userPostId));

            //Create if userPost does not exists
            if (userPostFromQuery == null)
            {
                var userPostToAdd = _mapper.Map <UserPost>(userPost);
                //userPostToAdd.Id = userPostId;

                _postRepository.AddUserPost(userId, userPostToAdd);

                await _postRepository.SaveChangesAsync();

                var userPostToReturn = _mapper.Map <UserPostDto>(userPostToAdd);

                return(CreatedAtRoute("GetUserPostsForUser",
                                      new { userId = userId, userPostId = userPostToReturn.Id },
                                      userPostToReturn));
            }

            //else update
            _mapper.Map(userPost, userPostFromQuery);

            _postRepository.UpdateUserPost(userPostFromQuery);

            await _postRepository.SaveChangesAsync();

            return(NoContent());
        }