public IActionResult AddComment([FromBody] AddCommentRequestModel comment)
        {
            //string UserId = "439e896d-d4d4-4c3e-937c-cd45d6f63dfe"; //temp
            string UserId = UserManagerRepo.GetUserId(User);
            //string UserId = "1"; //temp

            var newComment = new Comment()
            {
                Content  = comment.Content,
                GroupId  = null,
                ReplyTo  = null,
                ThreadId = comment.ThreadId,
                UserId   = UserId
            };

            Repo.Add(newComment);
            if (Repo.SaveChanges() > 0)
            {
                //return Json(new {Success = true});
                return(Json(newComment));                        //rem map to viewmodel
            }
            return(BadRequest(new { Errors = "" }));

            /*try{
             *  Repo.SaveChanges();
             * }catch(DbUpdateException e){
             *  return BadRequest(new {Errors = e.InnerException.Message} );
             * }
             * return Json(new {Success = true});*/
        }
 public async Task <IActionResult> Add([FromBody] CommentImageDTO commentImageDTO)
 {
     if (ModelState.IsValid)
     {
         string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
         try
         {
             string commentId = _commentRepo.Add(commentImageDTO.Content, userId);
             _commentRepo.Save();
             if (String.IsNullOrEmpty(commentId))
             {
                 _imageCommentRepo.CommentImage(commentId, commentImageDTO.ImageId);
                 return(Ok());
             }
             return(NoContent());
         }
         catch (Exception ex)
         {
             throw new Exception(ex.Message);
         }
     }
     else
     {
         return(BadRequest());
     }
 }
Beispiel #3
0
        public async Task <Comment> Create(CommentCreate create, User user)
        {
            Comment c = factory.Create(create, user);
            await repo.Add(c);

            await bus.Dispatch(new CommentCreateEvent(c));

            return(c);
        }
Beispiel #4
0
        protected async override Task <CommentView> HandleInput(CommentCreateParams input)
        {
            using (var connection = database.GetConnection()) {
                IPostRepo    postRepo    = database.GetRepo <IPostRepo>(connection);
                ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection);
                IVoteRepo    voteRepo    = database.GetRepo <IVoteRepo>(connection);

                // Locate the post to ensure it actually exists.
                Post?post = await postRepo.FindById(input.PostId);

                if (post == null)
                {
                    throw new InvalidOperationException();
                }

                using (var transaction = connection.BeginTransaction()) {
                    Comment comment = new Comment()
                    {
                        User         = input.User,
                        PostId       = post.Id,
                        Body         = input.Body,
                        CreationDate = DateTime.UtcNow
                    };

                    // Set the parent comment if needed.
                    if (input.ParentId != 0)
                    {
                        comment.Parent = await commentRepo.FindById(input.ParentId);
                    }

                    // Update the comment count cache on the post.
                    post.CommentCount++;

                    comment.Upvotes++;
                    await commentRepo.Add(comment);

                    Vote upvote = new Vote()
                    {
                        User         = input.User,
                        ResourceId   = comment.Id,
                        ResourceType = VoteResourceType.Comment,
                        Direction    = VoteDirection.Up
                    };

                    await postRepo.Update(post);

                    await voteRepo.Add(upvote);

                    comment.Vote = upvote;
                    transaction.Commit();

                    return(commentMapper.Map(comment));
                }
            }
        }
        public void CommentRepository_GetByReviewId_ReturnsIEnumerable()
        {
            //arrange
            ICommentRepo cr = GetInMemoryCommentRepository();

            //act
            cr.Add(comment);
            //Id should be 1

            //assert
            Assert.IsAssignableFrom <IEnumerable <Comment> >(cr.GetByReviewId(1));
        }
        public void CommentRepository_GetById_ThrowsExceptionWhenIdNotFound()
        {
            //arrange
            ICommentRepo cr = GetInMemoryCommentRepository();

            //act
            cr.Add(comment);
            //Id should be 1

            //assert
            Assert.ThrowsAny <ArgumentNullException>(() => cr.GetById(0));
        }
 public IActionResult AddComment([FromBody] Comment comment)
 {
     try
     {
         int CommentId  = _commentRepository.Add(comment);
         var newComment = _commentRepository.GetById(CommentId);
         return(CreatedAtAction(nameof(GetCommentById), new { id = CommentId }, newComment));
     }
     catch (InvalidOperationException)
     {
         return(Conflict());
     }
 }
        public void CommentRepository_CreateLike_CreatesOneAndOnlyOneCommentLikePerUser()
        {
            //arrange
            ICommentRepo cr = GetInMemoryCommentRepository();

            //act
            cr.Add(comment);
            cr.CreateLike(1, 1); //user 1 likes comment 1
            cr.CreateLike(1, 1); //if user 1 likes the same comment again, won't cause exception

            //assert
            Assert.True(true);
        }
Beispiel #9
0
        public async Task <ActionResult> AddComment(AddCommentDto addCommentDto)
        {
            Comment comment = new Comment
            {
                PostId    = addCommentDto.PostId,
                UserId    = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                Content   = addCommentDto.Content,
                CreatedAt = DateTime.Now
            };

            _repo.Add(comment);
            await _repo.SaveAll();

            return(StatusCode(201));
        }
        public void CommentRepository_Add_AddsComment()
        {
            //arrange
            ICommentRepo cr = GetInMemoryCommentRepository();

            //act
            cr.Add(comment);
            var savedComment = cr.GetById(1); //should have Id of 1

            //assert
            Assert.Equal(1, savedComment.CommentId);
            Assert.Equal(1, savedComment.ReviewId);
            Assert.Equal(1, savedComment.UserId);
            Assert.IsType <DateTime>(savedComment.Date);
        }
        public void ShouldAddANewCommentToList()
        {
            //arrange
            var newitem = new Comment {
                commentid = 11, body = "body added for comment 11",
                userid    = 11, contentid = 11, shorttext = "slug-11"
            };

            //act
            _mockrepo.Add(newitem);
            var obj = _mockrepo.FindByID(11);

            //assert
            Assert.AreEqual("slug-11", obj.shorttext);
        }
        public void CommentRepository_Update_UpdatesComment()
        {
            //arrange
            ICommentRepo cr = GetInMemoryCommentRepository();

            //act
            cr.Add(comment);
            var savedComment = cr.GetById(1); //should have Id of 1

            savedComment.Content = "Updated content";
            cr.Update(savedComment);
            var updatedComment = cr.GetById(1);

            //assert
            Assert.Equal("Updated content", updatedComment.Content);
        }
Beispiel #13
0
        public ActionResult AddPlus(Comment comment)
        {
            var currentlyLoggedUserID = _user.GetIDOfCurrentlyLoggedUser().Value;
            var commToVote            = _comm.GetCommentById(comment.Comment_Id);

            bool checkPostState = true;
            var  commToVoteItem = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID);

            // prevent user from double voting
            if (commToVote != null)
            {
                if (commToVoteItem != null)
                {
                    checkPostState = commToVoteItem.IsCommentUpvoted;
                }
                else
                {
                    var commVoteEntity = new IsCommUpvoted()
                    {
                        User_Id          = currentlyLoggedUserID,
                        Comment_Id       = commToVote.Comment_Id,
                        IsCommentUpvoted = false
                    };
                    _comm.Add(commVoteEntity);
                    _comm.SaveChanges();
                    checkPostState = commVoteEntity.IsCommentUpvoted;
                }
            }

            if (commToVote != null && User.Identity.Name.ToLower() != commToVote.User.NickName.ToLower() && !checkPostState)
            {
                commToVote.Votes = commToVote.Votes + 1;
                commToVoteItem   = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID);
                commToVoteItem.IsCommentUpvoted = true;
                _comm.UpdateOnlyVotes(commToVote);
                _comm.UpdateIfCommState(commToVoteItem);
                _comm.SaveChanges();

                var result = new { result = true, votes = commToVote.Votes };
                return(Json(result,
                            JsonRequestBehavior.AllowGet));;
            }


            else if (commToVote != null && User.Identity.Name.ToLower() != commToVote.User.NickName.ToLower() && checkPostState)
            {
                commToVote.Votes = commToVote.Votes - 1;
                commToVoteItem   = _comm.GetPlusComment(commToVote.Comment_Id, currentlyLoggedUserID);
                commToVoteItem.IsCommentUpvoted = false;
                _comm.UpdateOnlyVotes(commToVote);
                _comm.UpdateIfCommState(commToVoteItem);
                _comm.SaveChanges();
                var result = new { result = false, votes = commToVote.Votes };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var result = new { result = false, votes = commToVote.Votes };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
        }