Spaces:
Running
Running
File size: 4,809 Bytes
05f47b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | from config import supabase
from models import Intent, IntentCreate, IntentUpdate, IntentWithCategory
from typing import List, Optional
class IntentService:
@staticmethod
async def get_all_intents(
search: Optional[str] = None,
category_id: Optional[int] = None,
priority: Optional[str] = None
) -> List[IntentWithCategory]:
"""Get all intents with optional filters"""
try:
query = supabase.table("Intents").select("*, Categories(label)")
if search:
query = query.ilike("name", f"%{search}%")
if category_id:
query = query.eq("category_id", category_id)
if priority:
query = query.eq("priority", priority)
result = query.execute()
intents = []
for item in result.data:
# Flatten nested category data
if "Categories" in item and item["Categories"]:
item["category_label"] = item["Categories"].get("label")
else:
item["category_label"] = None
# Remove nested Categories key before creating model
item.pop("Categories", None)
intent = IntentWithCategory(**item)
intents.append(intent)
return intents
except Exception as e:
raise Exception(f"Failed to fetch intents: {str(e)}")
@staticmethod
async def get_intent_by_id(intent_id: int) -> Optional[IntentWithCategory]:
"""Get a single intent by ID"""
try:
result = supabase.table("Intents").select("*, Categories(label)").eq("id", intent_id).execute()
if result.data:
item = result.data[0]
# Flatten nested category data
if "Categories" in item and item["Categories"]:
item["category_label"] = item["Categories"].get("label")
else:
item["category_label"] = None
# Remove nested Categories key before creating model
item.pop("Categories", None)
intent = IntentWithCategory(**item)
return intent
return None
except Exception as e:
raise Exception(f"Failed to fetch intent: {str(e)}")
@staticmethod
async def get_intents_by_category(category_id: int) -> List[IntentWithCategory]:
"""Get all intents for a specific category"""
try:
result = supabase.table("Intents").select("*, Categories(label)").eq("category_id", category_id).execute()
intents = []
for item in result.data:
# Flatten nested category data
if "Categories" in item and item["Categories"]:
item["category_label"] = item["Categories"].get("label")
else:
item["category_label"] = None
# Remove nested Categories key before creating model
item.pop("Categories", None)
intent = IntentWithCategory(**item)
intents.append(intent)
return intents
except Exception as e:
raise Exception(f"Failed to fetch intents for category: {str(e)}")
@staticmethod
async def create_intent(intent: IntentCreate) -> IntentWithCategory:
"""Create a new intent"""
try:
result = supabase.table("Intents").insert(intent.dict()).execute()
if result.data:
item = result.data[0]
return IntentWithCategory(**item)
raise Exception("Failed to create intent")
except Exception as e:
raise Exception(f"Failed to create intent: {str(e)}")
@staticmethod
async def update_intent(intent_id: int, intent: IntentUpdate) -> IntentWithCategory:
"""Update an existing intent"""
try:
update_data = intent.dict(exclude_unset=True)
result = supabase.table("Intents").update(update_data).eq("id", intent_id).execute()
if result.data:
item = result.data[0]
return IntentWithCategory(**item)
raise Exception("Intent not found")
except Exception as e:
raise Exception(f"Failed to update intent: {str(e)}")
@staticmethod
async def delete_intent(intent_id: int) -> bool:
"""Delete an intent"""
try:
result = supabase.table("Intents").delete().eq("id", intent_id).execute()
return True
except Exception as e:
raise Exception(f"Failed to delete intent: {str(e)}")
|