59 lines
No EOL
1.7 KiB
Python
59 lines
No EOL
1.7 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
import os
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
from src.classes import product_classes, cash_classes, user_classes
|
|
from src.config import definitions
|
|
from src.modules import price_to_cash_calculator
|
|
from shop_validators import image_validator
|
|
from src import data_connection
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup
|
|
data_connection.create_db_and_tables()
|
|
yield
|
|
# Shutdown
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
origins = [
|
|
"*" #TODO change this
|
|
]
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {"Hello": "World"}
|
|
|
|
@app.get("/products", response_model=list[product_classes.Product])
|
|
def get_all_products():
|
|
return data_connection.get_all_products()
|
|
|
|
@app.get("/products/{barcode}", response_model=product_classes.Product)
|
|
def get_single_product(barcode: int):
|
|
return data_connection.get_single_product(barcode)
|
|
|
|
@app.get("/icons/{icon_filename}", response_class=FileResponse)
|
|
def get_icon(icon_filename: str):
|
|
full_filepath = os.path.join(definitions.ICONS_PATH, icon_filename) + ".svg"
|
|
assert os.path.exists(full_filepath), f"File {full_filepath} not found"
|
|
assert image_validator.is_valid_svg(file_name=full_filepath), f"File {full_filepath} is not a valid image"
|
|
|
|
return full_filepath
|
|
|
|
@app.get("/price_to_cash/{price}", response_model=cash_classes.AmountOfBills)
|
|
def price_to_cash(price: int):
|
|
cash_model = price_to_cash_calculator.price_to_cash_model(price)
|
|
|
|
return cash_model |