/// <summary> /// Method returns the previous booking details /// </summary> /// <returns></returns> public async Task <List <BookingDetails> > FetchBookings(TrainSearch search) { List <BookingDetails> lstBookings = new List <BookingDetails>(); try { DateTime dateOfJourney = DateTime.Parse(search.DateOfJourney.ToString()); using (SqlConnection sqlConnection = new SqlConnection(_appSettings.Value.DbConnection)) { await sqlConnection.OpenAsync(); string command = "Select trainId,Count(*) as 'count' from Bookings where source=" + search.Source + " and destination=" + search.Destination + " and dateOfJourney = '" + dateOfJourney.ToString("yyyy-MM-dd") + "' group by trainId"; SqlCommand cmd = new SqlCommand(command, sqlConnection); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { BookingDetails booking = new BookingDetails(); booking.TrainId = Convert.ToInt32(rdr["trainId"]); booking.Count = Convert.ToInt32(rdr["count"]); lstBookings.Add(booking); } sqlConnection.Close(); } return(lstBookings); } catch (Exception) { throw; } }
/// <summary> /// Returns the list of Trains based on search criteria /// </summary> /// <returns></returns> public async Task <List <TrainDetails> > FetchTrains(TrainSearch search) { _logger.LogInformation("Accessed FetchTrains Method in BusinessManager"); try { DataTable dt = await _trainData.FetchTrains(search); List <TrainDetails> trainDetailslist = new List <TrainDetails>(); trainDetailslist = (from DataRow dr in dt.Rows select new TrainDetails() { Seats = Convert.ToInt32(dr["seats"]), Arrival = dr["arrival"].ToString(), Departure = dr["departure"].ToString(), Destination = dr["destination"].ToString(), RouteId = Convert.ToInt32(dr["routeId"]), Source = dr["source"].ToString(), TrainNumber = dr["trainNumber"].ToString(), TrainName = dr["trainName"].ToString(), TrainId = Convert.ToInt32(dr["trainId"]), IsAvailable = true }).ToList(); DateTime dateOfJourney = DateTime.Parse(search.DateOfJourney.ToString()); var bookingList = await _trainData.FetchBookings(search); if (bookingList.Count > 0) { foreach (var trains in trainDetailslist) { foreach (var bookings in bookingList) { if (bookings.TrainId == trains.TrainId) { int seats = bookings.Count; trains.Seats = trains.Seats - seats; if (trains.Seats <= 0) { trains.IsAvailable = false; } } } } } return(trainDetailslist); } catch (Exception) { throw; } }
public async Task <IActionResult> FetchTrains([FromBody] TrainSearch search) { _logger.LogInformation("Accessed FetchTrains Method"); try { var listTrains = await _businessManager.FetchTrains(search); return(Ok(listTrains)); } catch (Exception ex) { _logger.LogInformation("Error while accessing stations data from database", ex.Message); return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, "There is an error while processing your request")); } }
/// <summary> /// Returns the Trains based on Search Criteria /// </summary> /// <returns></returns> public async Task <DataTable> FetchTrains(TrainSearch search) { try { using (SqlConnection sqlConnection = new SqlConnection(_appSettings.Value.DbConnection)) { await sqlConnection.OpenAsync(); SqlDataAdapter da = new SqlDataAdapter("FetchTrains", sqlConnection); da.SelectCommand.CommandType = CommandType.StoredProcedure; da.SelectCommand.Parameters.Add("@from", SqlDbType.Int).Value = search.Source; da.SelectCommand.Parameters.Add("@to", SqlDbType.Int).Value = search.Destination; DataTable table = new DataTable(); da.Fill(table); return(table); } } catch (Exception) { throw; } }