public async Task Save(string company, Queue <TripDTO> items)
        {
            while (items.Count > 0)
            {
                var item = items.Dequeue();

                var trip = Mapper.Map <API.Models.TripDTO>(item);

                trip.StreetSegmentGroupKey = await StreetSegmentGroupRepository.FindGroupKey(trip.AlternateKey);

                trip.BicyclePathGroupKey = await BicyclePathGroupRepository.FindGroupKey(trip.AlternateKey);

                // if we've already calculated a segment group, don't repeat the expensive process
                Task <List <StreetSegment> > segmentsTask = null;
                if (trip.StreetSegmentGroupKey == null)
                {
                    segmentsTask = StreetSegmentRepository.Find(Mapper.Map <LineString>(trip.Route));
                }
                Task <List <BicyclePath> > pathsTask = null;
                if (trip.BicyclePathGroupKey == null)
                {
                    pathsTask = BicyclePathRepository.Find(Mapper.Map <LineString>(trip.Route));
                }

                var neighborhoodStartTask = NeighborhoodRepository.Find(Mapper.Map <Point>(item.StartPoint));
                var neighborhoodEndTask   = NeighborhoodRepository.Find(Mapper.Map <Point>(item.EndPoint));
                var patternAreaStartTask  = PatternAreaRepository.Find(Mapper.Map <Point>(item.StartPoint));
                var patternAreaEndTask    = PatternAreaRepository.Find(Mapper.Map <Point>(item.EndPoint));

                trip.NeighborhoodStartKey = (await neighborhoodStartTask)?.Key;
                trip.NeighborhoodEndKey   = (await neighborhoodEndTask)?.Key;
                trip.PatternAreaStartKey  = (await patternAreaStartTask)?.Key;
                trip.PatternAreaEndKey    = (await patternAreaEndTask)?.Key;

                trip.StreetSegmentGroupKey = trip.StreetSegmentGroupKey ??
                                             (await StreetSegmentGroupRepository.CreateGroup(await segmentsTask))?.Key;
                trip.BicyclePathGroupKey = trip.BicyclePathGroupKey ??
                                           (await BicyclePathGroupRepository.CreateGroup(await pathsTask))?.Key;

                // add it to the database
                try
                {
                    var response = await HttpClient.PostAsJsonAsync("trip", trip);

                    response.EnsureSuccessStatusCode();
                }
                catch (HttpRequestException e)
                {
                    Logger.LogWarning("Error saving trip record for {company}:\n{message}", company, e.Message);
                }
            }
        }
        public async void ShouldCreateGroup()
        {
            var now = DateTime.Now;
            //Given

            var segments = new StreetSegment[] {
                Segments[0],
                Segments[1]
            };
            //When
            var group = await Repository.CreateGroup(segments);

            //Then
            Assert.NotNull(group);
            Assert.Equal(1, group.Key);
            Assert.Equal(2, group.Bridges.Count);
        }