
Features: - User authentication with JWT tokens - Task and project CRUD operations - Task filtering and organization generated with BackendIM... (backend.im)
122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.database import get_db
|
|
from app.models.project import Project
|
|
from app.models.task import Task
|
|
from app.schemas.project import ProjectCreate, Project as ProjectSchema, ProjectUpdate
|
|
from app.auth.dependencies import get_current_active_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter(
|
|
prefix="/projects",
|
|
tags=["projects"],
|
|
)
|
|
|
|
|
|
@router.post("/", response_model=ProjectSchema, status_code=status.HTTP_201_CREATED)
|
|
def create_project(
|
|
project: ProjectCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
# Create new project
|
|
db_project = Project(
|
|
**project.dict(),
|
|
owner_id=current_user.id
|
|
)
|
|
|
|
db.add(db_project)
|
|
db.commit()
|
|
db.refresh(db_project)
|
|
|
|
return db_project
|
|
|
|
|
|
@router.get("/", response_model=List[ProjectSchema])
|
|
def read_projects(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
projects = db.query(Project).filter(
|
|
Project.owner_id == current_user.id
|
|
).offset(skip).limit(limit).all()
|
|
|
|
return projects
|
|
|
|
|
|
@router.get("/{project_id}", response_model=ProjectSchema)
|
|
def read_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_id == current_user.id
|
|
).first()
|
|
|
|
if project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
return project
|
|
|
|
|
|
@router.put("/{project_id}", response_model=ProjectSchema)
|
|
def update_project(
|
|
project_id: int,
|
|
project_update: ProjectUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
# Get the project
|
|
db_project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_id == current_user.id
|
|
).first()
|
|
|
|
if db_project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
# Update project with non-None values from update schema
|
|
update_data = project_update.dict(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_project, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_project)
|
|
|
|
return db_project
|
|
|
|
|
|
@router.delete("/{project_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
# Get the project
|
|
db_project = db.query(Project).filter(
|
|
Project.id == project_id,
|
|
Project.owner_id == current_user.id
|
|
).first()
|
|
|
|
if db_project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
# Check if there are any tasks associated with this project
|
|
tasks_count = db.query(Task).filter(Task.project_id == project_id).count()
|
|
if tasks_count > 0:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Cannot delete project with {tasks_count} associated tasks. Please delete or reassign them first."
|
|
)
|
|
|
|
# Delete the project
|
|
db.delete(db_project)
|
|
db.commit()
|
|
|
|
return |