Esempio n. 1
0
 public IHttpActionResult PostACommentReaction(CommentReaction commentReaction)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest());
     }
     db.CommentReactions.Add(commentReaction);
     db.SaveChanges();
     return(Created(new Uri(Request.RequestUri + "/" + commentReaction.Id), commentReaction));
 }
Esempio n. 2
0
        public async Task <IActionResult> SetCommentReaction(Guid id, CommentReaction reaction)
        {
            var result = await this.CommentService.SetCommentReaction(await this.CurrentUserModel(), id, reaction);

            if (result == null || result.IsNotFoundResult)
            {
                return(NotFound());
            }
            if (!result.IsSuccess)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            return(Ok(result));
        }
Esempio n. 3
0
        public void DownvoteComment(int id)
        {
            var token = HttpContext.Request.Cookies["user-token"];

            var             myprofile       = _profileRepository.GetUserByToken(token);
            CommentReaction commentReaction = new CommentReaction
            {
                UserId    = myprofile.Id,
                CommentId = id,
                Upvote    = false,
                AddedDate = DateTime.Now
            };

            _reactionRepository.AddCommentReaction(commentReaction);
        }
Esempio n. 4
0
      public IHttpActionResult UpdateACommentReaction(long id, CommentReaction commentReaction)
      {
          if (!ModelState.IsValid)
          {
              return(BadRequest());
          }
          var reactionInDb = db.CommentReactions.Where(i => i.Id == id).FirstOrDefault();

          if (reactionInDb == null)
          {
              return(NotFound());
          }
          reactionInDb.IsHelpfull = commentReaction.IsHelpfull;
          reactionInDb.IsLiked    = commentReaction.IsLiked;
          db.SaveChanges();
          return(Ok());
      }
Esempio n. 5
0
        public ActionResult ReactComment(FormCollection collection)
        {
            User user = getAuthUser();

            if (user == null)
            {
                return(HttpNotFound());
            }
            int             commentid  = int.Parse(collection["CommentID"] ?? Request.Form["CommentID"]);
            int             reactionid = int.Parse(collection["ReactionId"] ?? Request.Form["ReactionId"]);
            CommentReaction reaction   = null;

            if (db.CommentReactions.Any(p => p.CommentID == commentid && p.UserID == user.UserId))
            {
                reaction = db.CommentReactions.Single(p => p.CommentID == commentid && p.UserID == user.UserId);
                if (reaction.ReactionID == reactionid)
                {
                    db.CommentReactions.Remove(reaction);
                    db.SaveChanges();
                    return(Json(new { message = "unreacted", status = "success" }));
                }
                else
                {
                    return(Json(new { status = "error", message = "you already reacted to that post" }));
                }
            }
            else
            {
                reaction = new CommentReaction()
                {
                    ReactionID = reactionid, CommentID = commentid, UserID = user.UserId, CommentReactionId = 0
                };
                db.CommentReactions.Add(reaction);
                db.SaveChanges();
            }
            db.Dispose();
            db = new DB();

            string format = Request?["format"];

            //if (format == "json")
            return(Json(new { message = "reacted", status = "success" }));

            //return RedirectToAction("Details", new { id = reaction.PostID });
        }
        public async Task SwitchReaction(int commentId, string userId, CommentReactionType type)
        {
            var existingReaction =
                await _commentReactionRepo.GetOneByCondition(r => r.CommentId == commentId && r.UserId == userId);

            if (existingReaction != null)
            {
                _commentReactionRepo.Remove(existingReaction);
                return;
            }
            var commentReaction = new CommentReaction()
            {
                UserId    = userId,
                CommentId = commentId,
                Type      = type,
            };
            await _commentReactionRepo.Add(commentReaction);
        }
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var commentReaction = new CommentReaction
                {
                    Id        = request.Id,
                    CommentId = request.CommentId,
                    Like      = request.Like,
                    DisLike   = request.DisLike
                };


                _context.CommentReactions.Add(commentReaction);
                var result = await _context.SaveChangesAsync() > 0;

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

                throw new Exception();
            }
Esempio n. 8
0
        public async Task <IActionResult> SetCommentReaction(Guid id, CommentReaction reaction)
        {
            var result = await CommentService.SetCommentReaction(await CurrentUserModel(), id, reaction);

            if (result == null || result.IsNotFoundResult)
            {
                return(NotFound());
            }
            if (!result.IsSuccess)
            {
                if (result.IsAccessDeniedResult)
                {
                    return(StatusCode((int)HttpStatusCode.Forbidden));
                }
                if (result.Messages?.Any() ?? false)
                {
                    return(StatusCode((int)HttpStatusCode.BadRequest, result.Messages));
                }
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            return(Ok(result));
        }
Esempio n. 9
0
        public async Task <OperationResult <bool> > SetCommentReaction(User user, Guid id, CommentReaction reaction)
        {
            var sw      = Stopwatch.StartNew();
            var result  = false;
            var errors  = new List <Exception>();
            var comment = DbContext.Comments.FirstOrDefault(x => x.RoadieId == id);

            if (comment == null)
            {
                return(new OperationResult <bool>(true, string.Format("Comment Not Found [{0}]", id)));
            }
            var userCommentReaction =
                DbContext.CommentReactions.FirstOrDefault(x => x.CommentId == comment.Id && x.UserId == user.Id);

            if (userCommentReaction == null)
            {
                userCommentReaction = new data.CommentReaction
                {
                    CommentId = comment.Id,
                    UserId    = user.Id.Value
                };
                DbContext.CommentReactions.Add(userCommentReaction);
            }

            userCommentReaction.Reaction = reaction == CommentReaction.Unknown ? null : reaction.ToString();
            await DbContext.SaveChangesAsync();

            ClearCaches(comment);
            var userCommentReactions = (from cr in DbContext.CommentReactions
                                        where cr.CommentId == comment.Id
                                        select cr).ToArray();
            var additionalData = new Dictionary <string, object>();

            additionalData.Add("likedCount",
                               userCommentReactions.Where(x => x.ReactionValue == CommentReaction.Like).Count());
            additionalData.Add("dislikedCount",
                               userCommentReactions.Where(x => x.ReactionValue == CommentReaction.Dislike).Count());
            sw.Stop();
            result = true;
            return(new OperationResult <bool>
            {
                AdditionalClientData = additionalData,
                Data = result,
                IsSuccess = result,
                Errors = errors,
                OperationTime = sw.ElapsedMilliseconds
            });
        }
Esempio n. 10
0
 public void RemoveCommentReaction(CommentReaction commentReaction)
 {
     _context.Remove(commentReaction);
     _context.SaveChanges();
 }
Esempio n. 11
0
 public void AddCommentReaction(CommentReaction commentReaction)
 {
     _context.Add(commentReaction);
     _context.SaveChanges();
 }