2024-08-04 08:43:04 +02:00
|
|
|
from fastapi import FastAPI
|
2024-08-04 10:06:07 +02:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-08-04 17:23:56 +02:00
|
|
|
from fastapi.responses import FileResponse
|
2024-08-05 13:37:03 +02:00
|
|
|
import os
|
2024-08-04 10:06:07 +02:00
|
|
|
|
2024-08-24 13:07:15 +02:00
|
|
|
from src.classes import product_classes, cash_classes
|
2024-08-04 17:23:56 +02:00
|
|
|
from src.config import definitions
|
2024-08-24 13:07:15 +02:00
|
|
|
from src.modules import price_to_cash_calculator
|
2024-08-05 13:48:47 +02:00
|
|
|
from shop_validators import image_validator
|
2024-08-04 09:01:17 +02:00
|
|
|
from src import data_connection
|
2024-08-04 08:43:04 +02:00
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
2024-08-04 10:06:07 +02:00
|
|
|
origins = [
|
2024-09-04 11:45:11 +02:00
|
|
|
"https://toddlershop.yarnecoppens.com" #Will become something like 'shop.yarnecoppens.com'
|
2024-08-04 10:06:07 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
2024-08-04 10:13:59 +02:00
|
|
|
allow_credentials=True,
|
2024-08-04 10:06:07 +02:00
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-05 13:37:03 +02:00
|
|
|
|
2024-08-04 08:43:04 +02:00
|
|
|
@app.get("/")
|
|
|
|
|
def read_root():
|
|
|
|
|
return {"Hello": "World"}
|
|
|
|
|
|
2024-08-04 09:01:17 +02:00
|
|
|
@app.get("/products", response_model=list[product_classes.Product])
|
2024-08-04 08:47:49 +02:00
|
|
|
def get_all_products():
|
2024-08-04 09:01:17 +02:00
|
|
|
return data_connection.get_all_products()
|
2024-08-04 08:47:49 +02:00
|
|
|
|
2024-08-04 10:23:58 +02:00
|
|
|
@app.get("/products/{barcode}", response_model=product_classes.Product)
|
|
|
|
|
def get_single_product(barcode: int):
|
2024-08-04 17:23:56 +02:00
|
|
|
return data_connection.get_single_product(barcode)
|
|
|
|
|
|
|
|
|
|
@app.get("/icons/{icon_filename}", response_class=FileResponse)
|
|
|
|
|
def get_icon(icon_filename: str):
|
2024-08-05 13:37:03 +02:00
|
|
|
full_filepath = os.path.join(definitions.ICONS_PATH, icon_filename) + ".svg"
|
|
|
|
|
assert os.path.exists(full_filepath), f"File {full_filepath} not found"
|
2024-08-05 13:48:47 +02:00
|
|
|
assert image_validator.is_valid_svg(file_name=full_filepath), f"File {full_filepath} is not a valid image"
|
2024-08-05 13:37:03 +02:00
|
|
|
|
2024-08-24 13:07:15 +02:00
|
|
|
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
|