public IActionResult UpdateBlog([FromBody] UpdateBlogPost model, [FromRoute] string slug) { try { var blogInDb = _repositoryBlogPost.GetBySlug(slug); if (blogInDb == null) { return(NotFound("There is no blog with that slug")); } var blog = new BlogPost { CreatedAt = blogInDb.CreatedAt, Body = string.IsNullOrWhiteSpace(model.Body) ? blogInDb.Body : model.Body, Description = string.IsNullOrWhiteSpace(model.Description) ? blogInDb.Description : model.Description, Title = string.IsNullOrWhiteSpace(model.Title) ? blogInDb.Title : model.Title }; blog.Slug = blog.Title.Replace(' ', '-').ToLower(); _repositoryBlogPost.Update(blog, slug); return(Ok(_repositoryBlogPost.GetBySlug(blog.Slug))); } catch (Exception ex) { _logger.LogError(ex.Message, ex); return(StatusCode(500)); } }
/// <inheritdoc /> public BlogPost Update(int id, UpdateBlogPost updatedBlogPost) { var blogPost = _blogPosts.FirstOrDefault(x => x.Id == id); if (blogPost == null) { throw new EntityNotFoundException($"Can't find a blog post with id '{id}'"); } blogPost.Text = updatedBlogPost.Text; return(blogPost); }
public void UpdateBlogPost(UpdateBlogPost updateBlogPost, string slug) { BlogPost blogPost = BlogDBContext.BlogPosts.Where(m => m.Slug == slug).FirstOrDefault(); if (updateBlogPost.Title != null) { SlugHelper slugHelper = new SlugHelper(); string Slug = slugHelper.GenerateSlug(updateBlogPost.Title); blogPost.Slug = Slug; } if (updateBlogPost.Description != null) { blogPost.Description = updateBlogPost.Description; } if (updateBlogPost.Body != null) { blogPost.Body = updateBlogPost.Body; } blogPost.UpdatedAt = DateTime.Now; BlogDBContext.BlogPosts.Update(blogPost); BlogDBContext.SaveChanges(); }
public ActionResult <BlogPost> Update( int id, [FromBody] UpdateBlogPost updatedBlogPost ) { try { var updateBlogPostDto = new DTO.UpdateBlogPost() { Text = updatedBlogPost.Text, }; var blogPost = _blogPostsDataService.Update(id, updateBlogPostDto); // return blogPost; return(null); } catch (Exception ex) { if (ex.GetType() == typeof(EntityNotFoundException)) { return(NotFound()); } return(BadRequest()); } }
public IActionResult Put([FromBody] UpdateBlogPost updateBlogPost, string slug) { BlogPostServiceManagement.UpdateBlogPost(updateBlogPost, slug); return(NoContent()); }