File size: 1,912 Bytes
b7bf9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Task model for the sorted todo application.

Represents a todo item owned by a specific user.
"""

import uuid
from datetime import datetime
from typing import TYPE_CHECKING

from sqlalchemy import Column, ForeignKey, Index, String
from sqlmodel import Field, Relationship, SQLModel

from src.api.models.types import GUID

if TYPE_CHECKING:
    from src.api.models.user import User


class Task(SQLModel, table=True):
    """
    Task entity representing a todo item.

    Attributes:
        id: UUID primary key (client-generated)
        title: Task title (required, max 200 chars)
        description: Optional task description (max 2000 chars)
        completed: Task completion status (default False)
        is_deleted: Soft delete flag (default False)
        user_id: Foreign key reference to owning User
        created_at: Task creation timestamp
        updated_at: Last update timestamp
        user: Relationship to User entity
    """

    __tablename__ = "tasks"
    __table_args__ = (
        Index("ix_tasks_user_active_created", "user_id", "is_deleted", "created_at"),
    )

    id: uuid.UUID = Field(
        default_factory=uuid.uuid4,
        sa_column=Column(GUID, primary_key=True),
    )
    title: str = Field(sa_column=Column(String(200), nullable=False))
    description: str | None = Field(
        default=None, sa_column=Column(String(2000), nullable=True)
    )
    completed: bool = Field(default=False, nullable=False)
    is_deleted: bool = Field(default=False, nullable=False, index=True)
    user_id: uuid.UUID = Field(
        sa_column=Column(
            GUID,
            ForeignKey("users.id"),
            nullable=False,
        )
    )
    created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
    updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)

    # Relationship to User
    user: "User" = Relationship()