49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlmodel import Session, select
|
|
from typing import List
|
|
from sqlalchemy.exc import NoResultFound
|
|
|
|
from ..model.models import Card
|
|
from ..services.database import engine, get_session, add_and_refresh
|
|
from ..services.auth import auth_is_admin
|
|
import uuid as gen_uuid
|
|
from app.services.scanner import WriteNewCard, DeleteCard
|
|
|
|
card_router = APIRouter(prefix="/cards", tags=["Card"])
|
|
|
|
def register_card(group_id: int):
|
|
key = WriteNewCard()
|
|
if key == None:
|
|
print("No card registered. Check logs!")
|
|
raise HTTPException(status.HTTP_417_EXPECTATION_FAILED, detail="No card registered. Check logs!")
|
|
card = Card(group_id=group_id, uuid=key)
|
|
return card
|
|
|
|
@card_router.post("/{group_id}", response_model=Card)
|
|
def add_card(*, db: Session = Depends(get_session), group_id: int, admin: bool = Depends(auth_is_admin)):
|
|
card = register_card(group_id)
|
|
return add_and_refresh(db, card)
|
|
|
|
@card_router.get("/delete")
|
|
def del_card(*, db: Session = Depends(get_session), admin: bool = Depends(auth_is_admin)):
|
|
key = DeleteCard()
|
|
print(key)
|
|
try:
|
|
card = db.exec(select(Card).where(Card.uuid == key)).one()
|
|
except NoResultFound:
|
|
print(f"The key:'{key}' was not found in db!")
|
|
raise HTTPException(status_code=500, detail="Key on card not found in DB. Please tell an admin about this. KEY={key}")
|
|
db.delete(card)
|
|
db.commit()
|
|
return {"message": "Card deleted successfully"}
|
|
|
|
@card_router.get("/{group_id}", response_model=List[Card])
|
|
def get_cards(*, db: Session = Depends(get_session), group_id: int, admin: bool = Depends(auth_is_admin)):
|
|
cards = db.exec(select(Card).where(Card.group_id == group_id)).all()
|
|
return cards
|
|
|
|
|
|
#TODO:
|
|
# -Split Authorisations + Cards
|
|
# -Deactivation
|
|
# -Deleting |