186 lines
5.1 KiB
Python
186 lines
5.1 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
from app.models.task import TaskStatus
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.Task])
|
|
def read_tasks(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
status: Optional[TaskStatus] = None,
|
|
completed: Optional[bool] = None,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve tasks.
|
|
"""
|
|
if status:
|
|
tasks = crud.task.get_multi_by_status(
|
|
db=db, owner_id=current_user.id, status=status, skip=skip, limit=limit
|
|
)
|
|
elif completed is not None:
|
|
tasks = crud.task.get_multi_by_completion(
|
|
db=db, owner_id=current_user.id, is_completed=completed, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
tasks = crud.task.get_multi_by_owner(
|
|
db=db, owner_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return tasks
|
|
|
|
|
|
@router.post("/", response_model=schemas.Task)
|
|
def create_task(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_in: schemas.TaskCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new task.
|
|
"""
|
|
task = crud.task.create_with_owner(db=db, obj_in=task_in, owner_id=current_user.id)
|
|
return task
|
|
|
|
|
|
@router.get("/{task_id}", response_model=schemas.Task)
|
|
def read_task(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get task by ID.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
return task
|
|
|
|
|
|
@router.put("/{task_id}", response_model=schemas.Task)
|
|
def update_task(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
task_in: schemas.TaskUpdate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a task.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
task = crud.task.update(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(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> None:
|
|
"""
|
|
Delete a task.
|
|
"""
|
|
task = crud.task.get(db=db, id=task_id)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
if task.owner_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
crud.task.remove(db=db, id=task_id)
|
|
return None
|
|
|
|
|
|
@router.post("/{task_id}/complete", response_model=schemas.Task)
|
|
def mark_task_complete(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Mark a task as complete.
|
|
"""
|
|
task = crud.task.mark_as_complete(db=db, task_id=task_id, user=current_user)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
return task
|
|
|
|
|
|
@router.post("/{task_id}/incomplete", response_model=schemas.Task)
|
|
def mark_task_incomplete(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Mark a task as incomplete.
|
|
"""
|
|
task = crud.task.mark_as_incomplete(db=db, task_id=task_id, user=current_user)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
return task
|
|
|
|
|
|
@router.post("/{task_id}/status/{status}", response_model=schemas.Task)
|
|
def update_task_status(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
task_id: int,
|
|
status: TaskStatus,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update task status.
|
|
"""
|
|
task = crud.task.update_task_status(
|
|
db=db, task_id=task_id, user=current_user, status=status
|
|
)
|
|
if not task:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Task not found"
|
|
)
|
|
return task |