From 08633f128884a22ad04653fda8bcc0bc212c8273 Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Mon, 21 Nov 2022 21:06:49 +1000 Subject: [PATCH] working with caelen --- main.py | 30 +++++++++++++++++------------- setup-postgres.sh | 4 +++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 7783952..dfa7563 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ from fastapi import FastAPI from pydantic import BaseModel +from http import HTTPStatus from typing import List from sqlalchemy import create_engine, text import psycopg2.extras @@ -20,13 +21,14 @@ class vector3(BaseModel): class Route(BaseModel): - id: str - creatorName: str - dustPoints: list # List[Vector3] - checkpoints: list # List[Vector3] - startLocation: dict # Vector3 - endLocation: dict # Vector3 - creationDate: str + route: dict + # id: str + # creatorName: str + # dustPoints: list # List[Vector3] + # checkpoints: list # List[Vector3] + # startLocation: dict # Vector3 + # endLocation: dict # Vector3 + # creationDate: str @app.get("/route") @@ -35,22 +37,24 @@ async def get_route(): result = engine.execute(select) data = [] for route in result: - data.append(route) - return {"data": data} + data.append({"route": route}) + return data @app.post("/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 INTO - routes (id, creatorName, creationDate, startLocation, endLocation, checkpoints, dustPoints) + routes (id, creatorName, creationDate, name, creatorCharacter, startLocation, endLocation, checkpoints, dustPoints) VALUES - (:id, :creatorName, creationDate + (:id, :creatorName, :creationDate, :name, :creatorCharacter, CAST(:startLocation as jsonb), CAST(:endLocation as jsonb), CAST(:checkpoints as jsonb[]), CAST(:dustPoints as jsonb[]))""" ).bindparams(**as_dict) engine.execute(insert) - return + return HTTPStatus.OK diff --git a/setup-postgres.sh b/setup-postgres.sh index 430f753..0fa5509 100755 --- a/setup-postgres.sh +++ b/setup-postgres.sh @@ -15,10 +15,12 @@ fi psql $PGDATABASE << EOF CREATE TABLE routes ( \ id text, + name text, creatorName text, + creatorCharacter int, dustPoints jsonb[], checkPoints jsonb[], startLocation jsonb, endLocation jsonb, - createDate text + creationDate text );