11
11
from market .models import User , Product , ShoppingCart , UserRole
12
12
13
13
14
+ class NotFoundException (Exception ):
15
+ pass
16
+
17
+
14
18
class DB :
15
19
KEY_USERS : str = "users"
16
20
KEY_PRODUCTS : str = "products"
17
21
KEY_SHOPPING_CARTS : str = "shopping_carts"
18
22
19
23
db_name : str = str (DB_FILE_NAME )
20
24
25
+ def _generate_id (self ) -> str :
26
+ return str (uuid4 ())
27
+
21
28
def _do_init_db_objects (self ):
22
29
with self .get_shelve () as db :
23
- print (dict (db ))
24
-
25
30
if self .KEY_USERS not in db :
26
31
db [self .KEY_USERS ] = dict ()
27
32
@@ -31,18 +36,18 @@ def _do_init_db_objects(self):
31
36
if self .KEY_SHOPPING_CARTS not in db :
32
37
db [self .KEY_SHOPPING_CARTS ] = dict ()
33
38
34
- if not db [self .KEY_USERS ]:
35
- admin = User (
36
- # Это uuid4 – уникальный идентификатор пользователя
37
- id = "29ae7ebf-4445-42f2-9548-a3a54f095220" ,
38
- role = UserRole .ADMIN ,
39
- username = "admin" ,
40
- password = "Admin_4321!" ,
41
- )
42
- db [self .KEY_USERS ][admin .id ] = admin
43
-
39
+ has_key_users = bool (db [self .KEY_USERS ])
44
40
has_key_products = bool (db [self .KEY_PRODUCTS ])
45
41
42
+ # В create_user и так открывается get_shelve
43
+ if not has_key_users :
44
+ self .create_user (
45
+ role = UserRole .ADMIN ,
46
+ username = "admin" ,
47
+ password = "Admin_4321!" ,
48
+ id = "29ae7ebf-4445-42f2-9548-a3a54f095220" ,
49
+ )
50
+
46
51
# В create_product и так открывается get_shelve
47
52
if not has_key_products :
48
53
self .create_product (
@@ -96,28 +101,59 @@ def get_users(
96
101
97
102
return filtered_users
98
103
99
- def create_user (self , user : User ):
104
+ def create_user (
105
+ self ,
106
+ role : UserRole ,
107
+ username : str ,
108
+ password : str ,
109
+ id : str | None = None ,
110
+ ) -> User :
100
111
with self .get_shelve () as db :
101
- db [self .KEY_USERS ][user .id ] = user
112
+ obj = User (
113
+ id = id if id else self ._generate_id (),
114
+ role = role ,
115
+ username = username ,
116
+ password = password ,
117
+ )
118
+ db [self .KEY_USERS ][obj .id ] = obj
119
+ return obj
102
120
103
- def create_product (self , name : str , price_minor : int , description : str ):
121
+ def create_product (self , name : str , price_minor : int , description : str ) -> Product :
104
122
with self .get_shelve () as db :
105
123
obj = Product (
106
- id = str ( uuid4 () ),
124
+ id = self . _generate_id ( ),
107
125
name = name ,
108
126
price_minor = price_minor ,
109
127
description = description ,
110
128
)
111
-
112
129
db [self .KEY_PRODUCTS ][obj .id ] = obj
130
+ return obj
113
131
114
132
def get_products (self ) -> list [Product ]:
115
133
with self .get_shelve () as db :
116
134
return list (db [self .KEY_PRODUCTS ].values ())
117
135
118
- def create_shopping_cart (self , shopping_cart : ShoppingCart ):
136
+ def create_shopping_cart (self , product_ids : list [str ]) -> ShoppingCart :
137
+ with self .get_shelve () as db :
138
+ obj = ShoppingCart (
139
+ id = self ._generate_id (),
140
+ product_ids = product_ids ,
141
+ )
142
+ db [self .KEY_SHOPPING_CARTS ][obj .id ] = obj
143
+ return obj
144
+
145
+ def update_shopping_cart (
146
+ self ,
147
+ shopping_cart_id : str ,
148
+ product_ids : list [str ],
149
+ ):
150
+ # TODO: дублирует
119
151
with self .get_shelve () as db :
120
- db [self .KEY_SHOPPING_CARTS ][shopping_cart .id ] = shopping_cart
152
+ shopping_cart : ShoppingCart | None = self .get_shopping_cart (shopping_cart_id )
153
+ if not shopping_cart :
154
+ raise NotFoundException (f"Shopping cart #{ shopping_cart_id } not found!" )
155
+
156
+ shopping_cart .product_ids = product_ids
121
157
122
158
def get_shopping_carts (self ) -> list [ShoppingCart ]:
123
159
with self .get_shelve () as db :
@@ -127,21 +163,39 @@ def get_shopping_cart(self, id: str) -> ShoppingCart | None:
127
163
with self .get_shelve () as db :
128
164
return db [self .KEY_SHOPPING_CARTS ].get (id )
129
165
130
- # TODO: Для добавления продукта нужен только id
131
166
def add_product_in_shopping_cart (
132
- self , product : Product , shopping_cart : ShoppingCart
167
+ self ,
168
+ shopping_cart_id : str ,
169
+ product_id : str ,
133
170
):
134
- shopping_cart .products .append (product )
135
- # TODO: ?
136
- self .create_shopping_cart (shopping_cart )
171
+ # TODO: дублирует
172
+ with self .get_shelve () as db :
173
+ if product_id not in db [self .KEY_PRODUCTS ]:
174
+ raise NotFoundException (f"Product #{ product_id } not found!" )
175
+
176
+ shopping_cart : ShoppingCart | None = self .get_shopping_cart (shopping_cart_id )
177
+ if not shopping_cart :
178
+ raise NotFoundException (f"Shopping cart #{ shopping_cart_id } not found!" )
179
+
180
+ shopping_cart .product_ids .append (product_id )
181
+ self .update_shopping_cart (shopping_cart_id , shopping_cart .product_ids )
137
182
138
- # TODO: Для удаления продукта нужен только id
139
183
def remove_product_from_shopping_cart (
140
- self , product : Product , shopping_cart : ShoppingCart
184
+ self ,
185
+ shopping_cart_id : str ,
186
+ product_id : str ,
141
187
):
142
- shopping_cart .products .remove (product )
143
- # TODO: ?
144
- self .create_shopping_cart (shopping_cart )
188
+ # TODO: дублирует
189
+ with self .get_shelve () as db :
190
+ if product_id not in db [self .KEY_PRODUCTS ]:
191
+ raise NotFoundException (f"Product #{ product_id } not found!" )
192
+
193
+ shopping_cart : ShoppingCart | None = self .get_shopping_cart (shopping_cart_id )
194
+ if not shopping_cart :
195
+ raise NotFoundException (f"Shopping cart #{ shopping_cart_id } not found!" )
196
+
197
+ shopping_cart .product_ids .remove (product_id )
198
+ self .update_shopping_cart (shopping_cart_id , shopping_cart .product_ids )
145
199
146
200
147
201
db = DB ()
0 commit comments