Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +18 -18
app/main.py
CHANGED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
class Task(BaseModel):
|
| 9 |
-
id: str
|
| 10 |
title: str
|
| 11 |
completed: bool = False
|
| 12 |
|
| 13 |
-
tasks
|
| 14 |
-
|
| 15 |
-
@app.post("/tasks/", response_model=Task)
|
| 16 |
def add_task(task: Task):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return task
|
| 20 |
|
| 21 |
-
@app.get("/tasks/"
|
| 22 |
def get_tasks():
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
@app.patch("/tasks/{task_id}"
|
| 26 |
def update_task(task_id: str, completed: bool):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return task
|
| 31 |
raise HTTPException(status_code=404, detail="Task not found")
|
| 32 |
|
| 33 |
@app.delete("/tasks/{task_id}")
|
| 34 |
def delete_task(task_id: str):
|
| 35 |
-
|
| 36 |
-
tasks = [task for task in tasks if task.id != task_id]
|
| 37 |
return {"detail": "Task deleted"}
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from supabase import create_client, Client
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 9 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 10 |
+
|
| 11 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 12 |
+
|
| 13 |
class Task(BaseModel):
|
|
|
|
| 14 |
title: str
|
| 15 |
completed: bool = False
|
| 16 |
|
| 17 |
+
@app.post("/tasks/")
|
|
|
|
|
|
|
| 18 |
def add_task(task: Task):
|
| 19 |
+
response = supabase.table("tasks").insert(task.dict()).execute()
|
| 20 |
+
return response.data
|
|
|
|
| 21 |
|
| 22 |
+
@app.get("/tasks/")
|
| 23 |
def get_tasks():
|
| 24 |
+
response = supabase.table("tasks").select("*").order("id", desc=False).execute()
|
| 25 |
+
return response.data
|
| 26 |
|
| 27 |
+
@app.patch("/tasks/{task_id}")
|
| 28 |
def update_task(task_id: str, completed: bool):
|
| 29 |
+
response = supabase.table("tasks").update({"completed": completed}).eq("id", task_id).execute()
|
| 30 |
+
if response.data:
|
| 31 |
+
return response.data[0]
|
|
|
|
| 32 |
raise HTTPException(status_code=404, detail="Task not found")
|
| 33 |
|
| 34 |
@app.delete("/tasks/{task_id}")
|
| 35 |
def delete_task(task_id: str):
|
| 36 |
+
supabase.table("tasks").delete().eq("id", task_id).execute()
|
|
|
|
| 37 |
return {"detail": "Task deleted"}
|