Esempio n. 1
0
        public async Task <JsonResult> SubmitVoteForDate(Guid eventId, Guid optionId, Guid usersVoteId, WillAttend?willAttend)
        {
            var userId    = User.Identity.GetUserId();
            var voteModel = new VoteForDate()
            {
                Id         = usersVoteId,
                TimeSlotId = optionId,
                UserId     = userId,
                WillAttend = willAttend
            };

            await _votingService.SubmitVoteForDate(voteModel);

            var votes = await _votingService.GetVotesForDateAsync(eventId, optionId);

            var totalNumberOfVoters = await _votingService.GetTotalNumberOfVotersForEvent(eventId);

            return(Json(new
            {
                Option = new OptionViewModel()
                {
                    UsersVote = MappingHelper.MapToUsersVoteModel(votes, userId),
                    Votes = MappingHelper.MapToVotesViewModel(votes)
                },
                TotalNumberOfVoters = totalNumberOfVoters
            },
                        JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public async Task GetVotesForDateAsyncTest()
        {
            Guid  eId = new Guid();
            Event e   = new Event {
                Id = eId
            };
            TimeSlot t1 = new TimeSlot {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), EventId = eId, Event = e
            };
            TimeSlot t2 = new TimeSlot {
                Id = new Guid("00000000-0000-0000-0000-000000000002"), EventId = eId, Event = e
            };
            VoteForDate vd1 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), TimeSlotId = t1.Id, TimeSlot = t1
            };
            VoteForDate vd2 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000002"), TimeSlotId = t1.Id, TimeSlot = t1
            };
            VoteForDate vd3 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000003"), TimeSlotId = t2.Id, TimeSlot = t2
            };
            var tcs = new TaskCompletionSource <List <VoteForDate> >();

            tcs.SetResult(new List <VoteForDate> {
                vd1, vd2, vd3
            });
            _voteForDateRepository.Setup(mock => mock.GetAll()).Returns(tcs.Task);
            var task = await _votingService.GetVotesForDateAsync(eId, t1.Id);

            _voteForDateRepository.Verify(mock => mock.GetAll(), Times.Once(), "Method GetAll was not called or was called more than once (or its parameters were wrong).");

            CollectionAssert.AreEqual(new List <VoteForDate> {
                vd1, vd2
            }, task.ToList(), "Returned list of votes is not correct");
        }
Esempio n. 3
0
        public async Task GetTotalNumberOfVotersForEventTest()
        {
            Guid  eId = new Guid();
            Event e   = new Event {
                Id = eId
            };
            VoteForPlace vp1 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), UserId = "0"
            };
            VoteForPlace vp2 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), UserId = "1"
            };
            Place p1 = new Place {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), Event = e, EventId = eId, VotesForPlace = new List <VoteForPlace> {
                    vp1, vp2
                }
            };
            Place p2 = new Place {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), Event = e, EventId = eId
            };
            List <Place> places = new List <Place> {
                p1, p2
            };
            VoteForDate vd1 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), UserId = "1"
            };
            VoteForDate vd2 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), UserId = "2"
            };
            TimeSlot t1 = new TimeSlot {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), Event = e, EventId = eId, VotesForDate = new List <VoteForDate> {
                    vd1, vd2
                }
            };
            TimeSlot t2 = new TimeSlot {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), Event = e, EventId = eId
            };
            List <TimeSlot> timeslots = new List <TimeSlot> {
                t1, t2
            };
            var tcs1 = new TaskCompletionSource <IList <Place> >();

            tcs1.SetResult(places);
            var tcs2 = new TaskCompletionSource <IList <TimeSlot> >();

            tcs2.SetResult(timeslots);

            _eventDetailsService.Setup(mock => mock.GetPlacesWithVotes(eId)).Returns(tcs1.Task);
            _eventDetailsService.Setup(mock => mock.GetDatesWithVotes(eId)).Returns(tcs2.Task);
            var task = await _votingService.GetTotalNumberOfVotersForEvent(eId);

            _eventDetailsService.Verify(mock => mock.GetPlacesWithVotes(eId), Times.Once(), "Method GetPlacesWithVotes was not called or was called more than once (or its parameters were wrong).");
            _eventDetailsService.Verify(mock => mock.GetDatesWithVotes(eId), Times.Once(), "Method GetPlacesWithVotes was not called or was called more than once (or its parameters were wrong).");

            Assert.AreEqual(3, task, "Returned number of voters is different to the actual value");
        }
Esempio n. 4
0
        /// <summary>
        ///     Asynchronous method that adds or update en existed date vote based on date vote entity.
        /// </summary>
        /// <param name="voteForDate">Date vote entity with related</param>
        /// <returns>Returns created date vote entity</returns>
        public virtual async Task <VoteForDate> AddOrUpdate(VoteForDate voteForDate)
        {
            using (var context = EventPlannerContext.Get())
            {
                var entity = Mapper.Map <VoteForDateEntity>(voteForDate);

                context.VotesForDates.AddOrUpdate(entity);
                await context.SaveChangesAsync();

                return(Mapper.Map <VoteForDate>(entity));
            }
        }
Esempio n. 5
0
        public async Task SubmitVoteForDateTest()
        {
            TimeSlot t1 = new TimeSlot {
                Id = new Guid("00000000-0000-0000-0000-000000000001")
            };
            VoteForDate vd1 = new VoteForDate {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), TimeSlotId = t1.Id, TimeSlot = t1
            };
            var tcs = new TaskCompletionSource <VoteForDate>();

            tcs.SetResult(vd1);
            _voteForDateRepository.Setup(mock => mock.AddOrUpdate(vd1)).Returns(tcs.Task);
            var task = await _votingService.SubmitVoteForDate(vd1);

            _voteForDateRepository.Verify(mock => mock.AddOrUpdate(vd1), Times.Once(), "Method AddOrUpdate was not called or was called more than once (or its parameters were wrong).");

            Assert.AreEqual(task, vd1, "Vote was not submitted correctly.");
        }
Esempio n. 6
0
 /// <summary>
 /// Submit Vote for date and stores it to database
 /// </summary>
 /// <param name="voteForDate">VoteForDate to submit</param>
 /// <returns>Submitted VoteForDate</returns>
 public async Task <VoteForDate> SubmitVoteForDate(VoteForDate voteForDate)
 {
     return(await _voteForDateRepository.AddOrUpdate(voteForDate));
 }