Add groupManager;
reduce cardmanager to creating and deleting for now; fix typo
This commit is contained in:
@@ -6,49 +6,39 @@ from ..model.models import Card, AccessAuthorizationDB, AccessAuthorizationCreat
|
||||
from ..services.database import engine
|
||||
import uuid as gen_uuid
|
||||
|
||||
card_router = APIRouter(tags=["Card"])
|
||||
card_router = APIRouter(prefix="/cards", tags=["Card"])
|
||||
|
||||
def register_card(name: str):
|
||||
def get_session():
|
||||
with Session(engine) as db:
|
||||
yield db
|
||||
|
||||
def register_card(group_id: int):
|
||||
uuid = str(gen_uuid.uuid4()) #hier code für mifare registrierung
|
||||
card = Card(user_id=name, uuid=uuid)
|
||||
card = Card(group_id=group_id, uuid=uuid)
|
||||
return card
|
||||
|
||||
@card_router.get("/cards", response_model=List[AccessAuthorizationResponse])
|
||||
def get_accesses():
|
||||
with Session(engine) as db:
|
||||
accesses = db.query(AccessAuthorization).all()
|
||||
if accesses is None:
|
||||
raise HTTPException(status_code=404, detail="N/A")
|
||||
return accesses
|
||||
def add_and_refresh(db: Session, obj):
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
@card_router.post("/cards", response_model=AccessAuthorizationResponse)
|
||||
def create_access(access: AccessAuthorizationCreate):
|
||||
with Session(engine) as db:
|
||||
db_access = AccessAuthorization(**access.dict())
|
||||
card = register_card(access.name)
|
||||
db.add(db_access)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
db.refresh(db_access)
|
||||
return db_access
|
||||
@card_router.post("/{group_id}", response_model=Card)
|
||||
def add_card(*, db: Session = Depends(get_session), group_id: int):
|
||||
card = register_card(group_id)
|
||||
return add_and_refresh(db, card)
|
||||
|
||||
@card_router.delete("/{card_id}")
|
||||
def del_card(*, db: Session = Depends(get_session), card_id: int):
|
||||
card = db.get(Card, card_id)
|
||||
if card is None:
|
||||
raise HTTPException(status_code=404, detail="Card not found")
|
||||
db.delete(card)
|
||||
db.commit()
|
||||
return {"message": "Card deleted successfully"}
|
||||
##TBH not a big fan of having creation using group_id but deletion using card_id
|
||||
|
||||
@card_router.get("/cards/{auth_name}", response_model=List[Card])
|
||||
def get_cards(auth_name: str):
|
||||
with Session(engine) as db:
|
||||
stmt = select(AccessAuthorization).where(AccessAuthorization.name == auth_name)
|
||||
access_auth = db.execute(stmt).scalar_one_or_none()
|
||||
if access_auth is None:
|
||||
raise HTTPException(status_code=404, detail="Not found!")
|
||||
return access_auth.card_id
|
||||
|
||||
@card_router.post("/cards/{auth_name}", response_model=Card)
|
||||
def add_card(auth_name: str):
|
||||
with Session(engine) as db:
|
||||
card = register_card(auth_name)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
db.refresh(card)
|
||||
return card
|
||||
|
||||
#TODO:
|
||||
# -Split Authorisations + Cards
|
||||
|
||||
Reference in New Issue
Block a user