public async Task SortBySortsRestaurantAsExpected(ActivitiesSorter sorter, string expectedFirstActivityName)
        {
            await this.AddTestingCountryToDb();

            this.DbContext.Destinations.AddRange(new List <Destination>
            {
                new Destination {
                    Id = TestDestinationId, Name = TestDestinationName, CountryId = TestCountryId
                },
                new Destination {
                    Id = SecondTestDestinationId, Name = "Aaa", CountryId = TestCountryId
                },
            });
            this.DbContext.Activities.AddRange(new List <Activity>
            {
                new Activity
                {
                    Name          = "Aaa",
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Recreation,
                    Date          = DateTime.Now.AddDays(1),
                },
                new Activity
                {
                    Name          = TestActivityName,
                    DestinationId = SecondTestDestinationId,
                    Type          = ActivityType.Culture,
                    Date          = DateTime.Now.AddDays(2),
                }, // Destination
                new Activity
                {
                    Name          = SecondTestActivityName,
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Shopping,
                    Date          = DateTime.Now.AddDays(3),
                },
                new Activity
                {
                    Name          = "Zzz",
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Adventure,
                    Date          = DateTime.Now.AddDays(4),
                }, // Type
            });
            await this.DbContext.SaveChangesAsync();

            var activitiesToSort = new ActivityViewModel[]
            {
                new ActivityViewModel
                {
                    Name          = "Aaa",
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Recreation.ToString(),
                    Date          = DateTime.Now.AddDays(1),
                },
                new ActivityViewModel
                {
                    Name          = TestActivityName,
                    DestinationId = SecondTestDestinationId,
                    Type          = ActivityType.Culture.ToString(),
                    Date          = DateTime.Now.AddDays(2),
                },
                new ActivityViewModel
                {
                    Name          = SecondTestActivityName,
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Shopping.ToString(),
                    Date          = DateTime.Now.AddDays(3),
                },
                new ActivityViewModel
                {
                    Name          = "Zzz",
                    DestinationId = TestDestinationId,
                    Type          = ActivityType.Adventure.ToString(),
                    Date          = DateTime.Now.AddDays(4),
                },
            };

            var actual = this.ActivitiesServiceMock.SortBy(activitiesToSort, sorter);

            Assert.Equal(expectedFirstActivityName, actual.First().Name);
        }
Exemplo n.º 2
0
        public IEnumerable <ActivityViewModel> SortBy(ActivityViewModel[] activities, ActivitiesSorter sorter)
        {
            ActivityViewModel[] passedActivities;
            switch (sorter)
            {
            case ActivitiesSorter.Upcoming:
                passedActivities = activities.OrderBy(a => a.Date).Where(a => a.Date < DateTime.UtcNow).ToArray();
                break;

            case ActivitiesSorter.Name:
                passedActivities = activities.OrderBy(a => a.Name).Where(a => a.Date < DateTime.UtcNow).ToArray();
                break;

            case ActivitiesSorter.Type:
                passedActivities = activities.OrderBy(a => a.Type).Where(a => a.Date < DateTime.UtcNow).ToArray();
                break;

            case ActivitiesSorter.Destination:
                passedActivities = activities.OrderBy(a => a.DestinationName).Where(a => a.Date < DateTime.UtcNow).ToArray();
                break;

            default:
                passedActivities = activities.OrderBy(a => a.Date).Where(a => a.Date < DateTime.UtcNow).ToArray();
                break;
            }

            switch (sorter)
            {
            case ActivitiesSorter.Upcoming:
                return(activities.OrderBy(a => a.Date).Where(a => a.Date >= DateTime.UtcNow).ToArray().Concat(passedActivities));

            case ActivitiesSorter.Name:
                return(activities.OrderBy(a => a.Name).Where(a => a.Date >= DateTime.UtcNow).ToArray().Concat(passedActivities));

            case ActivitiesSorter.Type:
                return(activities.OrderBy(a => a.Type).Where(a => a.Date >= DateTime.UtcNow).ToArray().Concat(passedActivities));

            case ActivitiesSorter.Destination:
                return(activities.OrderBy(a => a.DestinationName).Where(a => a.Date >= DateTime.UtcNow).ToArray().Concat(passedActivities));

            default:
                return(activities.OrderBy(a => a.Date).Where(a => a.Date >= DateTime.UtcNow).ToArray().Concat(passedActivities));
            }
        }