Example #1
0
        public async Task <Reservation> ReserveAsync(TrainId trainId, SeatsRequested seatsRequested)
        {
            // get the train
            var train = await _trainDataService.GetTrain(trainId);

            if (train.MustNotExceedTrainCapacity(seatsRequested))
            {
                var reservationAttenpt = train.BuildReservationAttempt(seatsRequested);

                if (reservationAttenpt.IsFulFilled())
                {
                    var bookingRef = await _bookingReference.GetBookingReference();

                    reservationAttenpt.AssignBookingReference(bookingRef);

                    await _trainDataService.ReserveAsync(reservationAttenpt);

                    return(reservationAttenpt.Confirm());
                }
            }

            return(new ReservationFailure(train.TrainId));
        }
        public async Task <string> Reserve(string trainId, int seatsRequestedCount)
        {
            List <Seat> availableSeats = new List <Seat>();
            int         count          = 0;

            // get the train
            var jsonTrain = await _trainDataService.GetTrain(trainId);

            var trainInst = new Train(jsonTrain);

            if (trainInst.ReservedSeats + seatsRequestedCount <= Math.Floor(ThreasholdManager.GetMaxRes() * trainInst.GetMaxSeat()))
            {
                var numberOfReserv = 0;
                // find seats to reserve
                for (int index = 0, i = 0; index < trainInst.Seats.Count; index++)
                {
                    var each = trainInst.Seats[index];
                    if (each.BookingRef == "")
                    {
                        i++;
                        if (i <= seatsRequestedCount)
                        {
                            availableSeats.Add(each);
                        }
                    }
                }

                foreach (var unused in availableSeats)
                {
                    count++;
                }

                var reservedSets = 0;

                string bookingRef;
                if (count != seatsRequestedCount)
                {
                    return($"{{\"train_id\": \"{trainId}\", \"booking_reference\": \"\", \"seats\": []}}");
                }
                else
                {
                    bookingRef = await _bookingReferenceService.GetBookingReference();

                    foreach (var availableSeat in availableSeats)
                    {
                        availableSeat.BookingRef = bookingRef;
                        numberOfReserv++;
                        reservedSets++;
                    }
                }

                if (numberOfReserv == seatsRequestedCount)
                {
                    await _trainCaching.Save(trainId, trainInst, bookingRef);

                    await _trainDataService.BookSeats(trainId, bookingRef, availableSeats);

                    return
                        ($"{{\"train_id\": \"{trainId}\", \"booking_reference\": \"{bookingRef}\", \"seats\": {dumpSeats(availableSeats)}}}");
                }
            }
            return($"{{\"train_id\": \"{trainId}\", \"booking_reference\": \"\", \"seats\": []}}");
        }