public async Task <IActionResult> Update(Guid id, BlogPostCreateDto blogPostCreateDto) { if (blogPostCreateDto == null || id != blogPostCreateDto.Id || blogPostCreateDto.CategoryIds.Count == 0) { return(BadRequest()); } var blogPost = _mapper.Map <BlogPost>(blogPostCreateDto); var existCategoryPostItems = _service.Where <CategoryPostItem>(x => x.BlogPostId == id); _service.RemoveRange(existCategoryPostItems); List <CategoryPostItem> categoryPostItems = new List <CategoryPostItem>(); blogPostCreateDto.CategoryIds.ForEach(categoryId => { var categoryBlogItem = new CategoryPostItem(); categoryBlogItem.UserId = Guid.Parse("1db506b5-3fa5-451a-9a4b-68edf7b4c2d7"); categoryBlogItem.CategoryId = categoryId; categoryBlogItem.BlogPostId = blogPost.Id; categoryPostItems.Add(categoryBlogItem); }); await _service.AddRangeAsync <CategoryPostItem>(categoryPostItems.AsQueryable()); _service.Update(blogPost); return(NoContent()); }
public ActionResult <BlogPostReadDto> CreateBlogPost(BlogPostCreateDto blogPostCreateDto) { var blogPostModel = _mapper.Map <BlogPost>(blogPostCreateDto); if (blogPostModel == null) { throw new ArgumentNullException(nameof(blogPostModel)); } _context.BlogPosts.Add(blogPostModel); _context.SaveChanges(); var blogPostReadDto = _mapper.Map <BlogPostReadDto>(blogPostModel); return(CreatedAtRoute("Get", new { blogPostReadDto.Id }, blogPostReadDto)); }
public async Task <IActionResult> Save(BlogPostCreateDto blogPostCreateDto) { var blogPost = _mapper.Map <BlogPost>(blogPostCreateDto); await _service.AddAsync <BlogPost>(blogPost); List <CategoryPostItem> categoryPostItems = new List <CategoryPostItem>(); blogPostCreateDto.CategoryIds.ForEach(categoryId => { var categoryBlogItem = new CategoryPostItem(); categoryBlogItem.UserId = Guid.Parse("1db506b5-3fa5-451a-9a4b-68edf7b4c2d7"); categoryBlogItem.CategoryId = categoryId; categoryBlogItem.BlogPostId = blogPost.Id; categoryPostItems.Add(categoryBlogItem); }); await _service.AddRangeAsync <CategoryPostItem>(categoryPostItems.AsQueryable()); return(NoContent()); }