diff --git a/lib/repositories/environment.py b/lib/repositories/environment.py index 59daf2c..6e81eca 100644 --- a/lib/repositories/environment.py +++ b/lib/repositories/environment.py @@ -16,13 +16,10 @@ class EnvRepository(Repository): """ - def __init__(self, environment: Env = None, env_id: str = None): + def __init__(self, environment: Env | None = None): super().__init__("environments") - self._env = environment - if env_id: - self._env_id = env_id - else: - self._env_id = str(hash(self._env)) + self._env = environment if environment else None + self._env_id = environment.env_id if environment else None def __del__(self): self.connection.close() @@ -58,7 +55,7 @@ async def create_env(self): except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.environment.create_env: {exc_str}") - raise Exception(f"Error creating environment: {str(e)}") from e + raise Exception(f"Error creating environment: {exc_str}") from e else: return self finally: @@ -66,7 +63,7 @@ async def create_env(self): f"Call to repositories.environment.create_env completed for Env {self.env_id}" ) - async def update_env(self): + async def update_env_by_id(self, env_id: str): """ Updates a models.Env in the database @@ -75,15 +72,14 @@ async def update_env(self): """ try: environment_to_dict = self.env.dict() - environment_to_dict["env_id"] = str(hash(self.env)) + environment_to_dict["env_id"] = self.env_id await self.collection.update_one( - {"env_id": self.env_id}, {"$set": environment_to_dict} + {"env_id": env_id}, {"$set": environment_to_dict} ) - self.env_id = environment_to_dict["env_id"] except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.environment.update_env: {exc_str}") - raise Exception(f"Error updating environment: {str(e)}") from e + raise Exception(f"Error updating environment: {exc_str}") from e else: return self finally: @@ -91,7 +87,7 @@ async def update_env(self): f"Call to repositories.environment.update_env completed for Env {self.env_id}" ) - async def get_env(self) -> Union[Env, None]: + async def get_env_by_id(self, env_id: str) -> Union[Env, None]: """ Gets a models.Env from the database @@ -99,20 +95,20 @@ async def get_env(self) -> Union[Env, None]: models.Env """ try: - read_env = await self.collection.find_one({"env_id": self.env_id}) + read_env = await self.collection.find_one({"env_id": env_id}) parsed_env = Env.parse_obj(read_env) if read_env else None except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.environment.get_env: {exc_str}") - raise Exception(f"Error getting environment: {str(e)}") from e + raise Exception(f"Error getting environment: {exc_str}") from e else: return parsed_env finally: logger.info( - f"Call to repositories.environment.get_env completed for Env {self.env_id}" + f"Call to repositories.environment.get_env completed for Env {env_id}" ) - async def delete_env(self): + async def delete_env_by_id(self, env_id: str): """ Deletes a models.Env from the database @@ -120,12 +116,12 @@ async def delete_env(self): None """ try: - await self.collection.delete_one({"env_id": self.env_id}) + await self.collection.delete_one({"env_id": env_id}) except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.environment.delete_env: {exc_str}") - raise Exception(f"Error deleting environment: {str(e)}") from e + raise Exception(f"Error deleting environment: {exc_str}") from e finally: logger.info( - f"Call to repositories.environment.delete_env completed for Env {self.env_id}" + f"Call to repositories.environment.delete_env completed for Env {env_id}" ) diff --git a/lib/repositories/flight.py b/lib/repositories/flight.py index 9dfcf87..e2c6dbc 100644 --- a/lib/repositories/flight.py +++ b/lib/repositories/flight.py @@ -16,13 +16,10 @@ class FlightRepository(Repository): """ - def __init__(self, flight: Flight = None, flight_id: str = None): + def __init__(self, flight: Flight | None = None): super().__init__("flights") - self._flight = flight - if flight_id: - self._flight_id = flight_id - else: - self._flight_id = str(hash(self._flight)) + self._flight = flight if flight else None + self._flight_id = flight.flight_id if flight else None def __del__(self): self.connection.close() @@ -45,7 +42,7 @@ def flight_id(self, flight_id: "str"): self._flight_id = flight_id async def create_flight( - self, motor_kind: str = "Solid", rocket_option: str = "Calisto" + self, motor_kind: str = "SOLID", rocket_option: str = "CALISTO" ): """ Creates a non-existing models.Flight in the database @@ -66,7 +63,7 @@ async def create_flight( except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.flight.create_flight: {exc_str}") - raise Exception(f"Error creating flight: {str(e)}") from e + raise Exception(f"Error creating flight: {exc_str}") from e else: return self finally: @@ -74,8 +71,12 @@ async def create_flight( f"Call to repositories.flight.create_flight completed for Flight {self.flight_id}" ) - async def update_flight( - self, motor_kind: str = "Solid", rocket_option: str = "Calisto" + async def update_flight_by_id( + self, + *, + flight_id: str, + motor_kind: str = "SOLID", + rocket_option: str = "CALISTO", ): """ Updates a models.Flight in the database @@ -85,23 +86,22 @@ async def update_flight( """ try: flight_to_dict = self.flight.dict() - flight_to_dict["flight_id"] = str(hash(self.flight)) + flight_to_dict["flight_id"] = self.flight_id flight_to_dict["rocket"]["rocket_option"] = rocket_option flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind await self.collection.update_one( - {"flight_id": self.flight_id}, {"$set": flight_to_dict} + {"flight_id": flight_id}, {"$set": flight_to_dict} ) - self.flight_id = flight_to_dict["flight_id"] except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.flight.update_flight: {exc_str}") - raise Exception(f"Error updating flight: {str(e)}") from e + raise Exception(f"Error updating flight: {exc_str}") from e finally: logger.info( f"Call to repositories.flight.update_flight completed for Flight {self.flight_id}" ) - async def get_flight(self) -> Union[Flight, None]: + async def get_flight_by_id(self, flight_id: str) -> Union[Flight, None]: """ Gets a models.Flight from the database @@ -110,7 +110,7 @@ async def get_flight(self) -> Union[Flight, None]: """ try: read_flight = await self.collection.find_one( - {"flight_id": self.flight_id} + {"flight_id": flight_id} ) parsed_flight = ( Flight.parse_obj(read_flight) if read_flight else None @@ -118,15 +118,15 @@ async def get_flight(self) -> Union[Flight, None]: except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.flight.get_flight: {exc_str}") - raise Exception(f"Error getting flight: {str(e)}") from e + raise Exception(f"Error getting flight: {exc_str}") from e else: return parsed_flight finally: logger.info( - f"Call to repositories.flight.get_flight completed for Flight {self.flight_id}" + f"Call to repositories.flight.get_flight completed for Flight {flight_id}" ) - async def delete_flight(self): + async def delete_flight_by_id(self, flight_id: str): """ Deletes a models.Flight from the database @@ -134,12 +134,12 @@ async def delete_flight(self): None """ try: - await self.collection.delete_one({"flight_id": self.flight_id}) + await self.collection.delete_one({"flight_id": flight_id}) except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.flight.delete_flight: {exc_str}") - raise Exception(f"Error deleting flight: {str(e)}") from e + raise Exception(f"Error deleting flight: {exc_str}") from e finally: logger.info( - f"Call to repositories.flight.delete_flight completed for Flight {self.flight_id}" + f"Call to repositories.flight.delete_flight completed for Flight {flight_id}" ) diff --git a/lib/repositories/motor.py b/lib/repositories/motor.py index 17032ad..006e2c6 100644 --- a/lib/repositories/motor.py +++ b/lib/repositories/motor.py @@ -16,13 +16,10 @@ class MotorRepository(Repository): """ - def __init__(self, motor: Motor = None, motor_id: str = None) -> None: + def __init__(self, motor: Motor | None = None): super().__init__("motors") - self._motor = motor - if motor_id: - self._motor_id = motor_id - else: - self._motor_id = str(hash(self._motor)) + self._motor = motor if motor else None + self._motor_id = motor.motor_id if motor else None def __del__(self): self.connection.close() @@ -44,7 +41,7 @@ def motor_id(self) -> str: def motor_id(self, motor_id: "str"): self._motor_id = motor_id - async def create_motor(self, motor_kind: str = "solid"): + async def create_motor(self, motor_kind: str = "SOLID"): """ Creates a non-existing models.Motor in the database @@ -62,7 +59,7 @@ async def create_motor(self, motor_kind: str = "solid"): except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.motor.create_motor: {exc_str}") - raise Exception(f"Error creating motor: {str(e)}") from e + raise Exception(f"Error creating motor: {exc_str}") from e else: return self finally: @@ -70,7 +67,9 @@ async def create_motor(self, motor_kind: str = "solid"): f"Call to repositories.motor.create_motor completed for Motor {self.motor_id}" ) - async def update_motor(self, motor_kind: str = "solid"): + async def update_motor_by_id( + self, *, motor_id: str, motor_kind: str = "SOLID" + ): """ Updates a models.Motor in the database @@ -79,16 +78,15 @@ async def update_motor(self, motor_kind: str = "solid"): """ try: motor_to_dict = self.motor.dict() - motor_to_dict["motor_id"] = str(hash(self.motor)) + motor_to_dict["motor_id"] = self.motor_id motor_to_dict["motor_kind"] = motor_kind await self.collection.update_one( - {"motor_id": self.motor_id}, {"$set": motor_to_dict} + {"motor_id": motor_id}, {"$set": motor_to_dict} ) - self.motor_id = motor_to_dict["motor_id"] except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.motor.update_motor: {exc_str}") - raise Exception(f"Error updating motor: {str(e)}") from e + raise Exception(f"Error updating motor: {exc_str}") from e else: return self finally: @@ -96,7 +94,7 @@ async def update_motor(self, motor_kind: str = "solid"): f"Call to repositories.motor.update_motor completed for Motor {self.motor_id}" ) - async def get_motor(self) -> Union[motor, None]: + async def get_motor_by_id(self, motor_id: str) -> Union[motor, None]: """ Gets a models.Motor from the database @@ -104,22 +102,20 @@ async def get_motor(self) -> Union[motor, None]: models.Motor """ try: - read_motor = await self.collection.find_one( - {"motor_id": self.motor_id} - ) + read_motor = await self.collection.find_one({"motor_id": motor_id}) parsed_motor = Motor.parse_obj(read_motor) if read_motor else None except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.motor.get_motor: {exc_str}") - raise Exception(f"Error getting motor: {str(e)}") from e + raise Exception(f"Error getting motor: {exc_str}") from e else: return parsed_motor finally: logger.info( - f"Call to repositories.motor.get_motor completed for Motor {self.motor_id}" + f"Call to repositories.motor.get_motor completed for Motor {motor_id}" ) - async def delete_motor(self): + async def delete_motor_by_id(self, motor_id: str): """ Deletes a models.Motor from the database @@ -127,12 +123,12 @@ async def delete_motor(self): None """ try: - await self.collection.delete_one({"motor_id": self.motor_id}) + await self.collection.delete_one({"motor_id": motor_id}) except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.motor.delete_motor: {exc_str}") - raise Exception(f"Error deleting motor: {str(e)}") from e + raise Exception(f"Error deleting motor: {exc_str}") from e finally: logger.info( - f"Call to repositories.motor.delete_motor completed for Motor {self.motor_id}" + f"Call to repositories.motor.delete_motor completed for Motor {motor_id}" ) diff --git a/lib/repositories/rocket.py b/lib/repositories/rocket.py index 84e8538..632b193 100644 --- a/lib/repositories/rocket.py +++ b/lib/repositories/rocket.py @@ -16,13 +16,10 @@ class RocketRepository(Repository): """ - def __init__(self, rocket: Rocket = None, rocket_id: str = None): + def __init__(self, rocket: Rocket | None = None): super().__init__("rockets") - self._rocket = rocket - if rocket_id: - self._rocket_id = rocket_id - else: - self._rocket_id = str(hash(self._rocket)) + self._rocket = rocket if rocket else None + self._rocket_id = rocket.rocket_id if rocket else None def __del__(self): self.connection.close() @@ -45,7 +42,7 @@ def rocket_id(self, rocket_id: "str"): self._rocket_id = rocket_id async def create_rocket( - self, rocket_option: str = "Calisto", motor_kind: str = "Solid" + self, rocket_option: str = "CALISTO", motor_kind: str = "SOLID" ): """ Creates a non-existing models.Rocket in the database @@ -66,7 +63,7 @@ async def create_rocket( except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.rocket.create_rocket: {exc_str}") - raise Exception(f"Error creating rocket: {str(e)}") from e + raise Exception(f"Error creating rocket: {exc_str}") from e else: return self finally: @@ -74,7 +71,13 @@ async def create_rocket( f"Call to repositories.rocket.create_rocket completed for Rocket {self.rocket_id}" ) - async def update_rocket(self, rocket_option: str = "Calisto", motor_kind: str = "Solid"): + async def update_rocket_by_id( + self, + *, + rocket_id: str, + rocket_option: str = "CALISTO", + motor_kind: str = "SOLID", + ): """ Updates a models.Rocket in the database @@ -83,18 +86,16 @@ async def update_rocket(self, rocket_option: str = "Calisto", motor_kind: str = """ try: rocket_to_dict = self.rocket.dict() - rocket_to_dict["rocket_id"] = str(hash(self.rocket)) + rocket_to_dict["rocket_id"] = self.rocket_id rocket_to_dict["rocket_option"] = rocket_option rocket_to_dict["motor"]["motor_kind"] = motor_kind await self.collection.update_one( - {"rocket_id": self.rocket_id}, {"$set": rocket_to_dict} + {"rocket_id": rocket_id}, {"$set": rocket_to_dict} ) - - self.rocket_id = rocket_to_dict["rocket_id"] except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.rocket.update_rocket: {exc_str}") - raise Exception(f"Error updating rocket: {str(e)}") from e + raise Exception(f"Error updating rocket: {exc_str}") from e else: return self finally: @@ -102,7 +103,7 @@ async def update_rocket(self, rocket_option: str = "Calisto", motor_kind: str = f"Call to repositories.rocket.update_rocket completed for Rocket {self.rocket_id}" ) - async def get_rocket(self) -> Union[Rocket, None]: + async def get_rocket_by_id(self, rocket_id: str) -> Union[Rocket, None]: """ Gets a models.Rocket from the database @@ -111,7 +112,7 @@ async def get_rocket(self) -> Union[Rocket, None]: """ try: read_rocket = await self.collection.find_one( - {"rocket_id": self.rocket_id} + {"rocket_id": rocket_id} ) parsed_rocket = ( Rocket.parse_obj(read_rocket) if read_rocket else None @@ -119,15 +120,15 @@ async def get_rocket(self) -> Union[Rocket, None]: except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.rocket.get_rocket: {exc_str}") - raise Exception(f"Error getting rocket: {str(e)}") from e + raise Exception(f"Error getting rocket: {exc_str}") from e else: return parsed_rocket finally: logger.info( - f"Call to repositories.rocket.get_rocket completed for Rocket {self.rocket_id}" + f"Call to repositories.rocket.get_rocket completed for Rocket {rocket_id}" ) - async def delete_rocket(self): + async def delete_rocket_by_id(self, rocket_id: str): """ Deletes a models.Rocket from the database @@ -135,12 +136,12 @@ async def delete_rocket(self): None """ try: - await self.collection.delete_one({"rocket_id": self.rocket_id}) + await self.collection.delete_one({"rocket_id": rocket_id}) except Exception as e: exc_str = parse_error(e) logger.error(f"repositories.rocket.delete_rocket: {exc_str}") - raise Exception(f"Error deleting rocket: {str(e)}") from e + raise Exception(f"Error deleting rocket: {exc_str}") from e finally: logger.info( - f"Call to repositories.rocket.delete_rocket completed for Rocket {self.rocket_id}" + f"Call to repositories.rocket.delete_rocket completed for Rocket {rocket_id}" )