File size: 2,846 Bytes
18ade12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Task model for the Task Management API.

Represents a task item in the task management system.
Per data-model.md specification.
"""

import uuid
from datetime import datetime
from typing import Optional

from pydantic import BaseModel
from sqlmodel import Field, SQLModel


class Task(SQLModel, table=True):
    """
    Task entity representing a to-do item owned by a user.

    Attributes:
        id: UUID primary key
        user_id: Owner user ID (from JWT)
        title: Task title (required, max 255 chars)
        description: Optional detailed description (max 2000 chars)
        is_completed: Completion status (default False)
        created_at: Timestamp when record was created
        updated_at: Timestamp when record was last updated
    """

    __tablename__ = "tasks"

    # Primary key
    id: uuid.UUID = Field(
        default_factory=uuid.uuid4,
        primary_key=True,
        description="Unique identifier"
    )

    # Required fields - Better Auth uses string IDs
    user_id: str = Field(
        description="Owner user ID from JWT (string format)"
    )

    title: str = Field(
        min_length=1,
        max_length=255,
        description="Task title (required, 1-255 chars)"
    )

    # Optional fields
    description: Optional[str] = Field(
        default=None,
        max_length=2000,
        description="Optional detailed description (max 2000 chars)"
    )

    # Status field with default
    is_completed: bool = Field(
        default=False,
        description="Completion status (default False)"
    )

    # Timestamps (naive UTC - no timezone info for PostgreSQL compatibility)
    created_at: datetime = Field(
        default_factory=lambda: datetime.utcnow(),
        description="Record creation timestamp"
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.utcnow(),
        description="Last update timestamp"
    )


class TaskCreate(SQLModel):
    """Schema for creating a new task."""

    title: str = Field(min_length=1, max_length=255, description="Task title (required)")
    description: Optional[str] = Field(default=None, max_length=2000, description="Task description (optional)")


class TaskUpdate(SQLModel):
    """Schema for updating task data."""

    title: Optional[str] = Field(default=None, min_length=1, max_length=255, description="Task title (optional)")
    description: Optional[str] = Field(default=None, max_length=2000, description="Task description (optional)")
    is_completed: Optional[bool] = Field(default=None, description="Completion status (optional)")


class TaskResponse(BaseModel):
    """Schema for reading task data."""

    id: uuid.UUID
    user_id: str  # Better Auth uses string IDs
    title: str
    description: Optional[str]
    is_completed: bool
    created_at: datetime
    updated_at: datetime