51 lines
No EOL
1.5 KiB
Python
51 lines
No EOL
1.5 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
import os
|
|
|
|
from src.classes import product_classes, cash_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
|
|
|
|
app = FastAPI()
|
|
|
|
origins = [
|
|
"https://toddlershop.yarnecoppens.com" #Will become something like 'shop.yarnecoppens.com'
|
|
]
|
|
|
|
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 |