-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook database queries to the products page
- Loading branch information
Showing
9 changed files
with
226 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,77 @@ | ||
from fastapi import APIRouter, Request | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Form, HTTPException, Request, Response, status | ||
from fastapi.responses import HTMLResponse | ||
|
||
from .. import templates | ||
from ..store import ProductTable | ||
from ..store import Product, ProductTable, delete_product | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/products", response_class=HTMLResponse) | ||
async def get_products(request: Request): | ||
products = await ProductTable.select_all() | ||
return HTMLResponse(templates.products(request, products)) | ||
return HTMLResponse(templates.products.page(request, products)) | ||
|
||
|
||
@router.post("/products", response_class=Response) | ||
async def new_product( | ||
product_id: Annotated[int, Form()], | ||
name: Annotated[str, Form(max_length=40)], | ||
filename: Annotated[str, Form(max_length=100)], | ||
price: Annotated[int, Form()], | ||
no_stock: Annotated[int | None, Form()] = None, | ||
): | ||
new_product = Product( | ||
product_id=product_id, | ||
name=name, | ||
filename=filename, | ||
price=price, | ||
no_stock=no_stock, | ||
) | ||
await ProductTable.insert(new_product) | ||
return Response(headers={"hx-refresh": "true"}) | ||
|
||
|
||
@router.post("/products/{product_id}", response_class=Response) | ||
async def update_product( | ||
product_id: int, | ||
new_product_id: Annotated[int, Form()], | ||
name: Annotated[str, Form(max_length=40)], | ||
filename: Annotated[str, Form(max_length=100)], | ||
price: Annotated[int, Form()], | ||
no_stock: Annotated[int | None, Form()] = None, | ||
): | ||
new_product = Product( | ||
product_id=new_product_id, | ||
name=name, | ||
filename=filename, | ||
price=price, | ||
no_stock=no_stock, | ||
) | ||
|
||
# TODO: Test whether the returning SQL clause in update function is working or not | ||
await ProductTable.update(product_id, new_product) | ||
return Response(headers={"hx-refresh": "true"}) | ||
|
||
# @router.post("/products/{product_id}", response_class=HTMLResponse) | ||
# async def update_product(request: Request, product_id: int): | ||
# product = await ProductTable.by_product_id(product_id) | ||
# return HTMLResponse(templates.components.product(request, product)) | ||
|
||
@router.delete("/products/{product_id}", response_class=Response) | ||
async def delete(product_id: int): | ||
await delete_product(product_id) | ||
return Response(headers={"hx-refresh": "true"}) | ||
|
||
|
||
@router.get("/products/{product_id}/editor", response_class=HTMLResponse) | ||
async def get_product_editor(request: Request, product_id: int): | ||
product = await ProductTable.by_product_id(product_id) | ||
return HTMLResponse(templates.components.product_editor(request, product)) | ||
maybe_product = await ProductTable.by_product_id(product_id) | ||
|
||
if (product := maybe_product) is None: | ||
detail = f"Product {product_id} not found" | ||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=detail) | ||
return HTMLResponse(templates.products.editor(request, product)) | ||
|
||
|
||
@router.get("/product-editor", response_class=HTMLResponse) | ||
async def get_empty_product_editor(request: Request): | ||
return HTMLResponse(templates.products.empty_editor(request)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.