|
1 | 1 | from fastapi import FastAPI, HTTPException, Depends, APIRouter |
2 | 2 | from json import JSONDecodeError |
3 | 3 |
|
4 | | -from ..core.dependencies import get_restaurant_service |
| 4 | +from ..core.dependencies import get_restaurant_service, get_customer_service, get_current_customer |
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 |
| 11 | +from ..core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError, DuplicateEmailError, CustomerNotFoundError, CustomerDeactivatedError |
| 12 | +from ..schemas.customer import Customer, CustomerResponse |
| 13 | +from ..schemas.error import ErrorResponse |
| 14 | +from ..schemas.customer_request import CustomerCreate, CustomerUpdate |
| 15 | +from ..services.customer_service import CustomerService |
12 | 16 |
|
13 | 17 | def create_routes(api: FastAPI | APIRouter): |
14 | 18 | @api.get("/health") |
@@ -121,6 +125,76 @@ def update_restaurant(restaurant_id: str, request: RestaurantUpdate): |
121 | 125 | raise HTTPException(status_code=409, detail=str(e)) |
122 | 126 |
|
123 | 127 |
|
| 128 | + @api.post( |
| 129 | + "/customers", |
| 130 | + response_model=CustomerResponse, |
| 131 | + status_code=201, |
| 132 | + summary="Register a customer", |
| 133 | + responses={409: {"model": ErrorResponse, "description": "A customer with this email already exists"}}, |
| 134 | + ) |
| 135 | + def register_customer(request: CustomerCreate, customer_service: CustomerService = Depends(get_customer_service)): |
| 136 | + """Creates a new customer account. The password is stored hashed and never returned.""" |
| 137 | + try: |
| 138 | + return customer_service.register_customer(request) |
| 139 | + |
| 140 | + except DuplicateEmailError as e: |
| 141 | + raise HTTPException(status_code=409, detail=str(e)) |
| 142 | + |
| 143 | + |
| 144 | + @api.get( |
| 145 | + "/customers/me", |
| 146 | + response_model=CustomerResponse, |
| 147 | + summary="Get the signed-in customer's info", |
| 148 | + responses={401: {"model": ErrorResponse, "description": "Not signed in"}}, |
| 149 | + ) |
| 150 | + def get_my_customer_info(current_customer: Customer = Depends(get_current_customer)): |
| 151 | + """Returns the customer the request is signed in as. The password hash is never included.""" |
| 152 | + return current_customer |
| 153 | + |
| 154 | + |
| 155 | + @api.patch( |
| 156 | + "/customers/me", |
| 157 | + response_model=CustomerResponse, |
| 158 | + summary="Update the signed-in customer's info and delivery address", |
| 159 | + responses={ |
| 160 | + 401: {"model": ErrorResponse, "description": "Not signed in"}, |
| 161 | + 409: {"model": ErrorResponse, "description": "Another customer already uses this email"}, |
| 162 | + }, |
| 163 | + ) |
| 164 | + def update_my_customer_info( |
| 165 | + request: CustomerUpdate, |
| 166 | + current_customer: Customer = Depends(get_current_customer), |
| 167 | + customer_service: CustomerService = Depends(get_customer_service), |
| 168 | + ): |
| 169 | + """Changes only the fields that are sent. The password, role and id can't be changed here.""" |
| 170 | + try: |
| 171 | + return customer_service.update_customer(current_customer.id, request) |
| 172 | + |
| 173 | + except DuplicateEmailError as e: |
| 174 | + raise HTTPException(status_code=409, detail=str(e)) |
| 175 | + |
| 176 | + except (CustomerNotFoundError, CustomerDeactivatedError) as e: |
| 177 | + raise HTTPException(status_code=401, detail=str(e)) |
| 178 | + |
| 179 | + |
| 180 | + @api.delete( |
| 181 | + "/customers/me", |
| 182 | + status_code=204, |
| 183 | + summary="Deactivate the signed-in customer's account", |
| 184 | + responses={401: {"model": ErrorResponse, "description": "Not signed in"}}, |
| 185 | + ) |
| 186 | + def deactivate_my_account( |
| 187 | + current_customer: Customer = Depends(get_current_customer), |
| 188 | + customer_service: CustomerService = Depends(get_customer_service), |
| 189 | + ): |
| 190 | + """The account is kept (so past orders stay valid) but marked inactive, so it can't be used again.""" |
| 191 | + try: |
| 192 | + customer_service.deactivate_customer(current_customer.id) |
| 193 | + |
| 194 | + except (CustomerNotFoundError, CustomerDeactivatedError) as e: |
| 195 | + raise HTTPException(status_code=401, detail=str(e)) |
| 196 | + |
| 197 | + |
124 | 198 | @api.get("/", include_in_schema=False) |
125 | 199 | @api.get("/{path:path}", include_in_schema=False) |
126 | 200 | def api_not_found(path: str): |
|
0 commit comments