예제 #1
0
        public IActionResult CreatePost([FromBody] PostCreateDTO postCreateDTO)
        {
            if (postCreateDTO == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var post = this._mapper.Map <Post>(postCreateDTO);

            // Insert tag from list tag id
            PostUtilities.InsertTagsInPost(this._unitOfWork, post, postCreateDTO.TagsGuid);
            this._unitOfWork.PostRepository.Add(post);
            if (this._unitOfWork.SaveChanges() == 0)
            {
                ModelState.AddModelError("error", $"Some thing wrong");
                return(StatusCode(StatusCodes.Status500InternalServerError, ModelState));
            }

            return(CreatedAtRoute("GetPostById", new { id = post.Id }, postCreateDTO));
        }
예제 #2
0
        public IActionResult UpdatePost([FromBody] PostUpdateDTO postUpdateDTO)
        {
            if (postUpdateDTO == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (this._unitOfWork.PostRepository.Find(postUpdateDTO.Id) == null)
            {
                return(NotFound());
            }

            var post = this._mapper.Map <Post>(postUpdateDTO);

            // Insert tag from list tag id
            PostUtilities.InsertTagsInPost(this._unitOfWork, post, postUpdateDTO.TagsGuid);
            // Insert tag from list tag id
            PostUtilities.InsertCommentsInPost(this._unitOfWork, post, postUpdateDTO.CommentsGuid);

            this._unitOfWork.PostRepository.Update(post);
            if (this._unitOfWork.SaveChanges() == 0)
            {
                ModelState.AddModelError("error", $"Something went wrong");
                return(StatusCode(StatusCodes.Status500InternalServerError, ModelState));
            }

            return(NoContent());
        }