2024-06-07 23:47:13 +04:00
|
|
|
from typing import List
|
|
|
|
|
2024-06-06 22:49:43 +04:00
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
|
|
|
|
from genetic_algorithm.genetic_algorithm import genetic_algorithm, load_graph_from_request
|
2024-06-07 23:47:13 +04:00
|
|
|
from schemas import TripRequest, FlightSegment, TripOption
|
2024-06-06 22:49:43 +04:00
|
|
|
|
|
|
|
router = APIRouter(
|
|
|
|
prefix="/flight",
|
|
|
|
tags=["Flight"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-06-09 18:46:39 +04:00
|
|
|
@router.post("/get/", response_model=List[TripOption])
|
2024-06-06 22:49:43 +04:00
|
|
|
async def get_flight(request: TripRequest):
|
2024-06-07 23:29:55 +04:00
|
|
|
graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(
|
|
|
|
request)
|
2024-06-06 22:49:43 +04:00
|
|
|
if request.returnDate:
|
|
|
|
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
|
2024-06-07 23:29:55 +04:00
|
|
|
type="to", departure_date=departure_date)
|
|
|
|
resultFrom = genetic_algorithm(start=end_point, end=start_point, graph=graphBack, flights_data=flightsDataBack,
|
|
|
|
type="back", departure_date=departure_date_return)
|
|
|
|
if not resultTo or not resultFrom:
|
|
|
|
raise HTTPException(status_code=404, detail="No valid paths found")
|
|
|
|
|
|
|
|
trip_options = []
|
|
|
|
for to_trip in resultTo:
|
|
|
|
for back_trip in resultFrom:
|
|
|
|
to_segments = [FlightSegment(**segment) for segment in to_trip["to"]]
|
|
|
|
back_segments = [FlightSegment(**segment) for segment in back_trip["back"]]
|
2024-06-09 18:46:39 +04:00
|
|
|
trip_options.append(TripOption(to=to_segments, back=back_segments))
|
2024-06-07 23:29:55 +04:00
|
|
|
|
2024-06-07 23:47:13 +04:00
|
|
|
return trip_options
|
2024-06-06 22:49:43 +04:00
|
|
|
|
2024-06-07 23:29:55 +04:00
|
|
|
else:
|
|
|
|
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
|
|
|
|
type="to", departure_date=departure_date)
|
|
|
|
if not resultTo:
|
2024-06-06 22:49:43 +04:00
|
|
|
raise HTTPException(status_code=404, detail="No valid paths found")
|
2024-06-07 23:29:55 +04:00
|
|
|
|
2024-06-09 18:46:39 +04:00
|
|
|
trip_options = [TripOption(to=[FlightSegment(**segment) for segment in trip["to"]], back=[]) for trip in
|
2024-06-07 23:29:55 +04:00
|
|
|
resultTo]
|
|
|
|
|
2024-06-07 23:47:13 +04:00
|
|
|
return trip_options
|
2024-06-07 23:29:55 +04:00
|
|
|
|