from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy import func from sqlalchemy.orm import Session from ..database import get_db from ..deps import get_current_user, require_admin from ..models import Location, User from ..schemas import LocationCreate, LocationOut, LocationUpdate router = APIRouter(prefix="/locations", tags=["locations"]) @router.get("", response_model=list[LocationOut]) def list_locations( db: Session = Depends(get_db), _: User = Depends(get_current_user) ) -> list[Location]: return db.query(Location).order_by(Location.name).all() @router.post("", response_model=LocationOut, status_code=status.HTTP_201_CREATED) def create_location( payload: LocationCreate, db: Session = Depends(get_db), _: User = Depends(require_admin), ) -> Location: loc = Location(name=payload.name, parent_id=payload.parent_id) db.add(loc) db.commit() db.refresh(loc) return loc @router.patch("/{location_id}", response_model=LocationOut) def update_location( location_id: int, payload: LocationUpdate, db: Session = Depends(get_db), _: User = Depends(require_admin), ) -> Location: """Umbenennen. Chargen haengen an der ID, behalten ihren Lagerort also.""" loc = db.get(Location, location_id) if loc is None: raise HTTPException(status.HTTP_404_NOT_FOUND, "Lagerort nicht gefunden") name = payload.name.strip() doppelt = ( db.query(Location) .filter(func.lower(Location.name) == name.lower(), Location.id != location_id) .first() ) if doppelt is not None: raise HTTPException(status.HTTP_409_CONFLICT, "Diesen Lagerort gibt es bereits") loc.name = name db.commit() db.refresh(loc) return loc @router.delete("/{location_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_location( location_id: int, db: Session = Depends(get_db), _: User = Depends(require_admin), ) -> None: loc = db.get(Location, location_id) if loc is None: raise HTTPException(status.HTTP_404_NOT_FOUND, "Lagerort nicht gefunden") db.delete(loc) db.commit()