2023-08-03 10:53:38 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from models.databases.repository import Repository
|
2023-08-03 11:37:13 +03:00
|
|
|
from models.prompt import Prompt, PromptStatusEnum
|
2023-08-03 10:53:38 +03:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class CreatePromptProperties(BaseModel):
|
|
|
|
"""Properties that can be received on prompt creation"""
|
|
|
|
|
|
|
|
title: str
|
|
|
|
content: str
|
2023-08-03 11:37:13 +03:00
|
|
|
status: PromptStatusEnum = PromptStatusEnum.private
|
2023-08-03 10:53:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PromptUpdatableProperties(BaseModel):
|
|
|
|
"""Properties that can be received on prompt update"""
|
|
|
|
|
|
|
|
title: Optional[str]
|
|
|
|
content: Optional[str]
|
2023-08-03 11:37:13 +03:00
|
|
|
status: Optional[PromptStatusEnum]
|
|
|
|
|
|
|
|
|
|
|
|
class DeletePromptResponse(BaseModel):
|
|
|
|
"""Response when deleting a prompt"""
|
|
|
|
|
|
|
|
status: str = "delete"
|
|
|
|
prompt_id: UUID
|
2023-08-03 10:53:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Prompts(Repository):
|
|
|
|
def __init__(self, supabase_client):
|
|
|
|
self.db = supabase_client
|
|
|
|
|
|
|
|
def create_prompt(self, prompt: CreatePromptProperties) -> Prompt:
|
2023-08-03 11:37:13 +03:00
|
|
|
"""
|
|
|
|
Create a prompt
|
|
|
|
"""
|
2023-08-03 10:53:38 +03:00
|
|
|
|
|
|
|
response = (self.db.from_("prompts").insert(prompt.dict()).execute()).data
|
|
|
|
|
|
|
|
return Prompt(**response[0])
|
|
|
|
|
2023-08-03 11:37:13 +03:00
|
|
|
def delete_prompt_by_id(self, prompt_id: UUID) -> DeletePromptResponse:
|
2023-08-03 10:53:38 +03:00
|
|
|
"""
|
|
|
|
Delete a prompt by id
|
|
|
|
Args:
|
|
|
|
prompt_id (UUID): The id of the prompt
|
|
|
|
|
|
|
|
Returns:
|
2023-08-03 11:37:13 +03:00
|
|
|
A dictionary containing the status of the delete and prompt_id of the deleted prompt
|
2023-08-03 10:53:38 +03:00
|
|
|
"""
|
|
|
|
response = (
|
|
|
|
self.db.from_("prompts")
|
|
|
|
.delete()
|
|
|
|
.filter("id", "eq", prompt_id)
|
|
|
|
.execute()
|
|
|
|
.data
|
|
|
|
)
|
2023-08-03 11:37:13 +03:00
|
|
|
|
2023-08-03 10:53:38 +03:00
|
|
|
if response == []:
|
|
|
|
raise HTTPException(404, "Prompt not found")
|
2023-08-03 11:37:13 +03:00
|
|
|
|
|
|
|
return DeletePromptResponse(status="deleted", prompt_id=prompt_id)
|
2023-08-03 10:53:38 +03:00
|
|
|
|
|
|
|
def get_prompt_by_id(self, prompt_id: UUID) -> Prompt | None:
|
|
|
|
"""
|
|
|
|
Get a prompt by its id
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prompt_id (UUID): The id of the prompt
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Prompt: The prompt
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = (
|
|
|
|
self.db.from_("prompts").select("*").filter("id", "eq", prompt_id).execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
if response == []:
|
|
|
|
return None
|
|
|
|
return Prompt(**response[0])
|
|
|
|
|
|
|
|
def get_public_prompts(self) -> list[Prompt]:
|
|
|
|
"""
|
|
|
|
List all public prompts
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
self.db.from_("prompts")
|
|
|
|
.select("*")
|
|
|
|
.filter("status", "eq", "public")
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
def update_prompt_by_id(
|
|
|
|
self, prompt_id: UUID, prompt: PromptUpdatableProperties
|
|
|
|
) -> Prompt:
|
|
|
|
"""Update a prompt by id"""
|
|
|
|
|
|
|
|
response = (
|
|
|
|
self.db.from_("prompts")
|
|
|
|
.update(prompt.dict(exclude_unset=True))
|
|
|
|
.filter("id", "eq", prompt_id)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
if response == []:
|
|
|
|
raise HTTPException(404, "Prompt not found")
|
|
|
|
|
|
|
|
return Prompt(**response[0])
|