51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
from sqlalchemy import create_engine
|
|
from dataclasses import dataclass
|
|
|
|
app = FastAPI()
|
|
engine = create_engine('postgresql://postgres?host=/home/anish/usr/df-web/postgres/')
|
|
conn = engine.connect()
|
|
|
|
|
|
class Vector3(BaseModel):
|
|
x: float
|
|
y: float
|
|
z: float
|
|
|
|
|
|
class Route(BaseModel):
|
|
id: str
|
|
creatorName: str
|
|
# dustPoints: List[Vector3]
|
|
checkpoints: List[Vector3]
|
|
startLocation: Vector3
|
|
# endLocation: Vector3
|
|
# creationDate: str
|
|
|
|
|
|
@app.get("/route")
|
|
async def get_route():
|
|
select = "SELECT * FROM routes"
|
|
result = engine.execute(select)
|
|
for route in result:
|
|
print(route)
|
|
return {'message': "Woodley's world"}
|
|
|
|
|
|
@app.post("/route")
|
|
async def post_route(route: Route):
|
|
as_dict = route.dict()
|
|
columns = ", ".join(list(as_dict.keys()))
|
|
# TODO this is fucked lol
|
|
# was trying to turn it into a string i could use to insert
|
|
# but that's not going so great because we have nested arrays
|
|
# but it basically means I need to add "ARRAY" before array values
|
|
# or use the ORM as god intended...
|
|
# Ugh, oop strikes again lol
|
|
rows = str(list(as_dict.values()))[1:-1]
|
|
insert = f"INSERT INTO routes ({columns}) VALUES ({rows}))"
|
|
engine.execute(insert)
|
|
return
|