Spaces:
Sleeping

File size: 6,422 Bytes
2985cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from typing import List

from fastapi import APIRouter, Depends, HTTPException, status

from ...models.task import TaskCreate, TaskRead, TaskUpdate
from ...services.task_service import TaskService
from ..deps import get_current_user
from ...models.user import UserRead
from ...database.database import get_async_session
from sqlmodel.ext.asyncio.session import AsyncSession

router = APIRouter(prefix="/tasks", tags=["tasks"])


@router.get("/", response_model=dict)
async def get_tasks(
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session),
    limit: int = 20,
    offset: int = 0
):
    """
    Retrieve all tasks for the authenticated user.

    Args:
        current_user: The authenticated user
        session: Async database session
        limit: Maximum number of tasks to return (default: 20)
        offset: Number of tasks to skip (default: 0)

    Returns:
        Dictionary containing tasks and total count
    """
    try:
        tasks = await TaskService.get_tasks_for_user(session, current_user.id, limit, offset)

        return {
            "success": True,
            "data": {
                "tasks": tasks,
                "total": len(tasks)  # In a real implementation, this would come from a count query
            }
        }
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Unable to retrieve tasks: {str(e)}"
        )


@router.post("/", response_model=dict)
async def create_task(
    task_create: TaskCreate,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Create a new task for the authenticated user.

    Args:
        task_create: Task creation data
        current_user: The authenticated user
        session: Async database session

    Returns:
        Dictionary containing the created task
    """
    try:
        # Create the task with the authenticated user's ID
        created_task = await TaskService.create_task(session, task_create, current_user.id)

        return {
            "success": True,
            "data": created_task
        }
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=f"Unable to create task: {str(e)}"
        )


@router.get("/{task_id}", response_model=dict)
async def get_task(
    task_id: int,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Retrieve a specific task by ID for the authenticated user.

    Args:
        task_id: ID of the task to retrieve
        current_user: The authenticated user
        session: Async database session

    Returns:
        Dictionary containing the task data
    """
    try:
        task = await TaskService.get_task_by_id(session, task_id, current_user.id)

        return {
            "success": True,
            "data": task
        }
    except HTTPException:
        # Re-raise HTTP exceptions as-is
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Unable to retrieve task: {str(e)}"
        )


@router.put("/{task_id}", response_model=dict)
async def update_task(
    task_id: int,
    task_update: TaskUpdate,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Update a specific task by ID for the authenticated user.

    Args:
        task_id: ID of the task to update
        task_update: Task update data
        current_user: The authenticated user
        session: Async database session

    Returns:
        Dictionary containing the updated task
    """
    try:
        updated_task = await TaskService.update_task(session, task_id, task_update, current_user.id)

        return {
            "success": True,
            "data": updated_task
        }
    except HTTPException:
        # Re-raise HTTP exceptions as-is
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=f"Unable to update task: {str(e)}"
        )


@router.delete("/{task_id}", response_model=dict)
async def delete_task(
    task_id: int,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Delete a specific task by ID for the authenticated user.

    Args:
        task_id: ID of the task to delete
        current_user: The authenticated user
        session: Async database session

    Returns:
        Dictionary containing success message
    """
    try:
        await TaskService.delete_task(session, task_id, current_user.id)

        return {
            "success": True,
            "data": {
                "message": "Task deleted successfully"
            }
        }
    except HTTPException:
        # Re-raise HTTP exceptions as-is
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Unable to delete task: {str(e)}"
        )


@router.patch("/{task_id}/toggle", response_model=dict)
async def toggle_task_completion(
    task_id: int,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Toggle the completion status of a specific task.

    Args:
        task_id: ID of the task to toggle
        current_user: The authenticated user
        session: Async database session

    Returns:
        Dictionary containing the updated task
    """
    try:
        updated_task = await TaskService.toggle_task_completion(session, task_id, current_user.id)

        return {
            "success": True,
            "data": {
                "id": updated_task.id,
                "completed": updated_task.completed,
                "updated_at": updated_task.updated_at
            }
        }
    except HTTPException:
        # Re-raise HTTP exceptions as-is
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Unable to toggle task completion: {str(e)}"
        )