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) { await _trainCaching.Save(train, trainInst, bookingRef); 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)); }