Пример #1
0
        public ActionResult <List <Wagon> > GetFreeSeats([FromRoute] int id, [FromQuery] int from, [FromQuery] int to, [FromQuery] DateTime date)
        {
            var ride  = _rideService.GetRide(id);
            var route = _stopToRouteService.GetStops(ride.RouteId);

            var fromNo = route.Where(x => x.TrainStopId == from).Select(x => x.StopNo).Single();
            var toNo   = route.Where(x => x.TrainStopId == to).Select(x => x.StopNo).Single();

            // all tickets for given ride on given route section
            var rideTickets = _ticketsService.GetRideTickets(id)
                              .Where(t => t.RideDate.Date == date.Date).Where(t =>
            {
                var ticketFromNo = route.Where(x => x.TrainStopId == t.FromId).Select(x => x.StopNo).Single();
                var ticketToNo   = route.Where(x => x.TrainStopId == t.ToId).Select(x => x.StopNo).Single();
                return(!(fromNo > ticketToNo || ticketFromNo > toNo));
            });

            // all seats for given ride
            var seats = Enumerable.Range(1, ride.Train.Wagons).Select(i =>
                                                                      new Wagon
            {
                wagonNo = i,
                seats   = Enumerable.Range(1, ride.Train.Seats).Select(j =>
                {
                    if (!rideTickets.Any())
                    {
                        return(true);
                    }
                    return(rideTickets.SingleOrDefault(t => t.SeatNo == j && t.WagonNo == i) == null);
                }).ToList(),
                type = (WagonType)ride.Train.Type
            }
                                                                      ).ToList();

            return(seats);
        }