public async Task <int> UpdateBlog(int blogId, BlogForCreation blogDto) { if (blogId == 0) { return(0); } var blogForUpdate = await _db.Blogs.FirstAsync(p => p.BlogId == blogId); if (blogForUpdate == null) { return(-1); } if (blogDto.Title != null) { blogForUpdate.Title = blogDto.Title; } if (blogDto.ShortDescription != null) { blogForUpdate.ShortDescription = blogDto.ShortDescription; } if (blogDto.Content != null) { blogForUpdate.Content = blogDto.Content; } if (blogDto.BlogCategoryId != 0) { blogForUpdate.CategoryBlogId = blogDto.BlogCategoryId; } await _db.SaveChangesAsync(); return(1); }
public async Task <IActionResult> CreateBlog(BlogForCreation blogDto) { try { var result = await _blogServices.CreateBlog(blogDto); if (result == null) { return(new BadRequestObjectResult(new { Message = "Có lỗi xảy ra. Vui lòng kiểm tra thông tin trước khi đăng" })); } return(Ok(result)); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public async Task <IActionResult> UpdateBlog(int blogId, BlogForCreation blogDto) { try { var result = await _blogServices.UpdateBlog(blogId, blogDto); if (result == -1) { return(new BadRequestObjectResult(new { Message = "Vui lòng thay đổi thông tin của blog" })); } if (result == 0) { return(new BadRequestObjectResult(new { Message = "Có lỗi khi cập nhật. Vui lòng thử lại" })); } return(Ok(new { Message = "Cập nhật tin tức thành công" })); } catch (Exception ex) { return(new BadRequestObjectResult(new { Message = ex.Message.ToString() })); } }
public async Task <BlogResponseForCreationPhoto> CreateBlog(BlogForCreation blogView) { if (blogView == null) { return(null); } Blog blogToReturn = new Blog { Title = blogView.Title, ShortDescription = blogView.ShortDescription, Content = blogView.Content, CreatedDate = blogView.CreatedDate, CategoryBlogId = blogView.BlogCategoryId }; _db.Blogs.Add(blogToReturn); await _db.SaveChangesAsync(); return(new BlogResponseForCreationPhoto { BlogId = blogToReturn.BlogId, }); }