コード例 #1
0
        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\": []}}");
        }
コード例 #2
0
        public async Task <string> Reserve(string train, int seats)
        {
            List <Seat> availableSeats = new List <Seat>();
            int         count          = 0;
            var         result         = string.Empty;
            string      bookingRef;

            // get the train
            var JsonTrain = await GetTrain(train);

            result = JsonTrain;

            var trainInst = new Train(JsonTrain);

            if ((trainInst.ReservedSeats + seats) <= 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 <= seats)
                        {
                            availableSeats.Add(each);
                        }
                    }
                }

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

                var reservedSets = 0;


                if (count != seats)
                {
                    return(string.Format("{{\"train_id\": \"{0}\", \"booking_reference\": \"\", \"seats\": []}}",
                                         train));
                }
                else
                {
                    using (var client = new HttpClient())
                    {
                        bookingRef = await GetBookRef(client);
                    }

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

                if (numberOfReserv == seats)
                {
                    using (var client = new HttpClient())
                    {
                        var value = new MediaTypeWithQualityHeaderValue("application/json");
                        client.BaseAddress = new Uri(urITrainDataService);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(value);

                        if (reservedSets == 0)
                        {
                            Console.WriteLine("Reserved seat(s): ", reservedSets);
                        }

                        // HTTP POST
                        HttpContent resJSON  = new StringContent(buildPostContent(train, bookingRef, availableSeats), Encoding.UTF8, "application/json");
                        var         response = await client.PostAsync("reserve", resJSON);

                        response.EnsureSuccessStatusCode();

                        var todod = "[TODOD]";


                        return(string.Format(
                                   "{{\"train_id\": \"{0}\", \"booking_reference\": \"{1}\", \"seats\": {2}}}",
                                   train,
                                   bookingRef,
                                   dumpSeats(availableSeats)));
                    }
                }
            }

            return(string.Format("{{\"train_id\": \"{0}\", \"booking_reference\": \"\", \"seats\": []}}", train));
        }