public async Task <ActionResult> UpdatePost([FromBody] PostDto postDto) { if (postDto.Id == Guid.Empty) { var error = new ErrorDto().Append("Id", "this field Id is required to update a post object"); return(BadRequest(error)); } var post = postDto.GetPersistentObject(); var oldPost = await postService.GetPostOnly(post.Id); if (oldPost == null) { return(NotFound()); } if (oldPost.AuthorId != HttpContext.GetUserId()) { return(Forbid()); } if (TryValidateModel(post)) { var result = await postService.Update(oldPost, post); return(Ok(result)); } return(BadRequest(ModelState)); }
public async Task <ActionResult> CreatePost([FromBody] PostDto postDto) { var post = postDto.GetPersistentObject(); if (TryValidateModel(post)) { post.AuthorId = HttpContext.GetUserId(); var result = await postService.Create(post); string refUrl = $"{HttpContext.GetCurrentRequestUrl()}/{result.Id.ToString()}"; return(Created(refUrl, result)); } return(BadRequest(ModelState)); }