public async Task <IActionResult> GetAsync([FromQuery] string from, [FromQuery] string to, [FromQuery] string start)
        {
            // Check for errors
            if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to) || string.IsNullOrEmpty(start))
            {
                _logger.LogWarning("Invalid parameters.");
                return(BadRequest());
            }

            // Get all routes
            var routesResponse = await _client.GetAsync("/data-exchange/htl-homework/travelPlan.json");

            routesResponse.EnsureSuccessStatusCode();
            var responseBody = await routesResponse.Content.ReadAsStringAsync();

            var routes = JsonSerializer.Deserialize <Route[]>(responseBody);

            // Find the connection
            var finder = new ConnectionFinder(routes);
            var trip   = finder.FindConnection(from, to, start);

            if (trip == null)
            {
                _logger.LogWarning("No connection found.");
                return(NotFound());
            }

            return(Ok(trip));
        }
        public ActionResult <TripWithLocationsAPIResult> GetTripWithLocations(string from, string to, string start)
        {
            var connectionResult = connectionFinder.FindConnection(from, to, start);

            if (connectionResult == null)
            {
                return(NotFound());
            }
            return(ConvertConnectionResult(connectionResult));
        }
        public async Task <ActionResult> Get([FromQuery] string from, [FromQuery] string to, [FromQuery] string start)
        {
            var routesResponse = await client.GetAsync("https://cddataexchange.blob.core.windows.net/data-exchange/htl-homework/travelPlan.json");

            routesResponse.EnsureSuccessStatusCode();
            var json = await routesResponse.Content.ReadAsStringAsync();

            var routes          = JsonSerializer.Deserialize <IEnumerable <Route> >(json);
            ConnectionFinder cf = new ConnectionFinder(routes);
            var trips           = cf.FindConnection(from, to, start);

            if (trips == null)
            {
                return(NotFound());
            }
            return(Ok(trips));
        }
Exemple #4
0
        public async Task <IActionResult> Get([FromQuery] string from, [FromQuery] string to, [FromQuery] string start)
        {
            var client = _factory.CreateClient();
            var resp   = await client.GetAsync("https://cddataexchange.blob.core.windows.net/data-exchange/htl-homework/travelPlan.json");

            resp.EnsureSuccessStatusCode();
            var routes = JsonSerializer.Deserialize <Route[]>(await resp.Content.ReadAsStringAsync());
            var finder = new ConnectionFinder(routes);
            var trip   = finder.FindConnection(from, to, start);

            if (trip == null)
            {
                return(NotFound());
            }

            return(Ok(new {
                depart = trip.FromCity,
                departureTime = trip.Leave,
                arrive = trip.ToCity,
                arrivalTime = trip.Arrive,
            }));
        }