예제 #1
0
        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"));
        }