from fastapi import APIRouter, HTTPException from genetic_algorithm.genetic_algorithm import genetic_algorithm, load_graph_from_request from schemas import TripRequest router = APIRouter( prefix="/flight", tags=["Flight"], ) @router.post("/get/") async def get_flight(request: TripRequest): graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(request) if request.returnDate: resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to", departure_date = departure_date) resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back", departure_date = departure_date_return) if not resultTo or not resulFrom: raise HTTPException(status_code=404, detail="No valid paths found") return resultTo, resulFrom if not request.returnDate: resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type="to", departure_date = departure_date) if resultTo: resultTo.append({ "back": [] }) else: raise HTTPException(status_code=404, detail="No valid paths found") return resultTo