Exemple #1
0
        private RestaurantPoll LoadPoll(RestaurantPoll poll)
        {
            // busca os ultimos 100 resultados das votações passadas.
            var pollResults = dbContext.PollResults
                              .Where(pr => pr.Date.Date <= poll.Date.Date)
                              .OrderByDescending(pr => pr.Date)
                              .Include(v => v.WinnerRestaurant)
                              .Include(v => v.RestaurantPoll)
                              .Take(100).ToList();

            poll.SetPollResults(pollResults);

            // busca todos os votos (máximo = 5000).
            var votes = dbContext.Votes
                        .Where(v => v.RestaurantPoll.Id == poll.Id)
                        .Include(v => v.Restaurant)
                        .Include(v => v.Voter)
                        .Include(v => v.RestaurantPoll)
                        .Take(5000).ToList();

            poll.SetVotes(votes);

            poll.SetRestaurants(dbContext.Restaurants.ToList());

            return(poll);
        }
        public void Finaliza_As_Votacoes_Anteriores_Caso_Nao_Tiverem_Sido_Finalizadas()
        {
            List <Mock <User> >       mockedUsers       = TestArtifacts.GetMockedUsers();
            List <Mock <Restaurant> > mockedRestaurants = TestArtifacts.GetMockedRestaurants();

            var restaurant1 = TestArtifacts.GetRestaurant1();
            var restaurant2 = TestArtifacts.GetRestaurant2();

            var dia1 = new DateTime(2020, 1, 10);
            var dia2 = new DateTime(2020, 1, 11);

            var poll_Dia1 = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", dia1, new List <Restaurant>()
            {
                restaurant1, restaurant2
            });

            poll_Dia1.AddVote(restaurant1, mockedUsers[0].Object);
            poll_Dia1.AddVote(restaurant1, mockedUsers[1].Object);
            poll_Dia1.AddVote(restaurant1, mockedUsers[2].Object);
            poll_Dia1.AddVote(restaurant2, mockedUsers[3].Object);

            var mockRepo = new Mock <IRestaurantPollRepository>();
            var service  = new RestaurantPollService(mockRepo.Object);

            mockRepo.Setup(r => r.GetUnfinishedPolls(It.IsAny <DateTime>())).Returns(new RestaurantPoll[] { poll_Dia1 });

            service.GetPollByDate(dia2);

            // ao buscar a votação do dia 11/1/2020 deverá chamar o método AddPollResult
            mockRepo.Verify(r => r.AddPollResult(It.IsAny <RestaurantPollResult>()), Times.Once());
            mockRepo.Verify(r => r.GetPollByDate(It.Is <DateTime>(c => c == dia2)), Times.Once());
        }
Exemple #3
0
        public void Specification_Listagem_Somente_Restaurantes_Que_Nao_Venceram_Na_Semana()
        {
            List <Mock <User> >       mockedUsers       = TestArtifacts.GetMockedUsers();
            List <Mock <Restaurant> > mockedRestaurants = TestArtifacts.GetMockedRestaurants();

            var restaurant1 = TestArtifacts.GetRestaurant1();
            var restaurant2 = TestArtifacts.GetRestaurant2();
            var poll1       = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 9, 19), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });
            var pollResults = new List <RestaurantPollResult>()
            {
                new RestaurantPollResult(Guid.NewGuid(), poll1, new DateTime(2020, 9, 13), restaurant1, 42)
            };

            poll1.SetPollResults(pollResults);

            Assert.Single(poll1.GetValidRestaurantsForPoll());
            Assert.Equal(restaurant2, poll1.GetValidRestaurantsForPoll().First());

            var poll2 = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 9, 19), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });
            var pollResults2 = new List <RestaurantPollResult>()
            {
                new RestaurantPollResult(Guid.NewGuid(), poll2, new DateTime(2020, 9, 12), restaurant1, 42)
            };

            poll2.SetPollResults(pollResults2);

            Assert.Equal(2, poll2.GetValidRestaurantsForPoll().Count());
        }
Exemple #4
0
        public void Specification_Somente_Um_Restaurante_Vencedor_Por_Semana()
        {
            List <Mock <User> >       mockedUsers       = TestArtifacts.GetMockedUsers();
            List <Mock <Restaurant> > mockedRestaurants = TestArtifacts.GetMockedRestaurants();

            var restaurant1 = TestArtifacts.GetRestaurant1();
            var restaurant2 = TestArtifacts.GetRestaurant2();
            var poll        = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 9, 13), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });
            var pollResults = new List <RestaurantPollResult>()
            {
                new RestaurantPollResult(Guid.NewGuid(), poll, new DateTime(2020, 9, 6), restaurant1, 42)
            };

            poll.SetPollResults(pollResults);

            var user1 = mockedUsers[0].Object;
            var user2 = mockedUsers[1].Object;

            poll.AddVote(restaurant1, user1);
            Assert.Single(poll.Votes);

            var poll2 = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 9, 12), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });

            poll2.SetPollResults(pollResults);

            Assert.ThrowsAny <Exception>(() => poll2.AddVote(restaurant1, user2));
            Assert.Empty(poll2.Votes);
        }
Exemple #5
0
        public RestaurantPoll NewPoll(string name, DateTime date)
        {
            var restaurantPoll = new RestaurantPoll(Guid.NewGuid(), name, date, restaurantRepository.GetAllRestaurants());

            dbContext.RestaurantPolls.Add(restaurantPoll);
            dbContext.SaveChanges();
            return(restaurantPoll);
        }
Exemple #6
0
        public void Confirm(RestaurantPoll poll)
        {
            dbContext.Attach(poll);

            // salva no banco de dados o(s) voto(s) adicionado(s).
            dbContext.AttachRange(poll.Votes);

            dbContext.SaveChanges();
        }
Exemple #7
0
        public ActionResult <IEnumerable <RestaurantDTO> > GetRestaurantsValidForPoll()
        {
            try
            {
                RestaurantPoll poll = restaurantPollService.GetTodayRestaurantPoll();

                var validRestaurants = poll.GetValidRestaurantsForPoll()
                                       .Select(restaurant => mapper.Map <RestaurantDTO>(restaurant));

                return(Ok(validRestaurants));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return(BadRequest(ex.Message));
            }
        }
Exemple #8
0
        public Response CreatePoll([FromBody] RestaurantPoll model)
        {
            var isValidateData = model != null &&
                                 !string.IsNullOrWhiteSpace(model.RestaurantId) &&
                                 !string.IsNullOrWhiteSpace(model.RestaurantName);

            if (!isValidateData)
            {
                return new Response {
                           IsSuccess = false, ErrorMessage = "Data can not be empty"
                }
            }
            ;

            var poll = _collectionRestaurantpolls.Find(it => true && it.ClosedDate > DateTime.Now).FirstOrDefault();
            var isPollAlreadyExist = poll != null;

            if (isPollAlreadyExist)
            {
                return new Response {
                           IsSuccess = false, ErrorMessage = "Poll has been created already, Can not create any more"
                }
            }
            ;

            var isClosedDateValid = model.ClosedDate > DateTime.Now;

            if (!isClosedDateValid)
            {
                return new Response {
                           IsSuccess = false, ErrorMessage = "CloseDate must more than the current date"
                }
            }
            ;

            model.Id = Guid.NewGuid().ToString();
            var restaurant = _collectionRestaurants.Find(it => it.Id == model.RestaurantId).FirstOrDefault();

            model.MenuPolls = restaurant.Menus.Select(it => new RestaurantMenuPoll {
                Id = it.Id, Name = it.Name
            });
            _collectionRestaurantpolls.InsertOne(model);
            return(new Response {
                IsSuccess = true
            });
        }
        public void Verifica_Ranking()
        {
            List <Mock <User> >       mockedUsers       = TestArtifacts.GetMockedUsers();
            List <Mock <Restaurant> > mockedRestaurants = TestArtifacts.GetMockedRestaurants();

            var restaurant1 = TestArtifacts.GetRestaurant1();
            var restaurant2 = TestArtifacts.GetRestaurant2();
            var poll_Dia1   = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 1, 10), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });

            poll_Dia1.AddVote(restaurant1, mockedUsers[0].Object);
            poll_Dia1.AddVote(restaurant1, mockedUsers[1].Object);
            poll_Dia1.AddVote(restaurant2, mockedUsers[2].Object);

            var ranking = poll_Dia1.GetRanking();

            Assert.Equal(2, ranking.ElementAt(0).Votes);
            Assert.Equal(1, ranking.ElementAt(1).Votes);
            Assert.Equal(restaurant1, ranking.ElementAt(0).Restaurant);
            Assert.Equal(restaurant2, ranking.ElementAt(1).Restaurant);
        }
        public void Verifica_Resultado_Da_Votacao()
        {
            List <Mock <User> >       mockedUsers       = TestArtifacts.GetMockedUsers();
            List <Mock <Restaurant> > mockedRestaurants = TestArtifacts.GetMockedRestaurants();

            var restaurant1 = TestArtifacts.GetRestaurant1();
            var restaurant2 = TestArtifacts.GetRestaurant2();
            var poll_Dia1   = new RestaurantPoll(new Guid("{D6F9DD48-8F80-4B71-8D34-937526BAC306}"), "Almoço", new DateTime(2020, 1, 10), new List <Restaurant>()
            {
                restaurant1, restaurant2
            });

            poll_Dia1.AddVote(restaurant1, mockedUsers[0].Object);
            poll_Dia1.AddVote(restaurant1, mockedUsers[1].Object);
            poll_Dia1.AddVote(restaurant1, mockedUsers[2].Object);
            poll_Dia1.AddVote(restaurant2, mockedUsers[3].Object);

            var result = poll_Dia1.GetResult();

            Assert.Equal(3, result.TotalVotes);
            Assert.Equal(restaurant1, result.WinnerRestaurant);
            Assert.Equal(restaurant1, poll_Dia1.WinnerRestaurant);
        }
Exemple #11
0
 public void Add(RestaurantPoll poll)
 {
     dbContext.RestaurantPolls.Add(poll);
     dbContext.SaveChanges();
 }