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

            await _votingService.SubmitVoteForPlace(voteModel);

            var votes = await _votingService.GetVotesForPlaceAsync(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 GetVotesForPlaceAsyncTest()
        {
            Guid  eId = new Guid();
            Event e   = new Event {
                Id = eId
            };
            Place p1 = new Place {
                Id = new Guid("00000000-0000-0000-0000-000000000001"), EventId = eId, Event = e
            };
            Place p2 = new Place {
                Id = new Guid("00000000-0000-0000-0000-000000000002"), EventId = eId, Event = e
            };
            VoteForPlace vp1 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), PlaceId = p1.Id, Place = p1
            };
            VoteForPlace vp2 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000002"), PlaceId = p1.Id, Place = p1
            };
            VoteForPlace vp3 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000003"), PlaceId = p2.Id, Place = p2
            };
            var tcs = new TaskCompletionSource <List <VoteForPlace> >();

            tcs.SetResult(new List <VoteForPlace> {
                vp1, vp2, vp3
            });
            _voteForPlaceRepository.Setup(mock => mock.GetAll()).Returns(tcs.Task);
            var task = await _votingService.GetVotesForPlaceAsync(eId, p1.Id);

            _voteForPlaceRepository.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 <VoteForPlace> {
                vp1, vp2
            }, 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");
        }
        /// <summary>
        ///     Asynchronous method that adds or update en existed place vote based on place vote entity.
        /// </summary>
        /// <param name="voteForPlace">Place vote entity with related</param>
        /// <returns>Returns created place vote entity</returns>
        public virtual async Task <VoteForPlace> AddOrUpdate(VoteForPlace voteForPlace)
        {
            using (var context = EventPlannerContext.Get())
            {
                var entity = Mapper.Map <VoteForPlaceEntity>(voteForPlace);

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

                return(Mapper.Map <VoteForPlace>(entity));
            }
        }
Esempio n. 5
0
        public async Task SubmitVoteForPlaceTest()
        {
            Place p1 = new Place {
                Id = new Guid("00000000-0000-0000-0000-000000000001")
            };
            VoteForPlace vp1 = new VoteForPlace {
                Id = new Guid("00000000-0000-0000-0000-000000000000"), PlaceId = p1.Id, Place = p1
            };
            var tcs = new TaskCompletionSource <VoteForPlace>();

            tcs.SetResult(vp1);
            _voteForPlaceRepository.Setup(mock => mock.AddOrUpdate(vp1)).Returns(tcs.Task);
            var task = await _votingService.SubmitVoteForPlace(vp1);

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

            Assert.AreEqual(task, vp1, "Vote was not submitted correctly");
        }
Esempio n. 6
0
 /// <summary>
 /// Submit Vote for Place and stores it to database
 /// </summary>
 /// <param name="voteForPlace">VoteForPlace to submit</param>
 /// <returns>Submitted VoteForPlace</returns>
 public async Task <VoteForPlace> SubmitVoteForPlace(VoteForPlace voteForPlace)
 {
     return(await _voteForPlaceRepository.AddOrUpdate(voteForPlace));
 }