1+ from fastapi import HTTPException , status
2+
13from src .controllers .interface import (
24 ControllerBase ,
35 controller_exception_handler ,
46)
5- from src .views .flight import FlightSimulation
6- from src .models .flight import FlightModel
7+ from src .views .flight import FlightSimulation , FlightCreated
8+ from src .models .flight import (
9+ FlightModel ,
10+ FlightWithReferencesRequest ,
11+ )
712from src .models .environment import EnvironmentModel
813from src .models .rocket import RocketModel
14+ from src .repositories .interface import RepositoryInterface
915from src .services .flight import FlightService
1016
1117
@@ -21,6 +27,56 @@ class FlightController(ControllerBase):
2127 def __init__ (self ):
2228 super ().__init__ (models = [FlightModel ])
2329
30+ async def _load_environment (self , environment_id : str ) -> EnvironmentModel :
31+ repo_cls = RepositoryInterface .get_model_repo (EnvironmentModel )
32+ async with repo_cls () as repo :
33+ environment = await repo .read_environment_by_id (environment_id )
34+ if environment is None :
35+ raise HTTPException (
36+ status_code = status .HTTP_404_NOT_FOUND ,
37+ detail = "Environment not found" ,
38+ )
39+ return environment
40+
41+ async def _load_rocket (self , rocket_id : str ) -> RocketModel :
42+ repo_cls = RepositoryInterface .get_model_repo (RocketModel )
43+ async with repo_cls () as repo :
44+ rocket = await repo .read_rocket_by_id (rocket_id )
45+ if rocket is None :
46+ raise HTTPException (
47+ status_code = status .HTTP_404_NOT_FOUND ,
48+ detail = "Rocket not found" ,
49+ )
50+ return rocket
51+
52+ @controller_exception_handler
53+ async def create_flight_from_references (
54+ self , payload : FlightWithReferencesRequest
55+ ) -> FlightCreated :
56+ environment = await self ._load_environment (payload .environment_id )
57+ rocket = await self ._load_rocket (payload .rocket_id )
58+ flight_model = payload .flight .assemble (
59+ environment = environment ,
60+ rocket = rocket ,
61+ )
62+ return await self .post_flight (flight_model )
63+
64+ @controller_exception_handler
65+ async def update_flight_from_references (
66+ self ,
67+ flight_id : str ,
68+ payload : FlightWithReferencesRequest ,
69+ ) -> None :
70+ environment = await self ._load_environment (payload .environment_id )
71+ rocket = await self ._load_rocket (payload .rocket_id )
72+ flight_model = payload .flight .assemble (
73+ environment = environment ,
74+ rocket = rocket ,
75+ )
76+ flight_model .set_id (flight_id )
77+ await self .put_flight_by_id (flight_id , flight_model )
78+ return
79+
2480 @controller_exception_handler
2581 async def update_environment_by_flight_id (
2682 self , flight_id : str , * , environment : EnvironmentModel
0 commit comments