working with caelen

This commit is contained in:
Anish Lakhwara 2022-11-21 21:06:49 +10:00
parent ab80adf70f
commit 08633f1288
2 changed files with 20 additions and 14 deletions

30
main.py
View file

@ -1,5 +1,6 @@
from fastapi import FastAPI from fastapi import FastAPI
from pydantic import BaseModel from pydantic import BaseModel
from http import HTTPStatus
from typing import List from typing import List
from sqlalchemy import create_engine, text from sqlalchemy import create_engine, text
import psycopg2.extras import psycopg2.extras
@ -20,13 +21,14 @@ class vector3(BaseModel):
class Route(BaseModel): class Route(BaseModel):
id: str route: dict
creatorName: str # id: str
dustPoints: list # List[Vector3] # creatorName: str
checkpoints: list # List[Vector3] # dustPoints: list # List[Vector3]
startLocation: dict # Vector3 # checkpoints: list # List[Vector3]
endLocation: dict # Vector3 # startLocation: dict # Vector3
creationDate: str # endLocation: dict # Vector3
# creationDate: str
@app.get("/route") @app.get("/route")
@ -35,22 +37,24 @@ async def get_route():
result = engine.execute(select) result = engine.execute(select)
data = [] data = []
for route in result: for route in result:
data.append(route) data.append({"route": route})
return {"data": data} return data
@app.post("/route") @app.post("/route")
async def post_route(route: Route): async def post_route(route: Route):
as_dict = route.dict() print(route)
as_dict = route.dict()['route']
as_dict['id'] = "astoehsteaohus"
insert = text( insert = text(
"""INSERT INTO """INSERT INTO
routes (id, creatorName, creationDate, startLocation, endLocation, checkpoints, dustPoints) routes (id, creatorName, creationDate, name, creatorCharacter, startLocation, endLocation, checkpoints, dustPoints)
VALUES VALUES
(:id, :creatorName, creationDate (:id, :creatorName, :creationDate, :name, :creatorCharacter,
CAST(:startLocation as jsonb), CAST(:startLocation as jsonb),
CAST(:endLocation as jsonb), CAST(:endLocation as jsonb),
CAST(:checkpoints as jsonb[]), CAST(:checkpoints as jsonb[]),
CAST(:dustPoints as jsonb[]))""" CAST(:dustPoints as jsonb[]))"""
).bindparams(**as_dict) ).bindparams(**as_dict)
engine.execute(insert) engine.execute(insert)
return return HTTPStatus.OK

View file

@ -15,10 +15,12 @@ fi
psql $PGDATABASE << EOF psql $PGDATABASE << EOF
CREATE TABLE routes ( \ CREATE TABLE routes ( \
id text, id text,
name text,
creatorName text, creatorName text,
creatorCharacter int,
dustPoints jsonb[], dustPoints jsonb[],
checkPoints jsonb[], checkPoints jsonb[],
startLocation jsonb, startLocation jsonb,
endLocation jsonb, endLocation jsonb,
createDate text creationDate text
); );