Exemplo n.º 1
0
        public VoteDTO AddVoteToRound(VoteCreateUpdateDTO vote, int roundId)
        {
            var newVote = new Vote {
                Estimate = vote.Estimate, UserId = vote.UserId, RoundId = roundId
            };

            this.context.Votes.Add(newVote);
            this.context.SaveChanges();

            return(new VoteDTO
            {
                Estimate = newVote.Estimate,
                Id = newVote.Id,
                UserId = newVote.UserId
            });
        }
        public async Task AddVoteToRound_given_vote_and_itemId_returns_new_voteDto()
        {
            using (var connection = await this.CreateConnectionAsync())
                using (var context = await this.CreateContextAsync(connection))
                {
                    var entity = this.CreateDummySessionEntity();
                    context.Sessions.Add(entity);
                    context.SaveChanges();

                    var id         = entity.Items[0].Id;
                    var repository = new SessionRepository(context);

                    var round = repository.AddRoundToSessionItem(id);

                    var vote = new VoteCreateUpdateDTO {
                        Estimate = 13, UserId = 42
                    };

                    var dto = repository.AddVoteToRound(vote, round.Id);
                    Assert.Equal(1, dto.Id);
                    Assert.Equal(13, dto.Estimate);
                    Assert.Equal(42, dto.UserId);
                }
        }
        public async Task AddVoteToRound_given_vote_and_itemId_inserts_vote()
        {
            using (var connection = await this.CreateConnectionAsync())
                using (var context = await this.CreateContextAsync(connection))
                {
                    var entity = this.CreateDummySessionEntity();
                    context.Sessions.Add(entity);
                    context.SaveChanges();

                    var id         = entity.Items[0].Id;
                    var repository = new SessionRepository(context);

                    var round = repository.AddRoundToSessionItem(id);

                    Assert.Equal(0, entity.Items[0].Rounds.ToList()[0].Votes.Count);

                    var vote = new VoteCreateUpdateDTO {
                        Estimate = 13, UserId = 42
                    };

                    repository.AddVoteToRound(vote, round.Id);
                    Assert.Equal(1, entity.Items[0].Rounds.ToList()[0].Votes.Count);
                }
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Vote([FromHeader(Name = "PPAuthorization")] string authToken, string sessionKey, [FromBody] VoteCreateUpdateDTO vote)
        {
            if (!SecurityFilter.RequestIsValid(authToken, sessionKey, this.userStateManager))
            {
                return(this.Unauthorized());
            }

            var session = await this.sessionRepository.FindByKeyAsync(sessionKey);

            if (session == null)
            {
                return(this.StatusCode(404, "Session not found"));
            }

            var currentItem = SessionUtils.GetCurrentActiveItem(session.Items, session.Users.Count);

            if (!currentItem.HasValue)
            {
                return(this.StatusCode(404, "Active Item not found"));
            }

            var currentRound = SessionUtils.GetCurrentActiveRound(currentItem.ValueOrDefault().Rounds.ToList(), session.Users.Count);

            if (!currentRound.HasValue)
            {
                return(this.StatusCode(404, "Active Round not found"));
            }

            var userState = this.userStateManager.GetState(authToken.Replace("Bearer ", string.Empty)).ValueOrDefault();

            this.sessionRepository.AddVoteToRound(
                new VoteCreateUpdateDTO {
                Estimate = vote.Estimate, UserId = userState.Id
            },
                currentRound.ValueOrDefault().Id);

            return(this.Ok());
        }