From dc407204d278cfb6905b4cd5ffc0f8f694cf13ab Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Fri, 24 May 2024 00:38:35 -0300 Subject: [PATCH] addresses review comments --- lib/controllers/environment.py | 6 ++++-- lib/controllers/flight.py | 12 ++++++++---- lib/controllers/motor.py | 6 ++++-- lib/controllers/rocket.py | 6 ++++-- lib/repositories/environment.py | 10 ++++------ lib/repositories/flight.py | 10 ++++------ lib/repositories/motor.py | 10 ++++------ lib/repositories/rocket.py | 11 +++++------ 8 files changed, 37 insertions(+), 34 deletions(-) diff --git a/lib/controllers/environment.py b/lib/controllers/environment.py index f857415..612e828 100644 --- a/lib/controllers/environment.py +++ b/lib/controllers/environment.py @@ -70,7 +70,8 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]: views.EnvCreated """ try: - with EnvRepository.fetch_env(self.env) as env_repo: + with EnvRepository() as env_repo: + env_repo.fetch_env(self.env) await env_repo.create_env() except Exception as e: exc_str = parse_error(e) @@ -179,7 +180,8 @@ async def update_env_by_id( HTTP 404 Not Found: If the env is not found in the database. """ try: - with EnvRepository.fetch_env(self.env) as env_repo: + with EnvRepository() as env_repo: + env_repo.fetch_env_by_id(env_id) await env_repo.create_env() await env_repo.delete_env_by_id(env_id) except Exception as e: diff --git a/lib/controllers/flight.py b/lib/controllers/flight.py index 8546cb3..3f6641f 100644 --- a/lib/controllers/flight.py +++ b/lib/controllers/flight.py @@ -107,7 +107,8 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]: views.FlightCreated """ try: - with FlightRepository.fetch_flight(self.flight) as flight_repo: + with FlightRepository() as flight_repo: + flight_repo.fetch_flight(self.flight) await flight_repo.create_flight( motor_kind=self.motor_kind, rocket_option=self.rocket_option, @@ -219,7 +220,8 @@ async def update_flight_by_id( HTTP 404 Not Found: If the flight is not found in the database. """ try: - with FlightRepository.fetch_flight(self.flight) as flight_repo: + with FlightRepository() as flight_repo: + flight_repo.fetch_flight(self.flight) await flight_repo.create_flight( motor_kind=self.motor_kind, rocket_option=self.rocket_option, @@ -261,7 +263,8 @@ async def update_env_by_flight_id( new_flight = read_flight.dict() new_flight["environment"] = env new_flight = Flight(**new_flight) - with FlightRepository.fetch_flight(new_flight) as flight_repo: + with FlightRepository() as flight_repo: + flight_repo.fetch_flight(new_flight) await flight_repo.create_flight( motor_kind=read_flight.rocket.motor.motor_kind, rocket_option=read_flight.rocket.rocket_option, @@ -308,7 +311,8 @@ async def update_rocket_by_flight_id( new_flight = read_flight.dict() new_flight["rocket"] = updated_rocket new_flight = Flight(**new_flight) - with FlightRepository.fetch_flight(new_flight) as flight_repo: + with FlightRepository() as flight_repo: + flight_repo.fetch_flight(new_flight) await flight_repo.create_flight( motor_kind=motor_kind, rocket_option=rocket_option ) diff --git a/lib/controllers/motor.py b/lib/controllers/motor.py index 96c0487..b5b46e0 100644 --- a/lib/controllers/motor.py +++ b/lib/controllers/motor.py @@ -118,7 +118,8 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]: views.MotorCreated """ try: - with MotorRepository.fetch_motor(self.motor) as motor_repo: + with MotorRepository() as motor_repo: + motor_repo.fetch_motor(self.motor) await motor_repo.create_motor(motor_kind=self.motor_kind) except Exception as e: exc_str = parse_error(e) @@ -227,7 +228,8 @@ async def update_motor_by_id( HTTP 404 Not Found: If the motor is not found in the database. """ try: - with MotorRepository.fetch_motor(self.motor) as motor_repo: + with MotorRepository() as motor_repo: + motor_repo.fetch_motor(self.motor) await motor_repo.create_motor(motor_kind=self.motor_kind) await motor_repo.delete_motor_by_id(motor_id) except Exception as e: diff --git a/lib/controllers/rocket.py b/lib/controllers/rocket.py index 8e9ccd5..b228b4f 100644 --- a/lib/controllers/rocket.py +++ b/lib/controllers/rocket.py @@ -155,7 +155,8 @@ async def create_rocket(self) -> Union[RocketCreated, HTTPException]: views.RocketCreated """ try: - with RocketRepository.fetch_rocket(self.rocket) as rocket_repo: + with RocketRepository() as rocket_repo: + rocket_repo.fetch_rocket(self.rocket) await rocket_repo.create_rocket( rocket_option=self.rocket_option, motor_kind=self.motor_kind, @@ -268,7 +269,8 @@ async def update_rocket_by_id( HTTP 404 Not Found: If the rocket is not found in the database. """ try: - with RocketRepository.fetch_rocket(self.rocket) as rocket_repo: + with RocketRepository() as rocket_repo: + rocket_repo.fetch_rocket(self.rocket) await rocket_repo.create_rocket( rocket_option=self.rocket_option, motor_kind=self.motor_kind, diff --git a/lib/repositories/environment.py b/lib/repositories/environment.py index 8983ec7..6c621fb 100644 --- a/lib/repositories/environment.py +++ b/lib/repositories/environment.py @@ -20,12 +20,10 @@ def __init__(self): super().__init__("environments") self._env = None - @classmethod - def fetch_env(cls, environment: Env): - instance = cls() - instance.env = environment - instance.env_id = environment.env_id - return instance + def fetch_env(self, environment: Env): + self.env = environment + self.env_id = environment.env_id + return self @property def env(self) -> Env: diff --git a/lib/repositories/flight.py b/lib/repositories/flight.py index e2d476d..003469b 100644 --- a/lib/repositories/flight.py +++ b/lib/repositories/flight.py @@ -20,12 +20,10 @@ def __init__(self): super().__init__("flights") self._flight = None - @classmethod - def fetch_flight(cls, flight: Flight): - instance = cls() - instance.flight = flight - instance.flight_id = flight.flight_id - return instance + def fetch_flight(self, flight: Flight): + self.flight = flight + self.flight_id = flight.flight_id + return self @property def flight(self) -> Flight: diff --git a/lib/repositories/motor.py b/lib/repositories/motor.py index b47bd7f..4251a46 100644 --- a/lib/repositories/motor.py +++ b/lib/repositories/motor.py @@ -20,12 +20,10 @@ def __init__(self): super().__init__("motors") self._motor = None - @classmethod - def fetch_motor(cls, motor: Motor): - instance = cls() - instance.motor = motor - instance.motor_id = motor.motor_id - return instance + def fetch_motor(self, motor: Motor): + self.motor = motor + self.motor_id = motor.motor_id + return self @property def motor(self) -> Motor: diff --git a/lib/repositories/rocket.py b/lib/repositories/rocket.py index b504eb4..d74d258 100644 --- a/lib/repositories/rocket.py +++ b/lib/repositories/rocket.py @@ -18,13 +18,12 @@ class RocketRepository(Repository): def __init__(self): super().__init__("rockets") + self._rocket = None - @classmethod - def fetch_rocket(cls, rocket: Rocket): - instance = cls() - instance.rocket = rocket - instance.rocket_id = rocket.rocket_id - return instance + def fetch_rocket(self, rocket: Rocket): + self.rocket = rocket + self.rocket_id = rocket.rocket_id + return self @property def rocket(self) -> Rocket: