mash-server/src/endpoints/bettwaage.py

32 lines
748 B
Python
Raw Normal View History

2024-05-02 17:31:38 +02:00
from fastapi import APIRouter
from datetime import datetime
from os.path import exists
from fastapi.responses import HTMLResponse
router = APIRouter()
file_path = "bettwaage.csv"
def add_line_to_history(line: str) -> None:
with open(file_path, "+a", encoding="UTF-8") as fp:
fp.writelines([line])
@router.get("/file")
async def get_file():
with open(file_path, "r", encoding="UTF-8") as fp:
return HTMLResponse("\n".join(fp.readlines()))
@router.post("/add")
async def add_weight(tl: int, tr: int, bl: int, br: int):
sum = tl + tr + bl + br
add_line_to_history(f"{str(datetime.now())};{";".join([tl, tr, bl, br, sum])};")
if not exists(file_path):
add_line_to_history("timestamp;tl;tr;bl;br;total;")