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 fastapi import FastAPI
from pydantic import BaseModel from pydantic import BaseModel
from typing import List 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 dataclasses import dataclass
from sqlalchemy.dialects.postgresql import JSONB
app = FastAPI() 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() conn = engine.connect()
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
class Vector3(BaseModel): class vector3(BaseModel):
x: float x: float
y: float y: float
z: float z: float
@ -18,33 +22,35 @@ class Vector3(BaseModel):
class Route(BaseModel): class Route(BaseModel):
id: str id: str
creatorName: str creatorName: str
# dustPoints: List[Vector3] dustPoints: list # List[Vector3]
checkpoints: List[Vector3] checkpoints: list # List[Vector3]
startLocation: Vector3 startLocation: dict # Vector3
# endLocation: Vector3 endLocation: dict # Vector3
# creationDate: str creationDate: str
@app.get("/route") @app.get("/route")
async def get_route(): async def get_route():
select = "SELECT * FROM routes" select = "SELECT * FROM routes"
result = engine.execute(select) result = engine.execute(select)
data = []
for route in result: for route in result:
print(route) data.append(route)
return {'message': "Woodley's world"} return {"data": data}
@app.post("/route") @app.post("/route")
async def post_route(route: Route): async def post_route(route: Route):
as_dict = route.dict() as_dict = route.dict()
columns = ", ".join(list(as_dict.keys())) insert = text(
# TODO this is fucked lol """INSERT INTO
# was trying to turn it into a string i could use to insert routes (id, creatorName, creationDate, startLocation, endLocation, checkpoints, dustPoints)
# but that's not going so great because we have nested arrays VALUES
# but it basically means I need to add "ARRAY" before array values (:id, :creatorName, creationDate
# or use the ORM as god intended... CAST(:startLocation as jsonb),
# Ugh, oop strikes again lol CAST(:endLocation as jsonb),
rows = str(list(as_dict.values()))[1:-1] CAST(:checkpoints as jsonb[]),
insert = f"INSERT INTO routes ({columns}) VALUES ({rows}))" CAST(:dustPoints as jsonb[]))"""
).bindparams(**as_dict)
engine.execute(insert) engine.execute(insert)
return return

View file

@ -9,11 +9,12 @@ if [ ! -d $PGDATA ]; then
initdb $PGDATA --auth=trust >/dev/null initdb $PGDATA --auth=trust >/dev/null
fi 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 # TODO move to alembic migrations
psql $PGDATABASE << EOF psql $PGDATABASE << EOF
CREATE TABLE routes ( \ CREATE TABLE routes ( \
id text,
creatorName text, creatorName text,
dustPoints jsonb[], dustPoints jsonb[],
checkPoints jsonb[], checkPoints jsonb[],