Пример #1
0
        public async Task <IActionResult> LikeComment([FromRoute] int id)
        {
            if (!await CommentExists(id))
            {
                return(BadRequest($"Комментарий не существует"));
            }

            var user = await _currentUser.GetCurrentUser(HttpContext);

            var like = await _context.UserCommentLikes.FirstOrDefaultAsync(l =>
                                                                           l.UserId == user.Id && l.CommentId == id);

            var comment = await _context.Comments.FirstOrDefaultAsync(c => c.Id == id);

            if (like != null)
            {
                return(BadRequest("Не удалось поставить лайк"));
            }

            like = new UserCommentLike {
                UserId = user.Id, CommentId = id
            };
            _context.UserCommentLikes.Add(like);
            comment.LikesCount++;

            _context.Update(comment);
            await _context.SaveChangesAsync();

            return(Ok());
        }
Пример #2
0
            public async Task <CommentDto> Handle(CommentCreateCommand request, CancellationToken cancellationToken)
            {
                var post = await _context.Posts.FindAsync(request.PostId);

                if (request.Against == request.Support && request.Support)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Comment = "Comment can't be both in support and against." });
                }
                if (request.Against == request.Support && !request.Support)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Comment = "Comment need to be either in support or against the post." });
                }

                if (post == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Post = "Not Found" });
                }
                var user = await _context.Users.SingleOrDefaultAsync(u => u.Email == request.Email, cancellationToken : cancellationToken);

                var comment = new Comment
                {
                    Author   = user,
                    Post     = post,
                    Body     = request.Body,
                    Date     = DateTime.Now,
                    Support  = request.Support,
                    Against  = request.Against,
                    IsActive = true,
                };

                post.Comments.Add(comment);

                var isCommentAuthor = new UserCommentLike
                {
                    AppUser  = user,
                    Comment  = comment,
                    IsAuthor = true,
                    IsLiked  = false
                };
                await _context.UserCommentLikes.AddAsync(isCommentAuthor, cancellationToken);

                var success = await _context.SaveChangesAsync(cancellationToken) > 0;

                if (success)
                {
                    return(_mapper.Map <CommentDto>(comment));
                }

                throw new Exception("Problem saving new comment.");
            }
Пример #3
0
        public ActionResult UpdateLike(int commentId, bool like)
        {
            var  comment           = unitOfWork.Repository <Comment>().GetByID(commentId);
            var  postid            = comment.PostId;
            var  userId            = User.Identity.GetUserId();
            bool?currentUserChoice = like;

            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("login", "Account", new { returnUrl = "/Glight/Detail/" + postid }));
            }
            var Likes = unitOfWork.Repository <UserCommentLike>().Get(a => a.LikedCommentId.Equals(commentId) && a.LikeUserId.Equals(userId));

            if (Likes.Any())
            {
                if (Likes.First().Up.Equals(like))
                {
                    unitOfWork.Repository <UserCommentLike>().Delete(Likes.First().id);
                    currentUserChoice = null;
                }
                else
                {
                    Likes.First().Up = like;
                    unitOfWork.Repository <UserCommentLike>().Update(Likes.First());
                }
            }
            else
            {
                var likeModel = new UserCommentLike()
                {
                    LikeUserId     = userId,
                    LikedCommentId = commentId,
                    Up             = like
                };
                unitOfWork.Repository <UserCommentLike>().Insert(likeModel);
            }
            unitOfWork.Save();
            var comments = unitOfWork.Repository <Comment>().Get(a => a.PostId.Equals(postid));
            var model    = getCommentViewModel(comments);

            ViewBag.postId = postid;
            return(PartialView("_CommentSection", model));
        }
Пример #4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var comment = await _context.Comments.FindAsync(request.Id);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Comment = "Can't find comment." });
                }
                var user = await _context.Users.SingleOrDefaultAsync(x => x.Email == _userAccessor.GetEmail(), cancellationToken : cancellationToken);

                var userLikeOrComment = await _context.UserCommentLikes.SingleOrDefaultAsync(x => x.CommentId == comment.Id && x.AppUserId == user.Id, cancellationToken : cancellationToken);

                if (userLikeOrComment?.IsLiked == true)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Attendence = "Already Liked." });
                }
                else if (userLikeOrComment?.IsLiked == false)
                {
                    userLikeOrComment.IsLiked = true;
                }
                else
                {
                    var newLike = new UserCommentLike {
                        Comment  = comment,
                        AppUser  = user,
                        IsAuthor = false,
                        IsLiked  = true
                    };

                    await _context.UserCommentLikes.AddAsync(newLike, cancellationToken);
                }


                var success = await _context.SaveChangesAsync(cancellationToken) > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem liking comment.");
            }