|
1 | | -from fastapi import FastAPI, HTTPException, Depends, APIRouter |
| 1 | +from fastapi import FastAPI, HTTPException, Depends, APIRouter, Path |
2 | 2 | from json import JSONDecodeError |
3 | 3 |
|
4 | | -from ..core.dependencies import get_restaurant_service, get_customer_service, get_current_customer |
| 4 | +from ..core.dependencies import get_restaurant_service, get_customer_service, get_current_customer, get_cart_service |
5 | 5 | from ..schemas.restaurant import Restaurant |
6 | 6 | from ..schemas.restaurant_query import RestaurantQuery |
7 | 7 | from ..services.restaurant_service import RestaurantService |
8 | 8 | from ..schemas.menu_item import MenuItem |
9 | 9 | from ..schemas.menu_item_request import MenuItemCreate, MenuItemUpdate |
10 | 10 | from ..schemas.restaurant_request import RestaurantCreate, RestaurantUpdate |
11 | | -from ..core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError, DuplicateEmailError, CustomerNotFoundError, CustomerDeactivatedError |
| 11 | +from ..core.exceptions import ( |
| 12 | + DuplicateRestaurantError, RestaurantNotFoundError, |
| 13 | + DuplicateEmailError, CustomerNotFoundError, CustomerDeactivatedError, |
| 14 | + MenuItemNotFoundError, MenuItemUnavailableError, DifferentRestaurantError, CartItemNotFoundError, CartQuantityError, |
| 15 | +) |
12 | 16 | from ..schemas.customer import Customer, CustomerResponse |
13 | 17 | from ..schemas.error import ErrorResponse |
14 | 18 | from ..schemas.customer_request import CustomerCreate, CustomerUpdate |
15 | 19 | from ..services.customer_service import CustomerService |
| 20 | +from ..schemas.cart import CartResponse |
| 21 | +from ..schemas.cart_request import CartItemAdd, CartItemUpdate |
| 22 | +from ..services.cart_service import CartService |
16 | 23 |
|
17 | 24 | def create_routes(api: FastAPI | APIRouter): |
18 | 25 | @api.get("/health") |
@@ -195,6 +202,127 @@ def deactivate_my_account( |
195 | 202 | raise HTTPException(status_code=401, detail=str(e)) |
196 | 203 |
|
197 | 204 |
|
| 205 | + @api.get( |
| 206 | + "/cart", |
| 207 | + response_model=CartResponse, |
| 208 | + summary="Get my cart", |
| 209 | + responses={401: {"model": ErrorResponse, "description": "Not signed in"}}, |
| 210 | + ) |
| 211 | + def get_cart( |
| 212 | + current_customer: Customer = Depends(get_current_customer), |
| 213 | + cart_service: CartService = Depends(get_cart_service), |
| 214 | + ): |
| 215 | + """Returns the signed-in customer's active cart (an empty one is created the first time), |
| 216 | + with current prices and the subtotal and total calculated by the backend.""" |
| 217 | + try: |
| 218 | + return cart_service.get_cart(current_customer) |
| 219 | + |
| 220 | + except CustomerDeactivatedError as e: |
| 221 | + raise HTTPException(status_code=401, detail=str(e)) |
| 222 | + |
| 223 | + |
| 224 | + @api.post( |
| 225 | + "/cart/items", |
| 226 | + response_model=CartResponse, |
| 227 | + summary="Add a menu item to my cart", |
| 228 | + responses={ |
| 229 | + 401: {"model": ErrorResponse, "description": "Not signed in"}, |
| 230 | + 404: {"model": ErrorResponse, "description": "No menu item has this id"}, |
| 231 | + 409: {"model": ErrorResponse, "description": "The item is out of stock, the cart has items from another restaurant, or it would go over 99"}, |
| 232 | + }, |
| 233 | + ) |
| 234 | + def add_cart_item( |
| 235 | + request: CartItemAdd, |
| 236 | + current_customer: Customer = Depends(get_current_customer), |
| 237 | + cart_service: CartService = Depends(get_cart_service), |
| 238 | + ): |
| 239 | + """Adds the item to the cart. If it's already there, its quantity goes up instead.""" |
| 240 | + try: |
| 241 | + return cart_service.add_item(current_customer, request) |
| 242 | + |
| 243 | + except CustomerDeactivatedError as e: |
| 244 | + raise HTTPException(status_code=401, detail=str(e)) |
| 245 | + |
| 246 | + except MenuItemNotFoundError as e: |
| 247 | + raise HTTPException(status_code=404, detail=str(e)) |
| 248 | + |
| 249 | + except (MenuItemUnavailableError, DifferentRestaurantError, CartQuantityError) as e: |
| 250 | + raise HTTPException(status_code=409, detail=str(e)) |
| 251 | + |
| 252 | + |
| 253 | + @api.patch( |
| 254 | + "/cart/items/{menu_item_id}", |
| 255 | + response_model=CartResponse, |
| 256 | + summary="Change the quantity of an item in my cart", |
| 257 | + responses={ |
| 258 | + 401: {"model": ErrorResponse, "description": "Not signed in"}, |
| 259 | + 404: {"model": ErrorResponse, "description": "The item is not in the cart, or is no longer on the menu"}, |
| 260 | + 409: {"model": ErrorResponse, "description": "The item is out of stock, so its quantity can only go down"}, |
| 261 | + }, |
| 262 | + ) |
| 263 | + def update_cart_item( |
| 264 | + request: CartItemUpdate, |
| 265 | + menu_item_id: str = Path(description="Id of the menu item in the cart"), |
| 266 | + current_customer: Customer = Depends(get_current_customer), |
| 267 | + cart_service: CartService = Depends(get_cart_service), |
| 268 | + ): |
| 269 | + """Sets the item's quantity (1-99). To take it out of the cart, use DELETE instead.""" |
| 270 | + try: |
| 271 | + return cart_service.update_item_quantity(current_customer, menu_item_id, request) |
| 272 | + |
| 273 | + except CustomerDeactivatedError as e: |
| 274 | + raise HTTPException(status_code=401, detail=str(e)) |
| 275 | + |
| 276 | + except (CartItemNotFoundError, MenuItemNotFoundError) as e: |
| 277 | + raise HTTPException(status_code=404, detail=str(e)) |
| 278 | + |
| 279 | + except MenuItemUnavailableError as e: |
| 280 | + raise HTTPException(status_code=409, detail=str(e)) |
| 281 | + |
| 282 | + |
| 283 | + @api.delete( |
| 284 | + "/cart/items/{menu_item_id}", |
| 285 | + response_model=CartResponse, |
| 286 | + summary="Remove an item from my cart", |
| 287 | + responses={ |
| 288 | + 401: {"model": ErrorResponse, "description": "Not signed in"}, |
| 289 | + 404: {"model": ErrorResponse, "description": "The item is not in the cart"}, |
| 290 | + }, |
| 291 | + ) |
| 292 | + def remove_cart_item( |
| 293 | + menu_item_id: str = Path(description="Id of the menu item in the cart"), |
| 294 | + current_customer: Customer = Depends(get_current_customer), |
| 295 | + cart_service: CartService = Depends(get_cart_service), |
| 296 | + ): |
| 297 | + """Removes the item and returns the updated cart. Works for items that are out of stock or no longer on the menu.""" |
| 298 | + try: |
| 299 | + return cart_service.remove_item(current_customer, menu_item_id) |
| 300 | + |
| 301 | + except CustomerDeactivatedError as e: |
| 302 | + raise HTTPException(status_code=401, detail=str(e)) |
| 303 | + |
| 304 | + except CartItemNotFoundError as e: |
| 305 | + raise HTTPException(status_code=404, detail=str(e)) |
| 306 | + |
| 307 | + |
| 308 | + @api.delete( |
| 309 | + "/cart", |
| 310 | + response_model=CartResponse, |
| 311 | + summary="Clear my cart", |
| 312 | + responses={401: {"model": ErrorResponse, "description": "Not signed in"}}, |
| 313 | + ) |
| 314 | + def clear_cart( |
| 315 | + current_customer: Customer = Depends(get_current_customer), |
| 316 | + cart_service: CartService = Depends(get_cart_service), |
| 317 | + ): |
| 318 | + """Removes every item. The cart stays, empty, and can be used for any restaurant again.""" |
| 319 | + try: |
| 320 | + return cart_service.clear_cart(current_customer) |
| 321 | + |
| 322 | + except CustomerDeactivatedError as e: |
| 323 | + raise HTTPException(status_code=401, detail=str(e)) |
| 324 | + |
| 325 | + |
198 | 326 | @api.get("/", include_in_schema=False) |
199 | 327 | @api.get("/{path:path}", include_in_schema=False) |
200 | 328 | def api_not_found(path: str): |
|
0 commit comments