Skip to content

Commit 7f50464

Browse files
addresses review comments
1 parent 803f0db commit 7f50464

File tree

11 files changed

+110
-44
lines changed

11 files changed

+110
-44
lines changed

lib/controllers/flight.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class FlightController:
5656
def __init__(
5757
self,
5858
flight: Flight,
59+
*,
5960
rocket_option: RocketOptions,
6061
motor_kind: MotorKinds,
6162
):
@@ -236,7 +237,7 @@ async def update_flight_by_id(
236237

237238
@classmethod
238239
async def update_env_by_flight_id(
239-
cls, flight_id: str, env: Env
240+
cls, flight_id: str, *, env: Env
240241
) -> Union[FlightUpdated, HTTPException]:
241242
"""
242243
Update a models.Flight.env in the database.
@@ -281,7 +282,7 @@ async def update_env_by_flight_id(
281282

282283
@classmethod
283284
async def update_rocket_by_flight_id(
284-
cls, flight_id: str, rocket: Rocket, rocket_option, motor_kind
285+
cls, flight_id: str, *, rocket: Rocket, rocket_option, motor_kind
285286
) -> Union[FlightUpdated, HTTPException]:
286287
"""
287288
Update a models.Flight.rocket in the database.

lib/controllers/motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MotorController:
3131
- Create a rocketpy.Motor object from a Motor model object.
3232
"""
3333

34-
def __init__(self, motor: Motor, motor_kind: MotorKinds):
34+
def __init__(self, *, motor: Motor, motor_kind: MotorKinds):
3535
self.guard(motor, motor_kind)
3636
self._motor = motor
3737
self._motor_kind = motor_kind

lib/controllers/rocket.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Union
2+
import os
23
import ast
34
import jsonpickle
45

@@ -49,7 +50,13 @@ class RocketController:
4950
- CRUD operations over models.Rocket on the database.
5051
"""
5152

52-
def __init__(self, rocket: Rocket, rocket_option, motor_kind):
53+
def __init__(
54+
self,
55+
*,
56+
rocket: Rocket,
57+
rocket_option: RocketOptions,
58+
motor_kind: MotorKinds,
59+
):
5360
self._rocket = rocket
5461
self._rocket_option = rocket_option
5562
self._motor_kind = motor_kind
@@ -83,8 +90,16 @@ def get_rocketpy_rocket(cls, rocket: Rocket) -> RocketPyRocket:
8390
radius=rocket.radius,
8491
mass=rocket.mass,
8592
inertia=rocket.inertia,
86-
power_off_drag=f"lib/data/{rocket.rocket_option.lower()}/powerOffDragCurve.csv",
87-
power_on_drag=f"lib/data/{rocket.rocket_option.lower()}/powerOnDragCurve.csv",
93+
power_off_drag=os.path.join(
94+
"lib/data",
95+
f"{rocket.rocket_option.lower()}",
96+
"powerOffDragCurve.csv",
97+
),
98+
power_on_drag=os.path.join(
99+
"lib/data",
100+
f"{rocket.rocket_option.lower()}",
101+
"powerOnDragCurve.csv",
102+
),
88103
center_of_mass_without_motor=rocket.center_of_mass_without_motor,
89104
coordinate_system_orientation=rocket.coordinate_system_orientation,
90105
)

lib/repositories/environment.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def create_env(self):
5252
try:
5353
environment_to_dict = self.env.dict()
5454
environment_to_dict["env_id"] = self.env_id
55-
await self.collection.insert_one(environment_to_dict)
55+
await self.insert_env(environment_to_dict)
5656
except Exception as e:
5757
exc_str = parse_error(e)
5858
logger.error(f"repositories.environment.create_env: {exc_str}")
@@ -72,7 +72,7 @@ async def get_env_by_id(self, env_id: str) -> Union[Env, None]:
7272
models.Env
7373
"""
7474
try:
75-
read_env = await self.collection.find_one({"env_id": env_id})
75+
read_env = await self.find_env(env_id)
7676
parsed_env = Env.parse_obj(read_env) if read_env else None
7777
except Exception as e:
7878
exc_str = parse_error(e)
@@ -93,7 +93,7 @@ async def delete_env_by_id(self, env_id: str):
9393
None
9494
"""
9595
try:
96-
await self.collection.delete_one({"env_id": env_id})
96+
await self.delete_env(env_id)
9797
except Exception as e:
9898
exc_str = parse_error(e)
9999
logger.error(f"repositories.environment.delete_env: {exc_str}")
@@ -102,3 +102,14 @@ async def delete_env_by_id(self, env_id: str):
102102
logger.info(
103103
f"Call to repositories.environment.delete_env completed for Env {env_id}"
104104
)
105+
106+
async def insert_env(self, env_data: dict):
107+
await self.collection.insert_one(env_data)
108+
return self
109+
110+
async def find_env(self, env_id: str):
111+
return await self.collection.find_one({"env_id": env_id})
112+
113+
async def delete_env(self, env_id: str):
114+
await self.collection.delete_one({"env_id": env_id})
115+
return self

lib/repositories/flight.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def create_flight(
6060
flight_to_dict["flight_id"] = self.flight_id
6161
flight_to_dict["rocket"]["rocket_option"] = rocket_option
6262
flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind
63-
await self.collection.insert_one(flight_to_dict)
63+
await self.insert_flight(flight_to_dict)
6464
except Exception as e:
6565
exc_str = parse_error(e)
6666
logger.error(f"repositories.flight.create_flight: {exc_str}")
@@ -80,9 +80,7 @@ async def get_flight_by_id(self, flight_id: str) -> Union[Flight, None]:
8080
models.Flight
8181
"""
8282
try:
83-
read_flight = await self.collection.find_one(
84-
{"flight_id": flight_id}
85-
)
83+
read_flight = await self.find_flight(flight_id)
8684
parsed_flight = (
8785
Flight.parse_obj(read_flight) if read_flight else None
8886
)
@@ -105,7 +103,7 @@ async def delete_flight_by_id(self, flight_id: str):
105103
None
106104
"""
107105
try:
108-
await self.collection.delete_one({"flight_id": flight_id})
106+
await self.delete_flight(flight_id)
109107
except Exception as e:
110108
exc_str = parse_error(e)
111109
logger.error(f"repositories.flight.delete_flight: {exc_str}")
@@ -114,3 +112,13 @@ async def delete_flight_by_id(self, flight_id: str):
114112
logger.info(
115113
f"Call to repositories.flight.delete_flight completed for Flight {flight_id}"
116114
)
115+
116+
async def insert_flight(self, flight_data: dict):
117+
await self.collection.insert_one(flight_data)
118+
119+
async def find_flight(self, flight_id: str):
120+
return await self.collection.find_one({"flight_id": flight_id})
121+
122+
async def delete_flight(self, flight_id: str):
123+
await self.collection.delete_one({"flight_id": flight_id})
124+
return self

lib/repositories/motor.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def create_motor(self, motor_kind: str = "SOLID"):
5656
motor_to_dict = self.motor.dict()
5757
motor_to_dict["motor_id"] = self.motor_id
5858
motor_to_dict["motor_kind"] = motor_kind
59-
await self.collection.insert_one(motor_to_dict)
59+
await self.insert_motor(motor_to_dict)
6060
except Exception as e:
6161
exc_str = parse_error(e)
6262
logger.error(f"repositories.motor.create_motor: {exc_str}")
@@ -76,7 +76,7 @@ async def get_motor_by_id(self, motor_id: str) -> Union[motor, None]:
7676
models.Motor
7777
"""
7878
try:
79-
read_motor = await self.collection.find_one({"motor_id": motor_id})
79+
read_motor = await self.find_motor(motor_id)
8080
parsed_motor = Motor.parse_obj(read_motor) if read_motor else None
8181
except Exception as e:
8282
exc_str = parse_error(e)
@@ -97,7 +97,7 @@ async def delete_motor_by_id(self, motor_id: str):
9797
None
9898
"""
9999
try:
100-
await self.collection.delete_one({"motor_id": motor_id})
100+
await self.delete_motor(motor_id)
101101
except Exception as e:
102102
exc_str = parse_error(e)
103103
logger.error(f"repositories.motor.delete_motor: {exc_str}")
@@ -106,3 +106,14 @@ async def delete_motor_by_id(self, motor_id: str):
106106
logger.info(
107107
f"Call to repositories.motor.delete_motor completed for Motor {motor_id}"
108108
)
109+
110+
async def insert_motor(self, motor_data: dict):
111+
await self.collection.insert_one(motor_data)
112+
return self
113+
114+
async def find_motor(self, motor_id: str):
115+
return await self.collection.find_one({"motor_id": motor_id})
116+
117+
async def delete_motor(self, motor_id: str):
118+
await self.collection.delete_one({"motor_id": motor_id})
119+
return self

lib/repositories/repo.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@ def __new__(cls):
2121

2222
def __init__(self, collection: str):
2323
if not self._initialized:
24-
self._connection_string = Secrets.get_secret(
25-
"MONGODB_CONNECTION_STRING"
26-
)
27-
self._client = AsyncIOMotorClient(
28-
self.connection_string,
29-
server_api=ServerApi("1"),
30-
maxIdleTimeMS=5000,
31-
connectTimeoutMS=5000,
32-
serverSelectionTimeoutMS=30000,
33-
)
34-
self._collection = self.client.rocketpy[collection]
35-
self._initialized = True # Mark as initialized
24+
try:
25+
self._connection_string = Secrets.get_secret(
26+
"MONGODB_CONNECTION_STRING"
27+
)
28+
self._client = AsyncIOMotorClient(
29+
self.connection_string,
30+
server_api=ServerApi("1"),
31+
maxIdleTimeMS=5000,
32+
connectTimeoutMS=5000,
33+
serverSelectionTimeoutMS=30000,
34+
)
35+
self._collection = self.client.rocketpy[collection]
36+
self._initialized = True # Mark as initialized
37+
except Exception as e:
38+
logging.error(f"Failed to initialize MongoDB client: {e}")
39+
raise ConnectionError(
40+
"Could not establish a connection with MongoDB."
41+
) from e
3642

3743
@property
3844
def connection_string(self):

lib/repositories/rocket.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def create_rocket(
6060
rocket_to_dict["rocket_id"] = self.rocket_id
6161
rocket_to_dict["rocket_option"] = rocket_option
6262
rocket_to_dict["motor"]["motor_kind"] = motor_kind
63-
await self.collection.insert_one(rocket_to_dict)
63+
await self.insert_rocket(rocket_to_dict)
6464
except Exception as e:
6565
exc_str = parse_error(e)
6666
logger.error(f"repositories.rocket.create_rocket: {exc_str}")
@@ -80,9 +80,7 @@ async def get_rocket_by_id(self, rocket_id: str) -> Union[Rocket, None]:
8080
models.Rocket
8181
"""
8282
try:
83-
read_rocket = await self.collection.find_one(
84-
{"rocket_id": rocket_id}
85-
)
83+
read_rocket = await self.find_rocket(rocket_id)
8684
parsed_rocket = (
8785
Rocket.parse_obj(read_rocket) if read_rocket else None
8886
)
@@ -105,7 +103,7 @@ async def delete_rocket_by_id(self, rocket_id: str):
105103
None
106104
"""
107105
try:
108-
await self.collection.delete_one({"rocket_id": rocket_id})
106+
await self.delete_rocket(rocket_id)
109107
except Exception as e:
110108
exc_str = parse_error(e)
111109
logger.error(f"repositories.rocket.delete_rocket: {exc_str}")
@@ -114,3 +112,14 @@ async def delete_rocket_by_id(self, rocket_id: str):
114112
logger.info(
115113
f"Call to repositories.rocket.delete_rocket completed for Rocket {rocket_id}"
116114
)
115+
116+
async def insert_rocket(self, rocket_data: dict):
117+
await self.collection.insert_one(rocket_data)
118+
return self
119+
120+
async def find_rocket(self, rocket_id: str):
121+
return await self.collection.find_one({"rocket_id": rocket_id})
122+
123+
async def delete_rocket(self, rocket_id: str):
124+
await self.collection.delete_one({"rocket_id": rocket_id})
125+
return self

lib/routes/flight.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def create_flight(
3939
``` Flight object as JSON ```
4040
"""
4141
return await FlightController(
42-
flight, rocket_option, motor_kind
42+
flight, rocket_option=rocket_option, motor_kind=motor_kind
4343
).create_flight()
4444

4545

@@ -76,7 +76,7 @@ async def update_flight_env(flight_id: str, env: Env) -> FlightUpdated:
7676
env: env object as JSON
7777
```
7878
"""
79-
return await FlightController.update_env_by_flight_id(flight_id, env)
79+
return await FlightController.update_env_by_flight_id(flight_id, env=env)
8080

8181

8282
@router.put("/{flight_id}/rocket")
@@ -96,7 +96,10 @@ async def update_flight_rocket(
9696
```
9797
"""
9898
return await FlightController.update_rocket_by_flight_id(
99-
flight_id, rocket, rocket_option, motor_kind
99+
flight_id,
100+
rocket=rocket,
101+
rocket_option=rocket_option,
102+
motor_kind=motor_kind,
100103
)
101104

102105

@@ -117,7 +120,7 @@ async def update_flight(
117120
```
118121
"""
119122
return await FlightController(
120-
flight, rocket_option, motor_kind
123+
flight, rocket_option=rocket_option, motor_kind=motor_kind
121124
).update_flight_by_id(flight_id)
122125

123126

lib/routes/motor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ async def create_motor(motor: Motor, motor_kind: MotorKinds) -> MotorCreated:
3333
## Args
3434
``` Motor object as a JSON ```
3535
"""
36-
return await MotorController(motor, motor_kind).create_motor()
36+
return await MotorController(
37+
motor=motor, motor_kind=motor_kind
38+
).create_motor()
3739

3840

3941
@router.get("/{motor_id}")
@@ -60,9 +62,9 @@ async def update_motor(
6062
motor: Motor object as JSON
6163
```
6264
"""
63-
return await MotorController(motor, motor_kind).update_motor_by_id(
64-
motor_id
65-
)
65+
return await MotorController(
66+
motor=motor, motor_kind=motor_kind
67+
).update_motor_by_id(motor_id)
6668

6769

6870
@router.delete("/{motor_id}")

lib/routes/rocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def create_rocket(
3737
``` Rocket object as a JSON ```
3838
"""
3939
return await RocketController(
40-
rocket, rocket_option, motor_kind
40+
rocket=rocket, rocket_option=rocket_option, motor_kind=motor_kind
4141
).create_rocket()
4242

4343

@@ -69,7 +69,7 @@ async def update_rocket(
6969
```
7070
"""
7171
return await RocketController(
72-
rocket, rocket_option, motor_kind
72+
rocket=rocket, rocket_option=rocket_option, motor_kind=motor_kind
7373
).update_rocket_by_id(rocket_id)
7474

7575

0 commit comments

Comments
 (0)