diff --git a/app/controllers/cardManager.py b/app/controllers/cardManager.py index 6eabb6a..fb3668a 100644 --- a/app/controllers/cardManager.py +++ b/app/controllers/cardManager.py @@ -5,7 +5,7 @@ from sqlmodel import Session, select from typing import List from sqlalchemy.exc import NoResultFound -from ..model.models import Card, CardCreate, GroupDB +from ..model.models import Card, CardCreate, CardUpdate, GroupDB from ..services.database import engine, get_session, add_and_refresh from ..services.auth import auth_is_admin import uuid as gen_uuid @@ -56,8 +56,18 @@ def get_cards(*, db: Session = Depends(get_session), group_id: int, admin: bool cards = db.exec(select(Card).where(Card.group_id == group_id)).all() return cards - -#TODO: -# -Split Authorisations + Cards -# -Deactivation -# -Deleting \ No newline at end of file +@card_router.patch("/{card_id}", response_model=Card) +def update_card(card_id: int, cardInput: CardUpdate, db: Session = Depends(get_session), admin: bool = Depends(auth_is_admin)): + db_card = db.get(Card, card_id) + if db_card is None: + raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Card not found!") + card_data = cardInput.model_dump(exclude_unset=True, exclude_none=True) + print(card_data) + if "group_id" in card_data: + print("test") + print(db.exec(select(GroupDB).where(GroupDB.id == cardInput.group_id)).one_or_none()) + try: assert db.exec(select(GroupDB).where(GroupDB.id == cardInput.group_id)).one_or_none() is not None + except AssertionError: + raise HTTPException(status.HTTP_404_NOT_FOUND, detail="GroupID not found!") + db_card.sqlmodel_update(card_data) + return add_and_refresh(db, db_card) \ No newline at end of file diff --git a/app/model/models.py b/app/model/models.py index 02191da..7157b94 100644 --- a/app/model/models.py +++ b/app/model/models.py @@ -96,6 +96,11 @@ class CardCreate(Base): enabled: bool = True group_id: int +class CardUpdate(Base): + name: str | None = None + enabled: bool | None = None + group_id: int | None = None + class TimetableBase(Base): weekday: int = Field(le=6, ge=0) starttime: time