Ejemplo n.º 1
0
        public async Task SortBySortsRestaurantAsExpected(RestaurantSorter sorter, string expectedFirstRestaurantName)
        {
            await this.AddTestingDestinationToDb();

            this.DbContext.Restaurants.AddRange(new List <Restaurant>
            {
                new Restaurant
                {
                    Name          = TestRestaurantName,
                    DestinationId = TestDestinationId,
                    Type          = RestaurantType.Bar,
                    AverageRating = 0,
                },
                new Restaurant
                {
                    Name          = SecondTestRestaurantName,
                    DestinationId = SecondTestDestinationId,
                    Type          = RestaurantType.CoffeeShop,
                    AverageRating = 1,
                },
                new Restaurant
                {
                    Name          = "Aaaaa",
                    DestinationId = SecondTestDestinationId,
                    Type          = RestaurantType.FineDining,
                    AverageRating = 2,
                },
                new Restaurant
                {
                    Name          = "Zzzzz",
                    DestinationId = SecondTestDestinationId,
                    Type          = RestaurantType.FineDining,
                    AverageRating = 5,
                },
            });
            await this.DbContext.SaveChangesAsync();

            var expected = new RestaurantViewModel[]
            {
                new RestaurantViewModel
                {
                    Name            = TestRestaurantName,
                    DestinationId   = SecondTestDestinationId,
                    DestinationName = "Zzz",
                    Type            = RestaurantType.Bar.ToString(),
                    AverageRating   = 0,
                },
                new RestaurantViewModel
                {
                    Name            = SecondTestRestaurantName,
                    DestinationId   = TestDestinationId,
                    DestinationName = "Aaa",
                    Type            = RestaurantType.CoffeeShop.ToString(),
                    AverageRating   = 1,
                },
                new RestaurantViewModel
                {
                    Name            = "Aaa",
                    DestinationId   = SecondTestDestinationId,
                    DestinationName = "Aaa",
                    Type            = RestaurantType.FineDining.ToString(),
                    AverageRating   = 2,
                },
                new RestaurantViewModel
                {
                    Name            = "Zzz",
                    DestinationId   = SecondTestDestinationId,
                    DestinationName = "Aaa",
                    Type            = RestaurantType.FineDining.ToString(),
                    AverageRating   = 5,
                },
            };

            var actual = this.RestaurantsServiceMock.SortBy(expected, sorter);

            Assert.Equal(expectedFirstRestaurantName, actual.First().Name);
        }
Ejemplo n.º 2
0
        public IEnumerable <RestaurantViewModel> SortBy(RestaurantViewModel[] restaurants, RestaurantSorter sorter)
        {
            switch (sorter)
            {
            case RestaurantSorter.Name:
                return(restaurants.OrderBy(d => d.Name).ToArray());

            case RestaurantSorter.Rating:
                return(restaurants.OrderByDescending(d => d.AverageRating).ToArray());

            case RestaurantSorter.Type:
                return(restaurants.OrderBy(d => d.Type).ToArray());

            case RestaurantSorter.Destination:
                return(restaurants.OrderBy(d => d.DestinationName).ToArray());

            default:
                return(restaurants.OrderBy(d => d.Name).ToArray());
            }
        }