Skip to content

Commit

Permalink
refactor repositories accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed Jul 12, 2024
1 parent b43cf4a commit ec445e2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 137 deletions.
37 changes: 14 additions & 23 deletions lib/repositories/environment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Union
from pymongo.errors import PyMongoError
from lib import logger, parse_error
from lib.models.environment import Env
from lib.repositories.repo import Repository
from lib import logger
from lib.repositories.repo import Repository, RepositoryNotInitializedException


class EnvRepository(Repository):
Expand Down Expand Up @@ -42,14 +42,17 @@ def env_id(self, env_id: "str"):
self._env_id = env_id

async def insert_env(self, env_data: dict):
await self.collection.insert_one(env_data)
collection = self.get_collection()
await collection.insert_one(env_data)
return self

async def find_env(self, env_id: str):
return await self.collection.find_one({"env_id": env_id})
collection = self.get_collection()
return await collection.find_one({"env_id": env_id})

async def delete_env(self, env_id: str):
await self.collection.delete_one({"env_id": env_id})
collection = self.get_collection()
await collection.delete_one({"env_id": env_id})
return self

async def create_env(self):
Expand All @@ -64,13 +67,9 @@ async def create_env(self):
environment_to_dict["env_id"] = self.env_id
await self.insert_env(environment_to_dict)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.create_env: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.create_env: {exc_str}")
raise Exception(f"Error creating environment: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -90,13 +89,9 @@ async def get_env_by_id(self, env_id: str) -> Union[Env, None]:
parsed_env = Env.parse_obj(read_env) if read_env else None
self.env = parsed_env
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.get_env: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.get_env: {exc_str}")
raise Exception(f"Error getting environment: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -114,13 +109,9 @@ async def delete_env_by_id(self, env_id: str):
try:
await self.delete_env(env_id)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.delete_env: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.environment.delete_env: {exc_str}")
raise Exception(f"Error deleting environment: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand Down
37 changes: 14 additions & 23 deletions lib/repositories/flight.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Union
from pymongo.errors import PyMongoError
from lib import logger, parse_error
from lib import logger
from lib.models.flight import Flight
from lib.repositories.repo import Repository
from lib.repositories.repo import Repository, RepositoryNotInitializedException


class FlightRepository(Repository):
Expand Down Expand Up @@ -42,13 +42,16 @@ def flight_id(self, flight_id: "str"):
self._flight_id = flight_id

async def insert_flight(self, flight_data: dict):
await self.collection.insert_one(flight_data)
collection = self.get_collection()
await collection.insert_one(flight_data)

async def find_flight(self, flight_id: str):
return await self.collection.find_one({"flight_id": flight_id})
collection = self.get_collection()
return await collection.find_one({"flight_id": flight_id})

async def delete_flight(self, flight_id: str):
await self.collection.delete_one({"flight_id": flight_id})
collection = self.get_collection()
await collection.delete_one({"flight_id": flight_id})
return self

async def create_flight(
Expand All @@ -71,13 +74,9 @@ async def create_flight(
flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind
await self.insert_flight(flight_to_dict)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.create_flight: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.create_flight: {exc_str}")
raise Exception(f"Error creating flight: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -99,13 +98,9 @@ async def get_flight_by_id(self, flight_id: str) -> Union[Flight, None]:
)
self.flight = parsed_flight
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.get_flight: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.get_flight: {exc_str}")
raise Exception(f"Error getting flight: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -123,13 +118,9 @@ async def delete_flight_by_id(self, flight_id: str):
try:
await self.delete_flight(flight_id)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.delete_flight: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.flight.delete_flight: {exc_str}")
raise Exception(f"Error deleting flight: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand Down
37 changes: 14 additions & 23 deletions lib/repositories/motor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Union
from pymongo.errors import PyMongoError
from lib import logger, parse_error
from lib import logger
from lib.models.motor import Motor
from lib.repositories.repo import Repository
from lib.repositories.repo import Repository, RepositoryNotInitializedException


class MotorRepository(Repository):
Expand Down Expand Up @@ -42,14 +42,17 @@ def motor_id(self, motor_id: "str"):
self._motor_id = motor_id

async def insert_motor(self, motor_data: dict):
await self.collection.insert_one(motor_data)
collection = self.get_collection()
await collection.insert_one(motor_data)
return self

async def find_motor(self, motor_id: str):
return await self.collection.find_one({"motor_id": motor_id})
collection = self.get_collection()
return await collection.find_one({"motor_id": motor_id})

async def delete_motor(self, motor_id: str):
await self.collection.delete_one({"motor_id": motor_id})
collection = self.get_collection()
await collection.delete_one({"motor_id": motor_id})
return self

async def create_motor(self, motor_kind: str = "SOLID"):
Expand All @@ -68,13 +71,9 @@ async def create_motor(self, motor_kind: str = "SOLID"):
motor_to_dict["motor_kind"] = motor_kind
await self.insert_motor(motor_to_dict)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.create_motor: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.create_motor: {exc_str}")
raise Exception(f"Error creating motor: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -94,13 +93,9 @@ async def get_motor_by_id(self, motor_id: str) -> Union[motor, None]:
parsed_motor = Motor.parse_obj(read_motor) if read_motor else None
self.motor = parsed_motor
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.get_motor: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.get_motor: {exc_str}")
raise Exception(f"Error getting motor: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand All @@ -118,13 +113,9 @@ async def delete_motor_by_id(self, motor_id: str):
try:
await self.delete_motor(motor_id)
except PyMongoError as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.delete_motor: {exc_str}")
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"repositories.motor.delete_motor: {exc_str}")
raise Exception(f"Error deleting motor: {exc_str}") from e
except RepositoryNotInitializedException as e:
raise e from e
else:
return self
finally:
Expand Down
Loading

0 comments on commit ec445e2

Please sign in to comment.