Skip to content

Commit

Permalink
general refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed May 22, 2024
1 parent ae9dbd2 commit 803f0db
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 303 deletions.
28 changes: 12 additions & 16 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def env(self, env: Env):
self._env = env

@staticmethod
async def get_rocketpy_env(env: Env) -> RocketPyEnvironment:
def get_rocketpy_env(env: Env) -> RocketPyEnvironment:
"""
Get the rocketpy env object.
Expand All @@ -70,9 +70,7 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]:
views.EnvCreated
"""
try:
created_env = await EnvRepository(
environment=self.env
).create_env()
await EnvRepository.fetch_env(self.env).create_env()
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.create_env: {exc_str}")
Expand All @@ -81,7 +79,7 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]:
detail=f"Failed to create environment: {exc_str}",
) from e
else:
return EnvCreated(env_id=created_env.env_id)
return EnvCreated(env_id=self.env.env_id)
finally:
logger.info(
f"Call to controllers.environment.create_env completed for Env {hash(self.env)}"
Expand Down Expand Up @@ -141,7 +139,7 @@ async def get_rocketpy_env_as_jsonpickle(
"""
try:
read_env = await cls.get_env_by_id(env_id)
rocketpy_env = await cls.get_rocketpy_env(read_env)
rocketpy_env = cls.get_rocketpy_env(read_env)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -162,7 +160,7 @@ async def get_rocketpy_env_as_jsonpickle(
f"Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed for Env {env_id}"
)

async def update_env(
async def update_env_by_id(
self, env_id: str
) -> Union[EnvUpdated, HTTPException]:
"""
Expand All @@ -178,12 +176,8 @@ async def update_env(
HTTP 404 Not Found: If the env is not found in the database.
"""
try:
await EnvController.get_env_by_id(env_id)
updated_env = await EnvRepository(
environment=self.env
).update_env_by_id(env_id)
except HTTPException as e:
raise e from e
env_repo = await EnvRepository.fetch_env(self.env).create_env()
await env_repo.delete_env_by_id(env_id)
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.update_env: {exc_str}")
Expand All @@ -192,14 +186,16 @@ async def update_env(
detail=f"Failed to update environment: {exc_str}",
) from e
else:
return EnvUpdated(new_env_id=updated_env.env_id)
return EnvUpdated(new_env_id=self.env.env_id)
finally:
logger.info(
f"Call to controllers.environment.update_env completed for Env {env_id}; Env {hash(self.env)}"
)

@staticmethod
async def delete_env(env_id: str) -> Union[EnvDeleted, HTTPException]:
async def delete_env_by_id(
env_id: str,
) -> Union[EnvDeleted, HTTPException]:
"""
Delete a models.Env from the database.
Expand Down Expand Up @@ -246,7 +242,7 @@ async def simulate_env(
"""
try:
read_env = await cls.get_env_by_id(env_id)
rocketpy_env = await cls.get_rocketpy_env(read_env)
rocketpy_env = cls.get_rocketpy_env(read_env)
env_simulation_numbers = EnvData.parse_obj(
rocketpy_env.all_info_returned()
)
Expand Down
81 changes: 40 additions & 41 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def flight(self, flight: Flight):
self._flight = flight

@staticmethod
async def get_rocketpy_flight(flight: Flight) -> RocketPyFlight:
def get_rocketpy_flight(flight: Flight) -> RocketPyFlight:
"""
Get the rocketpy flight object.
Expand All @@ -106,20 +106,18 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
views.FlightCreated
"""
try:
created_flight = await FlightRepository(
flight=self.flight
).create_flight(
await FlightRepository.fetch_flight(self.flight).create_flight(
motor_kind=self.motor_kind, rocket_option=self.rocket_option
)
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.create_flight: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create flight: {exc_str}",
detail=f"Failed to create flight: {exc_str}",
) from e
else:
return FlightCreated(flight_id=created_flight.flight_id)
return FlightCreated(flight_id=self.flight.flight_id)
finally:
logger.info(
f"Call to controllers.flight.create_flight completed for Flight {hash(self.flight)}"
Expand All @@ -146,15 +144,15 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
logger.error(f"controllers.flight.get_flight_by_id: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to read flight: {exc_str}",
detail=f"Failed to read flight: {exc_str}",
) from e
else:
if read_flight:
return read_flight
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Flight not found.",
) from e
)
finally:
logger.info(
f"Call to controllers.flight.get_flight_by_id completed for Flight {flight_id}"
Expand All @@ -179,7 +177,7 @@ async def get_rocketpy_flight_as_jsonpickle(
"""
try:
read_flight = await cls.get_flight_by_id(flight_id)
rocketpy_flight = await cls.get_rocketpy_flight(read_flight)
rocketpy_flight = cls.get_rocketpy_flight(read_flight)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -189,7 +187,7 @@ async def get_rocketpy_flight_as_jsonpickle(
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to read flight: {exc_str}",
detail=f"Failed to read flight: {exc_str}",
) from e
else:
return FlightPickle(
Expand All @@ -200,7 +198,7 @@ async def get_rocketpy_flight_as_jsonpickle(
f"Call to controllers.flight.get_rocketpy_flight_as_jsonpickle completed for Flight {flight_id}"
)

async def update_flight(
async def update_flight_by_id(
self, flight_id: str
) -> Union[FlightUpdated, HTTPException]:
"""
Expand All @@ -216,30 +214,28 @@ async def update_flight(
HTTP 404 Not Found: If the flight is not found in the database.
"""
try:
await FlightController.get_flight_by_id(flight_id)
updated_flight = await FlightRepository(
flight=self.flight,
motor_kind=self.motor_kind,
rocket_option=self.rocket_option,
).update_flight_by_id(flight_id=flight_id)
except HTTPException as e:
raise e from e
flight_repo = await FlightRepository.fetch_flight(
self.flight
).create_flight(
motor_kind=self.motor_kind, rocket_option=self.rocket_option
)
await flight_repo.delete_flight_by_id(flight_id)
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_flight: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update flight: {exc_str}",
detail=f"Failed to update flight: {exc_str}",
) from e
else:
return FlightUpdated(new_flight_id=updated_flight.flight_id)
return FlightUpdated(new_flight_id=self.flight.flight_id)
finally:
logger.info(
f"Call to controllers.flight.update_flight completed for Flight {flight_id}"
)

@classmethod
async def update_env(
async def update_env_by_flight_id(
cls, flight_id: str, env: Env
) -> Union[FlightUpdated, HTTPException]:
"""
Expand All @@ -260,28 +256,32 @@ async def update_env(
new_flight = read_flight.dict()
new_flight["environment"] = env
new_flight = Flight(**new_flight)
updated_flight = await FlightRepository(
flight=new_flight, flight_id=flight_id
).update_flight()
flight_repo = await FlightRepository.fetch_flight(
new_flight
).create_flight(
motor_kind=read_flight.rocket.motor.motor_kind,
rocket_option=read_flight.rocket.rocket_option,
)
await flight_repo.delete_flight_by_id(flight_id)
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_env: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update environment: {exc_str}",
detail=f"Failed to update environment: {exc_str}",
) from e
else:
return FlightUpdated(new_flight_id=updated_flight.flight_id)
return FlightUpdated(new_flight_id=new_flight.flight_id)
finally:
logger.info(
f"Call to controllers.flight.update_env completed for Flight {flight_id}; Env {hash(env)}"
)

@staticmethod
async def update_rocket(
flight_id: str, rocket: Rocket, rocket_option, motor_kind
@classmethod
async def update_rocket_by_flight_id(
cls, flight_id: str, rocket: Rocket, rocket_option, motor_kind
) -> Union[FlightUpdated, HTTPException]:
"""
Update a models.Flight.rocket in the database.
Expand All @@ -297,34 +297,35 @@ async def update_rocket(
HTTP 404 Not Found: If the flight is not found in the database.
"""
try:
read_flight = await FlightController.get_flight_by_id(flight_id)
read_flight = await cls.get_flight_by_id(flight_id)
updated_rocket = rocket.dict()
updated_rocket["rocket_option"] = rocket_option
updated_rocket["motor"]["motor_kind"] = motor_kind
new_flight = read_flight.dict()
new_flight["rocket"] = updated_rocket
new_flight = Flight(**new_flight)
new_flight_id = await FlightRepository(
flight=new_flight, flight_id=flight_id
).update_flight()
flight_repo = await FlightRepository.fetch_flight(
new_flight
).create_flight(motor_kind=motor_kind, rocket_option=rocket_option)
await flight_repo.delete_flight_by_id(flight_id)
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_rocket: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update rocket: {exc_str}",
detail=f"Failed to update rocket: {exc_str}",
) from e
else:
return FlightUpdated(new_flight_id=new_flight_id)
return FlightUpdated(new_flight_id=new_flight.flight_id)
finally:
logger.info(
f"Call to controllers.flight.update_rocket completed for Flight {flight_id}; Rocket {hash(rocket)}"
)

@staticmethod
async def delete_flight(
async def delete_flight_by_id(
flight_id: str,
) -> Union[FlightDeleted, HTTPException]:
"""
Expand All @@ -341,14 +342,12 @@ async def delete_flight(
"""
try:
await FlightRepository().delete_flight_by_id(flight_id)
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.delete_flight: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to delete flight: {exc_str}",
detail=f"Failed to delete flight: {exc_str}",
) from e
else:
return FlightDeleted(deleted_flight_id=flight_id)
Expand Down Expand Up @@ -635,7 +634,7 @@ async def simulate_flight(
logger.error(f"controllers.flight.simulate_flight: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to simulate flight: {exc_str}",
detail=f"Failed to simulate flight: {exc_str}",
) from e
else:
return flight_summary
Expand Down
22 changes: 10 additions & 12 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
views.MotorCreated
"""
try:
created_motor = MotorRepository(motor=self.motor).create_motor(
await MotorRepository.fetch_motor(self.motor).create_motor(
motor_kind=self.motor_kind
)
except Exception as e:
Expand All @@ -129,7 +129,7 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
detail=f"Failed to create motor: {exc_str}",
) from e
else:
return MotorCreated(motor_id=created_motor.motor_id)
return MotorCreated(motor_id=self.motor.motor_id)
finally:
logger.info(
f"Call to controllers.motor.create_motor completed for Motor {self.motor.motor_id}"
Expand Down Expand Up @@ -210,7 +210,7 @@ async def get_rocketpy_motor_as_jsonpickle(
f"Call to controllers.motor.get_rocketpy_motor_as_jsonpickle completed for Motor {motor_id}"
)

async def update_motor(
async def update_motor_by_id(
self, motor_id: str
) -> Union[MotorUpdated, HTTPException]:
"""
Expand All @@ -226,12 +226,10 @@ async def update_motor(
HTTP 404 Not Found: If the motor is not found in the database.
"""
try:
await MotorController.get_motor_by_id(motor_id)
updated_motor = await MotorRepository(
motor=self.motor
).update_motor_by_id(motor_id=motor_id, motor_kind=self.motor_kind)
except HTTPException as e:
raise e from e
motor_repo = await MotorRepository.fetch_motor(
self.motor
).create_motor(motor_kind=self.motor_kind)
await motor_repo.delete_motor_by_id(motor_id)
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.motor.update_motor: {exc_str}")
Expand All @@ -240,14 +238,14 @@ async def update_motor(
detail=f"Failed to update motor: {exc_str}",
) from e
else:
return MotorUpdated(new_motor_id=updated_motor.motor_id)
return MotorUpdated(new_motor_id=self.motor.motor_id)
finally:
logger.info(
f"Call to controllers.motor.update_motor completed for Motor {motor_id}"
)

@staticmethod
async def delete_motor(
async def delete_motor_by_id(
motor_id: str,
) -> Union[MotorDeleted, HTTPException]:
"""
Expand Down Expand Up @@ -296,7 +294,7 @@ async def simulate_motor(
"""
try:
read_motor = await MotorRepository().get_motor_by_id(motor_id)
motor = await cls.get_rocketpy_motor(read_motor)
motor = cls.get_rocketpy_motor(read_motor)

motor_simulation_numbers = MotorData(
total_burning_time="Total Burning Time: "
Expand Down
Loading

0 comments on commit 803f0db

Please sign in to comment.