Skip to content

Commit

Permalink
refactors repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed May 21, 2024
1 parent e516fd6 commit ff4859f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 87 deletions.
36 changes: 16 additions & 20 deletions lib/repositories/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -58,15 +55,15 @@ 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:
logger.info(
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
Expand All @@ -75,57 +72,56 @@ 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:
logger.info(
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
Returns:
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
Returns:
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}"
)
44 changes: 22 additions & 22 deletions lib/repositories/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -66,16 +63,20 @@ 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:
logger.info(
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
Expand All @@ -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
Expand All @@ -110,36 +110,36 @@ 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
)
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
Returns:
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}"
)
42 changes: 19 additions & 23 deletions lib/repositories/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -62,15 +59,17 @@ 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:
logger.info(
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
Expand All @@ -79,60 +78,57 @@ 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:
logger.info(
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
Returns:
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
Returns:
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}"
)
Loading

0 comments on commit ff4859f

Please sign in to comment.