Added endpoint to convert price to cash

This commit is contained in:
Yarne Coppens 2024-08-24 13:07:15 +02:00
parent 72bf8e5ce3
commit b8727deca1
3 changed files with 46 additions and 2 deletions

View file

@ -0,0 +1,12 @@
from pydantic import BaseModel
class AmountOfBills(BaseModel):
one_euro: int
two_euro: int
five_euro: int
ten_euro: int
twenty_euro: int
fifty_euro: int
hundred_euro: int
twohundred_euro: int
fivehundred_euro: int

View file

@ -3,8 +3,9 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
import os
from src.classes import product_classes
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
@ -41,4 +42,10 @@ def get_icon(icon_filename: str):
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
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

View file

@ -0,0 +1,25 @@
from src.classes import cash_classes
import math
def price_to_cash_model(price: int) -> cash_classes.AmountOfBills:
cash_types = [500,200,100,50,20,10,5,2,1]
cash_model = {}
for cash_type in cash_types:
cash_fits_in_price = math.floor(price / cash_type)
print(cash_type, price, cash_fits_in_price)
cash_model[str(cash_type)] = cash_fits_in_price
price -= cash_type * cash_fits_in_price
return cash_classes.AmountOfBills(
one_euro=cash_model['1'],
two_euro=cash_model['2'],
five_euro=cash_model['5'],
ten_euro=cash_model['10'],
twenty_euro=cash_model['20'],
fifty_euro=cash_model['50'],
hundred_euro=cash_model['100'],
twohundred_euro=cash_model['200'],
fivehundred_euro=cash_model['500']
)