|
1 | 1 | from typing import Union
|
2 |
| -from pymongo.results import InsertOneResult |
3 |
| -from pymongo.results import DeleteResult |
| 2 | +from lib import logging, parse_error |
4 | 3 | from lib.models.flight import Flight
|
5 | 4 | from lib.repositories.repo import Repository
|
6 | 5 |
|
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
7 | 8 |
|
8 | 9 | class FlightRepository(Repository):
|
9 | 10 | """
|
10 |
| - Flight repository |
| 11 | + Enables database CRUD operations with models.Flight |
11 | 12 |
|
12 | 13 | Init Attributes:
|
13 |
| - flight: Flight object |
14 |
| - flight_id: Flight id |
| 14 | + flight: models.Flight |
| 15 | + flight_id: str |
15 | 16 |
|
16 |
| - Enables CRUD operations on flight objects |
17 | 17 | """
|
18 | 18 |
|
19 | 19 | def __init__(self, flight: Flight = None, flight_id: str = None):
|
20 | 20 | super().__init__("flights")
|
21 |
| - self.flight = flight |
| 21 | + self._flight = flight |
22 | 22 | if flight_id:
|
23 |
| - self.flight_id = flight_id |
| 23 | + self._flight_id = flight_id |
24 | 24 | else:
|
25 |
| - self.flight_id = self.flight.__hash__() |
| 25 | + self._flight_id = str(hash(self._flight)) |
26 | 26 |
|
27 | 27 | def __del__(self):
|
28 | 28 | self.connection.close()
|
29 | 29 | super().__del__()
|
30 | 30 |
|
| 31 | + @property |
| 32 | + def flight(self) -> Flight: |
| 33 | + return self._flight |
| 34 | + |
| 35 | + @flight.setter |
| 36 | + def flight(self, flight: "Flight"): |
| 37 | + self._flight = flight |
| 38 | + |
| 39 | + @property |
| 40 | + def flight_id(self) -> str: |
| 41 | + return self._flight_id |
| 42 | + |
| 43 | + @flight_id.setter |
| 44 | + def flight_id(self, flight_id: "str"): |
| 45 | + self._flight_id = flight_id |
| 46 | + |
31 | 47 | async def create_flight(
|
32 | 48 | self, motor_kind: str = "Solid", rocket_option: str = "Calisto"
|
33 |
| - ) -> "InsertOneResult": |
| 49 | + ): |
34 | 50 | """
|
35 |
| - Creates a flight in the database |
| 51 | + Creates a non-existing models.Flight in the database |
36 | 52 |
|
37 | 53 | Args:
|
38 |
| - rocketpy_flight: rocketpy flight object |
| 54 | + rocket_option: models.rocket.RocketOptions |
| 55 | + motor_kind: models.motor.MotorKinds |
39 | 56 |
|
40 | 57 | Returns:
|
41 |
| - InsertOneResult: result of the insert operation |
| 58 | + self |
42 | 59 | """
|
43 |
| - if not await self.get_flight(): |
44 |
| - try: |
45 |
| - flight_to_dict = self.flight.dict() |
46 |
| - flight_to_dict["flight_id"] = self.flight_id |
47 |
| - flight_to_dict["rocket"]["rocket_option"] = rocket_option |
48 |
| - flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind |
49 |
| - return await self.collection.insert_one(flight_to_dict) |
50 |
| - except Exception as e: |
51 |
| - raise Exception(f"Error creating flight: {str(e)}") from e |
52 |
| - finally: |
53 |
| - self.__del__() |
| 60 | + try: |
| 61 | + flight_to_dict = self.flight.dict() |
| 62 | + flight_to_dict["flight_id"] = self.flight_id |
| 63 | + flight_to_dict["rocket"]["rocket_option"] = rocket_option |
| 64 | + flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind |
| 65 | + await self.collection.insert_one(flight_to_dict) |
| 66 | + except Exception as e: |
| 67 | + exc_str = parse_error(e) |
| 68 | + logger.error(f"repositories.flight.create_flight: {exc_str}") |
| 69 | + raise Exception(f"Error creating flight: {str(e)}") from e |
54 | 70 | else:
|
55 |
| - return InsertOneResult(acknowledged=True, inserted_id=None) |
| 71 | + return self |
| 72 | + finally: |
| 73 | + logger.info( |
| 74 | + f"Call to repositories.flight.create_flight completed for Flight {self.flight_id}" |
| 75 | + ) |
56 | 76 |
|
57 | 77 | async def update_flight(
|
58 | 78 | self, motor_kind: str = "Solid", rocket_option: str = "Calisto"
|
59 |
| - ) -> "Union[str, None]": |
| 79 | + ): |
60 | 80 | """
|
61 |
| - Updates a flight in the database |
| 81 | + Updates a models.Flight in the database |
62 | 82 |
|
63 | 83 | Returns:
|
64 |
| - int: flight id |
| 84 | + self |
65 | 85 | """
|
66 | 86 | try:
|
67 | 87 | flight_to_dict = self.flight.dict()
|
68 |
| - flight_to_dict["flight_id"] = self.flight.__hash__() |
| 88 | + flight_to_dict["flight_id"] = str(hash(self.flight)) |
69 | 89 | flight_to_dict["rocket"]["rocket_option"] = rocket_option
|
70 | 90 | flight_to_dict["rocket"]["motor"]["motor_kind"] = motor_kind
|
71 |
| - |
72 | 91 | await self.collection.update_one(
|
73 | 92 | {"flight_id": self.flight_id}, {"$set": flight_to_dict}
|
74 | 93 | )
|
75 | 94 | self.flight_id = flight_to_dict["flight_id"]
|
76 | 95 | except Exception as e:
|
| 96 | + exc_str = parse_error(e) |
| 97 | + logger.error(f"repositories.flight.update_flight: {exc_str}") |
77 | 98 | raise Exception(f"Error updating flight: {str(e)}") from e
|
78 |
| - else: |
79 |
| - return str(self.flight_id) |
80 | 99 | finally:
|
81 |
| - self.__del__() |
| 100 | + logger.info( |
| 101 | + f"Call to repositories.flight.update_flight completed for Flight {self.flight_id}" |
| 102 | + ) |
82 | 103 |
|
83 |
| - async def get_flight(self) -> "Union[Flight, None]": |
| 104 | + async def get_flight(self) -> Union[Flight, None]: |
84 | 105 | """
|
85 |
| - Gets a flight from the database |
| 106 | + Gets a models.Flight from the database |
86 | 107 |
|
87 | 108 | Returns:
|
88 |
| - models.Flight: Model flight object |
| 109 | + models.Flight |
89 | 110 | """
|
90 | 111 | try:
|
91 |
| - flight = await self.collection.find_one( |
| 112 | + read_flight = await self.collection.find_one( |
92 | 113 | {"flight_id": self.flight_id}
|
93 | 114 | )
|
94 |
| - if flight is not None: |
95 |
| - return Flight.parse_obj(flight) |
96 |
| - return None |
| 115 | + parsed_flight = ( |
| 116 | + Flight.parse_obj(read_flight) if read_flight else None |
| 117 | + ) |
97 | 118 | except Exception as e:
|
| 119 | + exc_str = parse_error(e) |
| 120 | + logger.error(f"repositories.flight.get_flight: {exc_str}") |
98 | 121 | raise Exception(f"Error getting flight: {str(e)}") from e
|
| 122 | + else: |
| 123 | + return parsed_flight |
| 124 | + finally: |
| 125 | + logger.info( |
| 126 | + f"Call to repositories.flight.get_flight completed for Flight {self.flight_id}" |
| 127 | + ) |
99 | 128 |
|
100 |
| - async def delete_flight(self) -> "DeleteResult": |
| 129 | + async def delete_flight(self): |
101 | 130 | """
|
102 |
| - Deletes a flight from the database |
| 131 | + Deletes a models.Flight from the database |
103 | 132 |
|
104 | 133 | Returns:
|
105 |
| - DeleteResult: result of the delete operation |
| 134 | + None |
106 | 135 | """
|
107 | 136 | try:
|
108 |
| - return await self.collection.delete_one( |
109 |
| - {"flight_id": self.flight_id} |
110 |
| - ) |
| 137 | + await self.collection.delete_one({"flight_id": self.flight_id}) |
111 | 138 | except Exception as e:
|
| 139 | + exc_str = parse_error(e) |
| 140 | + logger.error(f"repositories.flight.delete_flight: {exc_str}") |
112 | 141 | raise Exception(f"Error deleting flight: {str(e)}") from e
|
113 | 142 | finally:
|
114 |
| - self.__del__() |
| 143 | + logger.info( |
| 144 | + f"Call to repositories.flight.delete_flight completed for Flight {self.flight_id}" |
| 145 | + ) |
0 commit comments