File size: 5,861 Bytes
1f982bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Depends, HTTPException, status
from typing import List
from uuid import UUID
from sqlmodel.ext.asyncio.session import AsyncSession
from ..models import (
    Task, TaskCreate, TaskUpdate, TaskPublic,
    User
)
from ..services.task_service import TaskService
from ..utils.database import get_async_session
from ..utils.logging_config import get_logger


router = APIRouter(prefix="/api/{user_id}", tags=["tasks"])
logger = get_logger(__name__)


@router.post("/tasks", response_model=TaskPublic, status_code=status.HTTP_201_CREATED)
async def create_task(
    user_id: UUID,
    task_data: TaskCreate,
    session: AsyncSession = Depends(get_async_session)
) -> TaskPublic:
    """
    Create a new task for a user.

    Args:
        user_id: The ID of the user creating the task
        task_data: The task data to create

    Returns:
        The created task
    """
    try:
        logger.info(f"Received request to create task for user {user_id}")
        task = await TaskService.create_task(session, user_id, task_data)
        logger.info(f"Successfully created task {task.id} for user {user_id}")
        return task
    except Exception as e:
        logger.error(f"Error creating task for user {user_id}: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error creating task"
        )


@router.get("/tasks", response_model=List[TaskPublic])
async def get_user_tasks(
    user_id: UUID,
    session: AsyncSession = Depends(get_async_session)
) -> List[TaskPublic]:
    """
    Get all tasks for a specific user.

    Args:
        user_id: The ID of the user whose tasks to retrieve

    Returns:
        A list of tasks for the user
    """
    try:
        logger.info(f"Received request to get tasks for user {user_id}")
        tasks = await TaskService.get_user_tasks(session, user_id)
        logger.info(f"Returning {len(tasks)} tasks for user {user_id}")
        return tasks
    except Exception as e:
        logger.error(f"Error retrieving tasks for user {user_id}: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error retrieving tasks"
        )


@router.get("/tasks/{task_id}", response_model=TaskPublic)
async def get_task(
    user_id: UUID,
    task_id: UUID,
    session: AsyncSession = Depends(get_async_session)
) -> TaskPublic:
    """
    Get a specific task by ID for a user.

    Args:
        user_id: The ID of the user
        task_id: The ID of the task to retrieve

    Returns:
        The requested task
    """
    try:
        logger.info(f"Received request to get task {task_id} for user {user_id}")
        task = await TaskService.get_task_by_id(session, user_id, task_id)

        if not task:
            logger.warning(f"Task {task_id} not found for user {user_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Task not found"
            )

        logger.info(f"Successfully retrieved task {task_id} for user {user_id}")
        return task
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error retrieving task {task_id} for user {user_id}: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error retrieving task"
        )


@router.put("/tasks/{task_id}", response_model=TaskPublic)
async def update_task(
    user_id: UUID,
    task_id: UUID,
    task_data: TaskUpdate,
    session: AsyncSession = Depends(get_async_session)
) -> TaskPublic:
    """
    Update a specific task for a user.

    Args:
        user_id: The ID of the user
        task_id: The ID of the task to update
        task_data: The updated task data

    Returns:
        The updated task
    """
    try:
        logger.info(f"Received request to update task {task_id} for user {user_id}")
        updated_task = await TaskService.update_task(session, user_id, task_id, task_data)

        if not updated_task:
            logger.warning(f"Task {task_id} not found for user {user_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Task not found"
            )

        logger.info(f"Successfully updated task {task_id} for user {user_id}")
        return updated_task
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error updating task {task_id} for user {user_id}: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error updating task"
        )


@router.delete("/tasks/{task_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_task(
    user_id: UUID,
    task_id: UUID,
    session: AsyncSession = Depends(get_async_session)
) -> None:
    """
    Delete a specific task for a user.

    Args:
        user_id: The ID of the user
        task_id: The ID of the task to delete
    """
    try:
        logger.info(f"Received request to delete task {task_id} for user {user_id}")
        success = await TaskService.delete_task(session, user_id, task_id)

        if not success:
            logger.warning(f"Task {task_id} not found for user {user_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Task not found"
            )

        logger.info(f"Successfully deleted task {task_id} for user {user_id}")
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error deleting task {task_id} for user {user_id}: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Error deleting task"
        )