- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
from datetime import date
|
|
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel, ConfigDict, PositiveFloat
|
|
from sqlalchemy.orm import Session
|
|
|
|
from models.maintenance import Maintenance
|
|
from routes.dependencies import get_db
|
|
|
|
|
|
router = APIRouter(prefix="/api/maintenance", tags=["Maintenance"])
|
|
|
|
|
|
class MaintenanceBase(BaseModel):
|
|
equipment_id: int
|
|
scenario_id: int
|
|
maintenance_date: date
|
|
description: Optional[str] = None
|
|
cost: PositiveFloat
|
|
|
|
|
|
class MaintenanceCreate(MaintenanceBase):
|
|
pass
|
|
|
|
|
|
class MaintenanceUpdate(MaintenanceBase):
|
|
pass
|
|
|
|
|
|
class MaintenanceRead(MaintenanceBase):
|
|
id: int
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
def _get_maintenance_or_404(db: Session, maintenance_id: int) -> Maintenance:
|
|
maintenance = (
|
|
db.query(Maintenance).filter(Maintenance.id == maintenance_id).first()
|
|
)
|
|
if maintenance is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Maintenance record {maintenance_id} not found",
|
|
)
|
|
return maintenance
|
|
|
|
|
|
@router.post(
|
|
"/", response_model=MaintenanceRead, status_code=status.HTTP_201_CREATED
|
|
)
|
|
def create_maintenance(
|
|
maintenance: MaintenanceCreate, db: Session = Depends(get_db)
|
|
):
|
|
db_maintenance = Maintenance(**maintenance.model_dump())
|
|
db.add(db_maintenance)
|
|
db.commit()
|
|
db.refresh(db_maintenance)
|
|
return db_maintenance
|
|
|
|
|
|
@router.get("/", response_model=List[MaintenanceRead])
|
|
def list_maintenance(
|
|
skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
|
):
|
|
return db.query(Maintenance).offset(skip).limit(limit).all()
|
|
|
|
|
|
@router.get("/{maintenance_id}", response_model=MaintenanceRead)
|
|
def get_maintenance(maintenance_id: int, db: Session = Depends(get_db)):
|
|
return _get_maintenance_or_404(db, maintenance_id)
|
|
|
|
|
|
@router.put("/{maintenance_id}", response_model=MaintenanceRead)
|
|
def update_maintenance(
|
|
maintenance_id: int,
|
|
payload: MaintenanceUpdate,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
db_maintenance = _get_maintenance_or_404(db, maintenance_id)
|
|
for field, value in payload.model_dump().items():
|
|
setattr(db_maintenance, field, value)
|
|
db.commit()
|
|
db.refresh(db_maintenance)
|
|
return db_maintenance
|
|
|
|
|
|
@router.delete("/{maintenance_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_maintenance(maintenance_id: int, db: Session = Depends(get_db)):
|
|
db_maintenance = _get_maintenance_or_404(db, maintenance_id)
|
|
db.delete(db_maintenance)
|
|
db.commit()
|