public async Task <ActionResult> Post(Post model, string postId) { await SetViewBag(); var post = await this.posRepository.GetAsync(postId); if (post == null) { return(HttpNotFound()); } if (string.IsNullOrWhiteSpace(model.NewComment.Content)) { return(RedirectToAction("Post", "HomeBlog", new { postId = postId })); } model.NewComment.UserId = UserId; model.NewComment.PostId = post.Id; try { ICommentRepository commentRepository = new CommentRepository(); await commentRepository.CreateAsync(model.NewComment); return(RedirectToAction("Post")); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return(View(model)); } }
public async Task <CommentDTO> AddComment(int answerId, string body, int?commentAncestorId, int userId) { var dateTime = DateTime.Now; var comment = new Comment() { AuthorId = userId, DateOfCreation = dateTime, Body = body, AnswerId = answerId, CommentAncestorId = commentAncestorId == 0 ? null : commentAncestorId, }; await _commentRepository.CreateAsync(comment); await _unitOfWork.Commit(); comment = await _commentRepository.GetByDate(userId, dateTime); return(_mapper.Map <CommentDTO>(comment)); }
public async Task Check_CreateCommentIsCorrect() { //Arrange var cls = new InMemoryAppDbContext(); using (var context = cls.GetContextWithData()) { ICommentRepository repository = new CommentRepository(context); //Act Comment commentNew = new Comment { Id = 10, ItemId = 1, Text = "Comment text10", UserId = "2138b181-4cee-4b85-9f16-18df308f387d", Date = DateTime.Today }; await repository.CreateAsync(commentNew); var actual = context.Comments.Find(commentNew.Id); //Assert Assert.NotNull(actual); Assert.Equal(commentNew, actual); context.Database.EnsureDeleted(); } }