sharktide commited on
Commit
5656488
·
verified ·
1 Parent(s): 82608f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # supabase_client.py
2
+ from supabase import create_client, Client
3
+ import os
4
+
5
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
6
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY") # Use anon key for now
7
+
8
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
9
+
10
+ from fastapi import FastAPI, HTTPException
11
+ from pydantic import BaseModel
12
+ from typing import List, Optional
13
+ from fastapi.middleware.cors import CORSMiddleware
14
+ from supabase_client import supabase
15
+
16
+ app = FastAPI()
17
+
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+
26
+ class Recipe(BaseModel):
27
+ name: str
28
+ time: str
29
+ creator: str
30
+ user_id: Optional[str] = None
31
+ category: str
32
+ diff: str
33
+ description: str
34
+ ingredients: List[str]
35
+ instructions: str
36
+
37
+ @app.put("/supabase/add/recipe")
38
+ def add_recipe_to_supabase(recipe: Recipe):
39
+ # Optional duplicate name check
40
+ existing = supabase.table("recipes").select("name").eq("name", recipe.name).execute()
41
+ if existing.data:
42
+ raise HTTPException(status_code=400, detail="Recipe with this name already exists.")
43
+
44
+ response = supabase.table("recipes").insert({
45
+ "name": recipe.name,
46
+ "time": recipe.time,
47
+ "creator": recipe.creator,
48
+ "user_id": recipe.user_id,
49
+ "category": recipe.category,
50
+ "diff": recipe.diff,
51
+ "description": recipe.description,
52
+ "ingredients": recipe.ingredients,
53
+ "instructions": recipe.instructions
54
+ }).execute()
55
+
56
+ if response.error:
57
+ raise HTTPException(status_code=500, detail=str(response.error))
58
+
59
+ return {"message": "Recipe stored in Supabase"}