public IActionResult PostEdit(int id, [FromBody] BlogPostForUpdateDto blogPost) { try { if (blogPost == null) { _logger.LogError("BlogPost object sent from client is null."); return(BadRequest("BlogPost object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid blogpost object sent from client."); return(BadRequest("Invalid model object")); } var blogPostEntity = _blogService.GetBlogPostById(id); if (blogPostEntity == null) { _logger.LogError($"BlogController PostEdit Method : Blog Post with id {id}, hasn't been found in db."); return(NotFound()); } blogPostEntity = blogPost.ToEntity(blogPostEntity); _blogService.UpdateBlogPost(blogPostEntity); return(NoContent()); } catch (Exception ex) { var logMessage = $"BlogController PostEdit Method. Something went wrong. Request : { JsonConvert.SerializeObject(blogPost) } Ex Message : { ex.Message }"; _logger.LogError(ex, logMessage); return(StatusCode(500, "Internal server error")); } }
public IActionResult UpdateBlogPost(int id, [FromBody] BlogPostForUpdateDto blogPost) { if (blogPost == null) { return(BadRequest()); } if (blogPost.Title == blogPost.Contents) { ModelState.AddModelError("Description", "The provided description should be different from the name."); ; } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var blogPostEntity = _blogInfoRepository.GetPost(id); if (blogPostEntity == null) { return(NotFound()); } Mapper.Map(blogPost, blogPostEntity); try { if (!_blogInfoRepository.Save()) { _logger.LogWarning("Save failed"); return(StatusCode(500, "A problem happened while handling your request.")); } var updatedBlogPostToReturn = Mapper.Map <Models.BlogPostDto>(blogPostEntity); _logger.LogInformation($"Blog Post {updatedBlogPostToReturn.Id} was updated"); return(CreatedAtRoute("GetPost", new { id = updatedBlogPostToReturn.Id }, updatedBlogPostToReturn)); } catch (Exception e) { _logger.LogError($"Issue in save: {e}"); return(StatusCode(500, "A problem happened while handling your request.")); } }