120 lines
3.7 KiB
Python
120 lines
3.7 KiB
Python
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.core.auth import get_current_user
|
|
from app.schemas.task import TaskCreate, TaskUpdate, TaskResponse
|
|
from app.crud import task as crud_task
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=TaskResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_task(
|
|
task_in: TaskCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Create a new task for the current user.
|
|
"""
|
|
task = crud_task.create_task(db=db, task_in=task_in, owner_id=current_user.id)
|
|
return task
|
|
|
|
@router.get("/", response_model=List[TaskResponse])
|
|
def read_tasks(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
status: Optional[str] = None
|
|
):
|
|
"""
|
|
Retrieve all tasks for the current user.
|
|
Optional status filter available.
|
|
"""
|
|
tasks = crud_task.get_tasks_by_owner(
|
|
db=db,
|
|
owner_id=current_user.id,
|
|
skip=skip,
|
|
limit=limit,
|
|
status=status
|
|
)
|
|
return tasks
|
|
|
|
@router.get("/{task_id}", response_model=TaskResponse)
|
|
def read_task(
|
|
task_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Get a specific task by ID.
|
|
"""
|
|
task = crud_task.get_task(db=db, task_id=task_id)
|
|
if not task:
|
|
raise HTTPException(status_code=404, detail="Task not found")
|
|
if task.owner_id != current_user.id and task.assigned_to_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
return task
|
|
|
|
@router.put("/{task_id}", response_model=TaskResponse)
|
|
def update_task(
|
|
task_id: int,
|
|
task_in: TaskUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task = crud_task.get_task(db=db, task_id=task_id)
|
|
if not task:
|
|
raise HTTPException(status_code=404, detail="Task not found")
|
|
if task.owner_id != current_user.id and task.assigned_to_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
task = crud_task.update_task(db=db, db_obj=task, obj_in=task_in)
|
|
return task
|
|
|
|
@router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_task(
|
|
task_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task = crud_task.get_task(db=db, task_id=task_id)
|
|
if not task:
|
|
raise HTTPException(status_code=404, detail="Task not found")
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
crud_task.delete_task(db=db, task_id=task_id)
|
|
return None
|
|
|
|
@router.post("/{task_id}/assign/{user_id}", response_model=TaskResponse)
|
|
def assign_task(
|
|
task_id: int,
|
|
user_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Assign a task to another user.
|
|
"""
|
|
task = crud_task.get_task(db=db, task_id=task_id)
|
|
if not task:
|
|
raise HTTPException(status_code=404, detail="Task not found")
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
|
|
|
# Check if user exists
|
|
from app.crud.user import get_user
|
|
user = get_user(db=db, user_id=user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
task = crud_task.assign_task(db=db, task_id=task_id, assigned_to_id=user_id)
|
|
return task |