コード例 #1
0
ファイル: PostController.cs プロジェクト: mirkoman007/Pigeon
        public IActionResult UpdatePostReaction([FromBody] ReactionCommand model)
        {
            if (_context.Posts.Find(model.PostID) == null)
            {
                return(NotFound("Post with this postId does not exist"));
            }
            if (_context.Users.Find(model.UserID) == null)
            {
                return(NotFound("User with this userId does not exist"));
            }
            if (!_context.Reactions.Where(x => x.Value == model.ReactionName).Any())
            {
                return(NotFound("Reaction with this name does not exist"));
            }
            var allPostsReactions = _context.PostReactions.Where(x => x.PostId == model.PostID).ToList();

            foreach (var item in allPostsReactions)
            {
                if (item.UserId == model.UserID)
                {
                    item.ReactionId = _context.Reactions.Where(x => x.Value == model.ReactionName).First().Idreaction;
                    _context.SaveChanges();
                    return(Ok());
                }
            }
            return(BadRequest("Error while trying to update post reaction"));
        }
コード例 #2
0
ファイル: PostController.cs プロジェクト: mirkoman007/Pigeon
        public IActionResult AddPostReaction([FromBody] ReactionCommand model)
        {
            if (_context.Posts.Find(model.PostID) == null)
            {
                return(NotFound("Post with this postId does not exist"));
            }
            if (_context.Users.Find(model.UserID) == null)
            {
                return(NotFound("User with this userId does not exist"));
            }
            if (!_context.Reactions.Where(x => x.Value == model.ReactionName).Any())
            {
                return(NotFound("Reaction with this name does not exist"));
            }
            var allPostsReactions = _context.PostReactions.Where(x => x.PostId == model.PostID).ToList();

            foreach (var item in allPostsReactions)
            {
                if (item.UserId == model.UserID)
                {
                    return(BadRequest("Reaction to this post already exists from this user"));
                }
            }
            var postReaction = new PostReaction
            {
                PostId     = model.PostID,
                ReactionId = _context.Reactions.Where(x => x.Value == model.ReactionName).First().Idreaction,
                UserId     = model.UserID
            };

            _context.PostReactions.Add(postReaction);
            _context.SaveChanges();
            return(Ok());
        }