should have working MVP now

This commit is contained in:
Anish Lakhwara 2022-11-21 13:10:02 +10:00
parent afdc7bd158
commit ab80adf70f
2 changed files with 27 additions and 20 deletions

44
main.py
View file

@ -1,15 +1,19 @@
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
from sqlalchemy import create_engine
from sqlalchemy import create_engine, text
import psycopg2.extras
from psycopg2.extensions import AsIs, register_adapter
from dataclasses import dataclass
from sqlalchemy.dialects.postgresql import JSONB
app = FastAPI()
engine = create_engine('postgresql://postgres?host=/home/anish/usr/df-web/postgres/')
engine = create_engine("postgresql://postgres?host=/home/anish/usr/df-web/postgres/")
conn = engine.connect()
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
class Vector3(BaseModel):
class vector3(BaseModel):
x: float
y: float
z: float
@ -18,33 +22,35 @@ class Vector3(BaseModel):
class Route(BaseModel):
id: str
creatorName: str
# dustPoints: List[Vector3]
checkpoints: List[Vector3]
startLocation: Vector3
# endLocation: Vector3
# creationDate: str
dustPoints: list # List[Vector3]
checkpoints: list # List[Vector3]
startLocation: dict # Vector3
endLocation: dict # Vector3
creationDate: str
@app.get("/route")
async def get_route():
select = "SELECT * FROM routes"
result = engine.execute(select)
data = []
for route in result:
print(route)
return {'message': "Woodley's world"}
data.append(route)
return {"data": data}
@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}))"
insert = text(
"""INSERT INTO
routes (id, creatorName, creationDate, startLocation, endLocation, checkpoints, dustPoints)
VALUES
(:id, :creatorName, creationDate
CAST(:startLocation as jsonb),
CAST(:endLocation as jsonb),
CAST(:checkpoints as jsonb[]),
CAST(:dustPoints as jsonb[]))"""
).bindparams(**as_dict)
engine.execute(insert)
return

View file

@ -9,11 +9,12 @@ if [ ! -d $PGDATA ]; then
initdb $PGDATA --auth=trust >/dev/null
fi
pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
# pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
# TODO move to alembic migrations
psql $PGDATABASE << EOF
CREATE TABLE routes ( \
id text,
creatorName text,
dustPoints jsonb[],
checkPoints jsonb[],