This commit is contained in:
2025-12-13 14:39:50 +03:00
commit b666cdcb95
79 changed files with 3081 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import get_db
from app.api.deps import get_current_active_user
from app.models.user import User
from app.schemas.task import Task, TaskCreate, TaskUpdate
from app.crud import task as crud_task, schedule as crud_schedule
router = APIRouter()
@router.get("/schedule/{schedule_id}", response_model=List[Task])
async def get_tasks(
schedule_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Получить все задачи расписания"""
# Проверка прав доступа
schedule = await crud_schedule.get(db, schedule_id)
if not schedule:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Schedule not found"
)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
tasks = await crud_task.get_by_schedule(db, schedule_id)
return tasks
@router.get("/{task_id}", response_model=Task)
async def get_task(
task_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Получить задачу по ID"""
task = await crud_task.get(db, task_id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Проверка прав доступа через расписание
schedule = await crud_schedule.get(db, task.schedule_id)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
return task
@router.post("", response_model=Task, status_code=status.HTTP_201_CREATED)
async def create_task(
task_in: TaskCreate,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Создать новую задачу"""
# Проверка прав доступа
schedule = await crud_schedule.get(db, task_in.schedule_id)
if not schedule:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Schedule not found"
)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
task = await crud_task.create(db, task_in.model_dump())
return task
@router.put("/{task_id}", response_model=Task)
async def update_task(
task_id: str,
task_in: TaskUpdate,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Обновить задачу"""
task = await crud_task.get(db, task_id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Проверка прав доступа
schedule = await crud_schedule.get(db, task.schedule_id)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
update_data = task_in.model_dump(exclude_unset=True)
task = await crud_task.update(db, task, update_data)
return task
@router.patch("/{task_id}/complete", response_model=Task)
async def complete_task(
task_id: str,
completed: bool = True,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Отметить задачу как выполненную/невыполненную"""
task = await crud_task.get(db, task_id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Проверка прав доступа
schedule = await crud_schedule.get(db, task.schedule_id)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
task = await crud_task.update_completion(db, task_id, completed)
return task
@router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_task(
task_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_db)
):
"""Удалить задачу"""
task = await crud_task.get(db, task_id)
if not task:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Task not found"
)
# Проверка прав доступа
schedule = await crud_schedule.get(db, task.schedule_id)
if schedule.user_id != current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions"
)
await crud_task.delete(db, task_id)
return None