96 lines
3.9 KiB
Python
96 lines
3.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.database import get_db
|
|
from app.models.models import Project, Specialty, Contractor
|
|
from app.security import get_current_active_user
|
|
import app.schemas
|
|
|
|
router = APIRouter(
|
|
prefix="/projects",
|
|
tags=["Projects"],
|
|
dependencies=[Depends(get_current_active_user)]
|
|
)
|
|
|
|
@router.post("/", response_model=app.schemas.Project)
|
|
def create_project(project: app.schemas.ProjectCreate, db: Session = Depends(get_db)):
|
|
db_project = db.query(Project).filter(Project.code == project.code).first()
|
|
if db_project:
|
|
raise HTTPException(status_code=400, detail="Project code already exists")
|
|
|
|
|
|
project_data = project.dict(exclude={'specialty_ids', 'contractor_ids'})
|
|
db_project = Project(**project_data)
|
|
|
|
# Handle Parent Project
|
|
if project.parent_id:
|
|
parent = db.query(Project).filter(Project.id == project.parent_id).first()
|
|
if not parent:
|
|
raise HTTPException(status_code=404, detail="Parent project not found")
|
|
if project.specialty_ids:
|
|
specialties = db.query(Specialty).filter(Specialty.id.in_(project.specialty_ids)).all()
|
|
db_project.specialties = specialties
|
|
|
|
# Handle Contractors
|
|
if project.contractor_ids:
|
|
contractors = db.query(Contractor).filter(Contractor.id.in_(project.contractor_ids)).all()
|
|
db_project.contractors = contractors
|
|
|
|
db.add(db_project)
|
|
db.commit()
|
|
db.refresh(db_project)
|
|
return db_project
|
|
|
|
@router.get("/", response_model=List[app.schemas.Project])
|
|
def read_projects(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
projects = db.query(Project).offset(skip).limit(limit).all()
|
|
return projects
|
|
|
|
@router.get("/{project_id}", response_model=app.schemas.Project)
|
|
def read_project(project_id: int, db: Session = Depends(get_db)):
|
|
db_project = db.query(Project).filter(Project.id == project_id).first()
|
|
if db_project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
return db_project
|
|
|
|
@router.put("/{project_id}", response_model=app.schemas.Project)
|
|
def update_project(project_id: int, project: app.schemas.ProjectCreate, db: Session = Depends(get_db)):
|
|
db_project = db.query(Project).filter(Project.id == project_id).first()
|
|
if db_project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
# Update simple fields
|
|
for key, value in project.dict(exclude={'specialty_ids', 'contractor_ids'}).items():
|
|
setattr(db_project, key, value)
|
|
|
|
# Handle Parent Project
|
|
if project.parent_id is not None:
|
|
parent = db.query(Project).filter(Project.id == project.parent_id).first()
|
|
if not parent and project.parent_id != 0: # Allow 0 or null to clear? Actually null is enough
|
|
raise HTTPException(status_code=404, detail="Parent project not found")
|
|
db_project.parent_id = project.parent_id if project.parent_id != 0 else None
|
|
|
|
# Update Specialties
|
|
if project.specialty_ids is not None:
|
|
specialties = db.query(Specialty).filter(Specialty.id.in_(project.specialty_ids)).all()
|
|
db_project.specialties = specialties
|
|
|
|
# Update Contractors
|
|
if project.contractor_ids is not None:
|
|
contractors = db.query(Contractor).filter(Contractor.id.in_(project.contractor_ids)).all()
|
|
db_project.contractors = contractors
|
|
|
|
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)):
|
|
db_project = db.query(Project).filter(Project.id == project_id).first()
|
|
if db_project is None:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
db.delete(db_project)
|
|
db.commit()
|
|
return None
|