コード例 #1
0
        public bool CanBook(int journeyId, int passengers)
        {
            var timetables = _timeTables.All();
            var allEntries = timetables.SelectMany(x => x.Entries).OrderBy(x => x.Time).ToList();

            foreach (var timetable in allEntries)
            {
                var ferry = _ferryService.NextFerryAvailableFrom(timetable.OriginId, timetable.Time);

                if (timetable.Id == journeyId)
                {
                    var bookings  = _bookings.All().Where(x => x.JourneyId == journeyId);
                    var seatsLeft = ferry.Passengers - bookings.Sum(x => x.Passengers);
                    return(seatsLeft >= passengers);
                }
            }
            return(false);
        }
コード例 #2
0
        public IEnumerable <AvailableCrossing> GetAvailableCrossings(TimeSpan time, int fromPort, int toPort)
        {
            var ports      = new Ports().All();
            var timetables = _timeTables.All();

            var allEntries = timetables.SelectMany(x => x.Entries).OrderBy(x => x.Time).ToList();

            foreach (var timetable in allEntries)
            {
                var origin      = ports.Single(x => x.Id == timetable.OriginId);
                var destination = ports.Single(x => x.Id == timetable.DestinationId);
                var ferry       = _ferryService.NextFerryAvailableFrom(timetable.OriginId, timetable.Time);

                if (toPort == destination.Id && fromPort == origin.Id)
                {
                    if (timetable.Time >= time)
                    {
                        var bookings  = _bookings.All().Where(x => x.JourneyId == timetable.Id);
                        var seatsLeft = ferry.Passengers - bookings.Sum(x => x.Passengers);
                        if (seatsLeft > 0)
                        {
                            yield return(new AvailableCrossing
                            {
                                Arrive = timetable.Time.Add(timetable.JourneyTime),
                                FerryName = ferry.Name,
                                SeatsLeft = ferry.Passengers - bookings.Sum(x => x.Passengers),
                                SetOff = timetable.Time,
                                OriginPort = origin.Name,
                                DestinationPort = destination.Name,
                                JourneyId = timetable.Id
                            });
                        }
                    }
                }
            }
        }