From 0ad971cc49d2011917c351695a8e4b10d2e1d3b9 Mon Sep 17 00:00:00 2001 From: Esa Kataja Date: Sat, 9 Nov 2024 22:21:44 +0200 Subject: [PATCH] Add Card add remove endpoints --- src/lib/database/admin.py | 25 +++++++++++++++++++++++-- src/models/card.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/lib/database/admin.py b/src/lib/database/admin.py index 125b6d9..77e2ef5 100644 --- a/src/lib/database/admin.py +++ b/src/lib/database/admin.py @@ -62,8 +62,29 @@ def set_movie_watched(movie_id: str): def add_card(card: Card): - pass + sql = """ + INSERT INTO cards (title, description, point_value) + VALUES (?, ?, ?) + """ + values = ( + card.title, + card.description, + card.point_value, + ) + try: + db_run(sql, values) + return {"message": "Card added successfully"} + except Exception as e: + return {"message": f"Error adding card: {e}"} def remove_card(card_id: str): - pass + sql = f""" + DELETE FROM cards WHERE id = '{card_id}' + """ + + try: + db_run(sql) + return {"message": "Card removed successfully"} + except Exception as e: + return {"message": f"Error removing card: {e}"} diff --git a/src/models/card.py b/src/models/card.py index 189ac57..1edc616 100644 --- a/src/models/card.py +++ b/src/models/card.py @@ -1,12 +1,29 @@ -from sqlmodel import Field, SQLModel +from pydantic import BaseModel, Field from datetime import datetime from typing import Optional -class Card(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - title: str - description: str - score: int - created_at: datetime = Field(default_factory=datetime.utcnow) - updated_at: datetime = Field(default_factory=datetime.utcnow) +class Card(BaseModel): + id: Optional[str] = Field( + default=None, + description="UUID representation of the card", + examples=["123e4567-e89b-12d3-a456-426655440000"], + ) + title: str = Field( + description="Title of the card", examples=["The Interupted Kiss"] + ) + description: str = Field( + description="Description of the card", + examples=["The couples first kiss is interrupted"], + ) + point_value: int = Field(description="Score of the card", examples=[2]) + created_at: datetime = Field( + default_factory=datetime.utcnow, + description="Creation date of the card", + examples=[datetime.utcnow()], + ) + modified_at: datetime = Field( + default_factory=datetime.utcnow, + description="Modification date of the card", + examples=[datetime.utcnow()], + )