Lagerorte und Einheiten umbenennen
Beide kannte die API bisher nur als Anlegen und Loeschen. Wer sich vertippt hatte, musste den Eintrag wegwerfen und neu anlegen - und verlor dabei genau das, was daran haengt: Chargen zeigen auf die Lagerort-ID, Produkte und Gruppen auf die Einheiten-ID. Ein Tippfehler kostete also Zuordnungen. Neu ist je ein PATCH nach dem Vorbild der Gebinde, samt Pruefung auf doppelte Namen ohne Ruecksicht auf Gross- und Kleinschreibung. Anders als beim Loeschen duerfen auch eingebaute Einheiten umbenannt werden - auch das wie bei den Gebinden. Art und Faktor einer Einheit bleiben dagegen fest: Sie stecken in bereits umgerechneten Bestaenden, eine Aenderung wuerde die still verfaelschen. Die Tests halten fest, worum es eigentlich geht - dass die Verweise das Umbenennen ueberleben. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
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
|
||||
from ..schemas import LocationCreate, LocationOut, LocationUpdate
|
||||
|
||||
router = APIRouter(prefix="/locations", tags=["locations"])
|
||||
|
||||
@@ -29,6 +30,33 @@ def create_location(
|
||||
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,
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, require_admin
|
||||
from ..models import Group, Product, Unit, User
|
||||
from ..schemas import UnitCreate, UnitOut
|
||||
from ..schemas import UnitCreate, UnitOut, UnitUpdate
|
||||
|
||||
router = APIRouter(prefix="/units", tags=["units"])
|
||||
|
||||
@@ -33,6 +33,34 @@ def create_unit(
|
||||
return unit
|
||||
|
||||
|
||||
@router.patch("/{unit_id}", response_model=UnitOut)
|
||||
def update_unit(
|
||||
unit_id: int,
|
||||
payload: UnitUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_admin),
|
||||
) -> Unit:
|
||||
"""Umbenennen – auch bei eingebauten Einheiten, wie bei den Gebinden.
|
||||
Produkte und Gruppen verweisen ueber die ID und bleiben unberuehrt."""
|
||||
unit = db.get(Unit, unit_id)
|
||||
if unit is None:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Einheit nicht gefunden")
|
||||
|
||||
name = payload.name.strip()
|
||||
doppelt = (
|
||||
db.query(Unit)
|
||||
.filter(func.lower(Unit.name) == name.lower(), Unit.id != unit_id)
|
||||
.first()
|
||||
)
|
||||
if doppelt is not None:
|
||||
raise HTTPException(status.HTTP_409_CONFLICT, "Einheit existiert bereits")
|
||||
|
||||
unit.name = name
|
||||
db.commit()
|
||||
db.refresh(unit)
|
||||
return unit
|
||||
|
||||
|
||||
@router.delete("/{unit_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_unit(
|
||||
unit_id: int,
|
||||
|
||||
Reference in New Issue
Block a user