コード例 #1
0
ファイル: LikeComment.cs プロジェクト: OctavianM1/Pipe
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var alreadyLiked = _context.CommentLikes.Where(cl => cl.ActivityId == request.ActivityId && cl.UserId == request.UserId && cl.ActivityCommentId == request.CommentId).FirstOrDefault();

                if (alreadyLiked == null)
                {
                    var commentLike = new CommentLikes
                    {
                        Id                = Guid.NewGuid(),
                        ActivityId        = request.ActivityId,
                        UserId            = request.UserId,
                        ActivityCommentId = request.CommentId
                    };
                    _context.CommentLikes.Add(commentLike);
                }
                else
                {
                    _context.CommentLikes.Remove(alreadyLiked);
                }
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem adding like to comment");
            }
コード例 #2
0
        public async Task <IActionResult> Edit(Guid id, [Bind("CommentId,LikeId")] CommentLikes commentLikes)
        {
            if (id != commentLikes.CommentId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(commentLikes);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CommentLikesExists(commentLikes.CommentId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CommentId"] = new SelectList(_context.Comments, "Id", "Id", commentLikes.CommentId);
            ViewData["LikeId"]    = new SelectList(_context.Like, "Id", "Id", commentLikes.LikeId);
            return(View(commentLikes));
        }
コード例 #3
0
        public async Task <bool> SaveCommentLike(CommentLikes commentLike)
        {
            bool isCommentLikeAdded = false;

            try
            {
                var isCommentLikeExists = await this.GetCommentLikes(commentLike);

                if (isCommentLikeExists != null)
                {
                    return(await this.RemoveCommentLike(isCommentLikeExists));
                }

                await _context.CommentLikes.AddAsync(commentLike);

                await _context.SaveChangesAsync();

                isCommentLikeAdded = true;
            }
            catch (Exception)
            {
                throw;
            }
            return(isCommentLikeAdded);
        }
コード例 #4
0
        public async Task <IActionResult> PutCommentLikes(Guid id, CommentLikes commentLikes)
        {
            if (id != commentLikes.CommentId)
            {
                return(BadRequest());
            }

            _context.Entry(commentLikes).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommentLikesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #5
0
        public async Task LikeCommentAsync(int userId, int commentId)
        {
            if (!await CanLikeAsync(userId, commentId))
            {
                return;
            }
            var like = await CommentLikes.FindAsync(userId, commentId);

            if (like == null)
            {
                CommentLikes.Add(new CommentLike()
                {
                    CommentId = commentId,
                    UserId    = userId,
                    IsLiked   = true
                });
            }
            else
            {
                like.IsLiked = true;
            }
            var comment = await Comments.FindAsync(commentId);

            comment.Rating += 1;
            await SaveChangesAsync();
        }
コード例 #6
0
        public void Delete(CommentLikes entity)
        {
            entity.IsDeleted = 1;

            this.DbHelper.Execute(this.Table, proc =>
                                  proc.AsUpdate(entity, "IsDeleted", "Modifier", "ModifyTime")
                                  .Where("Id", entity.Id)
                                  );
        }
コード例 #7
0
        public ActionResult Index( )
        {
            CommentRepository rep = new CommentRepository( );
            //var model = rep.GetComments( );
            CommentLikes stuff = new CommentLikes();

            stuff.comments = rep.GetComments();
            stuff.likes    = rep.GetLikes();
            return(View(stuff));
        }
コード例 #8
0
        public void AddCommentLike(CommentLikes liked)
        {
            var comment = _dbContext.Comments.Single(c => c.Id == liked.CommentId);

            if (comment.Likes == null)
            {
                comment.Likes = new List <CommentLikes>();
            }
            comment.Likes.Add(liked);
            _dbContext.SaveChanges();
        }
コード例 #9
0
 public IActionResult AddCommentLike([FromBody] CommentLikes like)
 {
     try
     {
         return(Ok(_likesService.AddCommentLike(like)));
     }
     catch (NegotiatedContentResultException)
     {
         return(StatusCode(409));
     }
 }
コード例 #10
0
        public async Task <bool> CanLikeAsync(int userId, int commentId)
        {
            var like = await CommentLikes.FindAsync(new object[2] {
                userId, commentId
            });

            if (like == null)
            {
                return(true);
            }
            return(!like.IsLiked);
        }
コード例 #11
0
 public void CreateLike(int commentId, int userId)
 {
     if (_context.CommentLikes.FirstOrDefault(rl => rl.CommentId == commentId && rl.UserId == userId) == null)
     {
         var entity = new CommentLikes
         {
             UserId    = userId,
             CommentId = commentId
         };
         _context.CommentLikes.Add(entity);
         _context.SaveChanges();
     }
 }
コード例 #12
0
        public async Task <IActionResult> Create([Bind("CommentId,LikeId")] CommentLikes commentLikes)
        {
            if (ModelState.IsValid)
            {
                commentLikes.CommentId = Guid.NewGuid();
                _context.Add(commentLikes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CommentId"] = new SelectList(_context.Comments, "Id", "Id", commentLikes.CommentId);
            ViewData["LikeId"]    = new SelectList(_context.Like, "Id", "Id", commentLikes.LikeId);
            return(View(commentLikes));
        }
コード例 #13
0
ファイル: LikesService.cs プロジェクト: wojtek-rak/PaintStore
 public CommentLikes AddCommentLike(CommentLikes like)
 {
     using (var db = _paintStoreContext)
     {
         if ((db.CommentLikes.Any(x => x.CommentId == like.CommentId && x.UserId == like.UserId)))
         {
             throw new NegotiatedContentResultException();
         }
         CommentsManager.CommentLikesCountPlus(db, like.CommentId);
         db.CommentLikes.Add(like);
         db.SaveChanges();
         return(like);
     }
 }
コード例 #14
0
        public async Task <bool> RemoveCommentLike(CommentLikes commentLike)
        {
            var isDeleted = false;

            try
            {
                _context.CommentLikes.Remove(commentLike);
                await _context.SaveChangesAsync();

                isDeleted = true;
            }
            catch (Exception)
            {
                throw;
            }
            return(isDeleted);
        }
コード例 #15
0
        public async Task <CommentLikes> GetCommentLikes(CommentLikes comment)
        {
            var commentLike = new CommentLikes();

            try
            {
                commentLike = await _context
                              .CommentLikes
                              .Where(c => c.CommentId.Equals(comment.CommentId) && c.ApplicationUserId.Equals(comment.ApplicationUserId))
                              .FirstOrDefaultAsync();
            }
            catch (Exception)
            {
                throw;
            }
            return(commentLike);
        }
コード例 #16
0
        public async Task UnlikeCommentAsync(int userId, int commentId)
        {
            if (await CanLikeAsync(userId, commentId))
            {
                return;
            }
            var like = await CommentLikes.FindAsync(userId, commentId);

            if (like == null)
            {
                return;
            }
            like.IsLiked = false;
            var comment = await Comments.FindAsync(commentId);

            comment.Rating -= 1;
            await SaveChangesAsync();
        }
コード例 #17
0
        public ActionResult Like(int id)
        {
            CommentLikes like = new CommentLikes();

            like.User    = StaticClasses.UserRetriever.RetrieveUser(User, context);
            like.Comment = (from data in context.Comments where data.ID == id select data).First();
            var likes = (from data in context.CommentLikes where data.Comment.ID == like.Comment.ID && data.User.Id == like.User.Id select data).ToList();

            if (likes.Count > 0)
            {
                context.CommentLikes.Remove(likes[0]);
                context.SaveChanges();
            }
            else
            {
                context.CommentLikes.Add(like);
                context.SaveChanges();
            }
            return(Redirect(Request.UrlReferrer.ToString()));
        }
コード例 #18
0
        public async Task <ActionResult <CommentLikes> > PostCommentLikes(CommentLikes commentLikes)
        {
            _context.CommentLikes.Add(commentLikes);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (CommentLikesExists(commentLikes.CommentId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetCommentLikes", new { id = commentLikes.CommentId }, commentLikes));
        }
コード例 #19
0
        private Object SaveCommentLike(int commentId, string user)
        {
            var postService = new PostService(null);
            var item        = postService.GetCommentById(commentId);
            var liked       = new CommentLikes
            {
                Id        = 0,
                CommentId = item.Id,
                UserId    = user,
            };

            postService.AddCommentLike(liked);
            var comment = postService.GetCommentById(commentId);
            var anom    = new
            {
                id    = comment.Id,
                count = comment.Likes.Count(),
                type  = false
            };

            return(anom);
        }
コード例 #20
0
ファイル: DocumentsController.cs プロジェクト: v1996-96/kms
        public async Task <IActionResult> LikeComment([FromRoute] int?documentId, [FromRoute] string slug, [FromRoute] int commentId)
        {
            var document = await GetDocument(documentId, slug);

            if (document == null)
            {
                return(NotFound());
            }

            var comment = await _db.Comments.SingleOrDefaultAsync(c => c.CommentId == commentId);

            if (comment == null)
            {
                return(NotFound());
            }

            var existingLike = await _db.CommentLikes.SingleOrDefaultAsync(c => c.CommentId == commentId && c.UserId == UserId);

            if (existingLike == null)
            {
                var newLike = new CommentLikes
                {
                    CommentId   = commentId,
                    UserId      = UserId,
                    TimeCreated = DateTime.UtcNow
                };
                _db.CommentLikes.Add(newLike);
            }
            else
            {
                _db.CommentLikes.Remove(existingLike);
            }

            await _db.SaveChangesAsync();

            return(Ok());
        }
コード例 #21
0
        public async Task <IActionResult> SaveCommentLike(CommentLikes commentLikes)
        {
            var isAdded = await _commentLikeRepository.SaveCommentLike(commentLikes);

            return(Json(isAdded));
        }
コード例 #22
0
 public void Insert(CommentLikes entity)
 {
     this.DbHelper.Execute(this.Table, proc =>
                           proc.AsInsert(entity, "Id", "Creator", "CreateTime", "IsDeleted", "CommentId", "UserId")
                           );
 }