public Comment Update(Guid commentId, Dto.Comment comment) { var existingComment = _context.Comments.SingleOrDefault(p => p.Id == commentId); if (existingComment == null) { throw new CommentNotFoundException(commentId); } existingComment.Message = comment.Message; _context.SaveChanges(); return(existingComment); }
public IActionResult Put(Guid id, [FromBody] Dto.Comment comment) { if (comment == null || id == Guid.Empty) { return(new BadRequestResult()); } try { var editedComment = _commentService.Update(id, comment); var dto = _mapper.Map <Domain.Comment, Dto.Comment>(editedComment); return(new OkObjectResult(dto)); } catch (Exception) { return(new BadRequestResult()); } }
public IActionResult Post(string postId, [FromBody] Dto.Comment comment) { try { var createdComment = _commentService.Create(postId, comment); var location = Url.RouteUrl(new { Action = "Get", Controller = "Comment", id = createdComment.Id }); var dto = _mapper.Map <Domain.Comment, Dto.Comment>(createdComment); return(new CreatedResult(location, dto)); } catch (DuplicatePostException e) { return(new BadRequestObjectResult(e.Message)); } catch (Exception) { return(new BadRequestResult()); } }
public Comment Create(string postId, Dto.Comment comment) { var post = _context.Posts.SingleOrDefault(p => p.Slug == postId); if (post == null) { throw new PostNotFoundException(postId); } var newComment = new Comment { Id = Guid.NewGuid(), Name = comment.Name, Message = comment.Message, DateCreated = DateTime.UtcNow, Email = comment.Email, Post = post }; _context.Comments.Add(newComment); _context.SaveChanges(); return(newComment); }