public async Task <IActionResult> UpvoteGame(int userId, int gameId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (await _boardologyRepo.GetGame(gameId) == null)
            {
                return(NotFound());
            }

            var upvote = await _votesRepo.GetUpvote(userId, gameId);

            var downvote = await _votesRepo.GetDownvote(userId, gameId);

            if (upvote != null)
            {
                await _votesRepo.DecreaseUpvotes(gameId);

                _boardologyRepo.Delete(upvote);
                if (await _boardologyRepo.SaveAll())
                {
                    return(Ok());
                }
                return(BadRequest("Failed to remove upvote"));
            }



            upvote = new Upvote
            {
                UpVoterId = userId,
                GameId    = gameId
            };

            _boardologyRepo.Add(upvote);

            await _votesRepo.IncreaseUpvotes(gameId);

            if (downvote != null)
            {
                await _votesRepo.DecreaseDownvotes(gameId);

                _boardologyRepo.Delete(downvote);
            }


            if (await _boardologyRepo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to upvote game"));
        }
        public async Task <IActionResult> AddToCollection(int userId, int gameId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }


            if (await _collectionRepo.GetCollectionItem(userId, gameId) != null)
            {
                return(BadRequest("This item is already in your collection"));
            }

            var collection = new Collection
            {
                UserId = userId,
                GameId = gameId
            };

            _boardologyRepo.Add(collection);

            if (await _boardologyRepo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add to collection"));
        }
Exemple #3
0
        public async Task <IActionResult> AddComment(int userId, int gameId, Comment comment)
        {
            if (comment == null)
            {
                throw new Exception("No comment");
            }
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (await _repo.GetGame(gameId) == null)
            {
                return(NotFound());
            }

            comment = new Comment
            {
                UserId  = userId,
                GameId  = gameId,
                Content = comment.Content
            };

            _repo.Add(comment);
            await _commentsRepo.IncreaseComments(gameId);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add comment"));
        }
Exemple #4
0
        public async Task <IActionResult> UpvoteGame(int userId, int gameId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var upvote = await _repo.GetUpvote(userId, gameId);

            if (upvote != null)
            {
                return(BadRequest("You already upvoted this game"));
            }

            if (await _repo.GetGame(gameId) == null)
            {
                return(NotFound());
            }

            upvote = new Upvote
            {
                UpVoterId = userId,
                GameId    = gameId
            };

            _repo.Add(upvote);

            await _repo.IncreaseUpvotes(gameId);


            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to upvote game"));
        }