public async Task <Post> Create(PostCreateInDto createOptions) { using (var transaction = await _context.Database.BeginTransactionAsync()) { var now = DateTime.Now; var newPost = new Post { StatusId = createOptions.StatusId, CategoryId = createOptions.CategoryId, CreateTime = now, UpdateTime = now, UserId = createOptions.UserId, Title = createOptions.Title, Content = createOptions.Content, Description = createOptions.Description }; await _context.Posts.AddAsync(newPost); await _context.SaveChangesAsync(); await _context.PostTagAssociations.AddRangeAsync(createOptions.TagIds.Select(t => new PostTagAssociation { TagId = t, PostId = newPost.Id })); await _context.SaveChangesAsync(); transaction.Commit(); return(newPost); } }
public async Task <ActionResult <ResultOutDto <Post> > > PostPost([FromBody] PostCreateInDto createOptions) { try { var post = await _postService.Create(createOptions); return(Ok(ResultOutDtoBuilder.Success(post))); } catch (NotExistedException e) { return(NotFound(ResultOutDtoBuilder.Fail <Post>(e, "Not exist"))); } }