public async Task <ActionResult <CommentDto> > PostComment(long taskId, CommentCreationDto comment) { var task = await _context.Tasks .Include(t => t.Comments) .FirstOrDefaultAsync(t => t.Id == taskId); var newComment = _mapper.Map <Comment>(comment); task.Comments.Add(newComment); await _context.SaveChangesAsync(); var commentToReturn = _mapper.Map <CommentDto>(newComment); return(Ok(commentToReturn)); }
public void CanAddComment() { _context.Users.Add(_testUser); _context.Products.Add(_testProduct); _context.SaveChanges(); var comment = new CommentCreationDto { ProductId = _testProduct.Id, Message = "My test message" }; Assert.DoesNotThrow(() => { _commentService.AddComment(comment, _testUser); _commentService.Save(); }); }
public IActionResult Post([FromBody] CommentCreationDto comment) { var user = _userService.GetById(int.Parse(User.Identity.Name)); if (user == null) { return(Unauthorized()); } try { _commentService.AddComment(comment, user); return(Ok()); } catch (AppException ex) { return(BadRequest(new { message = ex.Message })); } }
public void CanGetCommentOnProduct() { _context.Users.Add(_testUser); _context.Products.Add(_testProduct); _context.SaveChanges(); var comment = new CommentCreationDto { ProductId = _testProduct.Id, Message = "My test message" }; _commentService.AddComment(comment, _testUser); _commentService.Save(); var product = _productRepository.GetProduct(_testProduct.Id); var firstComment = product.Comments.First(); Assert.AreEqual(firstComment.Message, comment.Message); }
public void AddComment(CommentCreationDto comment, User user) { if (user == null) { throw new AppException("User not found"); } var product = _context.Products.FirstOrDefault(x => x.Id == comment.ProductId); if (product == null) { throw new AppException("Product not found"); } var commentToAdd = new Comment { User = user, Product = product, Message = comment.Message, CreatedAt = DateTime.Now }; _context.Comments.Add(commentToAdd); }
public async Task <ActionResult> AddCommentToPerson(Guid id, [FromBody] CommentCreationDto comment) { var person = await _ctx.Persons.Where(p => p.Id == id).Include(d => d.Details).SingleOrDefaultAsync(); if (person == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest()); } var newComment = _mapper.Map <Comment>(comment); newComment.Details = person.Details; await _ctx.Comments.AddAsync(newComment); await _ctx.SaveChangesAsync(); return(Ok(new { Id = newComment.Id })); }
public async Task <IActionResult> CreateComment(CommentCreationDto commentCDto) { if (ModelState.IsValid) { var newComment = new Comment { ParentId = commentCDto.ReplyToCommentId, FullName = commentCDto.FullName, Email = commentCDto.Email, Content = commentCDto.Content, DateCreated = DateTimeOffset.Now, DateModified = DateTimeOffset.Now }; await _db.Comments.AddAsync(newComment); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } // TODO: Throw some error return(RedirectToAction(nameof(Index))); }