54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
import logging
|
|
from typing import Dict, Any
|
|
from datetime import datetime
|
|
|
|
logger = logging.getLogger('uvicorn.error')
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
app = FastAPI()
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # Allows all origins
|
|
allow_credentials=True,
|
|
allow_methods=["*"], # Allows all methods
|
|
allow_headers=["*"], # Allows all headers
|
|
)
|
|
|
|
db_conn = psycopg2.connect(dbname='codices',user='postgres',password='1597')
|
|
|
|
postgres_url = "http://localhost:5432"
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Hello World"}
|
|
|
|
@app.get('/plants/all')
|
|
def get_plant_list():
|
|
cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
|
cur.execute('select * from plants.watering_status')
|
|
results = [dict(x) for x in cur.fetchall()]
|
|
cur.close()
|
|
return results
|
|
|
|
|
|
@app.post('/plants/update')
|
|
def update_plant(data: Dict[str, Any]):
|
|
|
|
flag_deleted = False
|
|
action = 'Watered'
|
|
if data['action'] == 'UNDO_WATER':
|
|
flag_deleted = True
|
|
|
|
value_place = (data['plant_name'], data['plant_location'], action, datetime.now().date(), flag_deleted)
|
|
cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
|
cur.execute("INSERT INTO plants.care_transactions (plant_name, plant_location, action, record_date, flag_deleted) VALUES (%s, %s, %s, %s, %s)",value_place)
|
|
db_conn.commit()
|
|
cur.close()
|
|
# body = req.json() # Parse incoming JSON payload
|
|
# print(body)
|
|
return
|
|
|