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
|
||||
|
||||
38
app/controllers/groupManager.py
Normal file
38
app/controllers/groupManager.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlmodel import Session, select
|
||||
from typing import List
|
||||
|
||||
from ..model.models import GroupDB, GroupResponse, GroupCreate
|
||||
from ..services.database import engine
|
||||
|
||||
group_router = APIRouter(prefix="/groups", tags=["Group"])
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as db:
|
||||
yield db
|
||||
|
||||
def add_and_refresh(db: Session, obj):
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
@group_router.get("/", response_model=List[GroupResponse])
|
||||
def get_groups(*, db: Session = Depends(get_session)):
|
||||
groups = db.exec(select(GroupDB)).all()
|
||||
return groups
|
||||
|
||||
@group_router.post("/", response_model=GroupResponse)
|
||||
def create_group(*, db: Session = Depends(get_session), group: GroupCreate):
|
||||
db_group = GroupDB.model_validate(group)
|
||||
add_and_refresh(db, db_group)
|
||||
return db_group
|
||||
|
||||
@group_router.delete("/{group_id}")
|
||||
def delete_group(*, db: Session = Depends(get_session), group_id: int):
|
||||
db_group = db.get(GroupDB, group_id)
|
||||
if db_group is None:
|
||||
raise HTTPException(status_code=404, detail="Group not found")
|
||||
db.delete(db_group)
|
||||
db.commit()
|
||||
return {"message": "Group deleted successfully"}
|
||||
@@ -5,7 +5,7 @@ from typing import List
|
||||
from ..model.models import UserResponse, UserCreate, UserDB, UserUpdate
|
||||
from ..services.database import engine
|
||||
|
||||
user_router = APIRouter(tags=["users"])
|
||||
user_router = APIRouter(tags=["Users"])
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as db:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from fastapi import FastAPI
|
||||
from .controllers import userManager, cardManager
|
||||
from .controllers import userManager, cardManager, groupManager
|
||||
from .services.database import create_db_and_tables
|
||||
|
||||
app = FastAPI()
|
||||
@@ -9,4 +9,5 @@ def on_startup():
|
||||
print("Database created and tables initialized.")
|
||||
|
||||
app.include_router(userManager.user_router)
|
||||
app.include_router(cardManager.card_router)
|
||||
app.include_router(groupManager.group_router)
|
||||
app.include_router(cardManager.card_router)
|
||||
|
||||
@@ -33,7 +33,10 @@ class AaGroupLink(Base, table=True):
|
||||
|
||||
#### Group
|
||||
class GroupBase(Base):
|
||||
name: str = Field(index=True)
|
||||
name: str = Field(index=True, unique=True)
|
||||
|
||||
class GroupCreate(GroupBase):
|
||||
pass
|
||||
|
||||
class GroupDB(GroupBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
@@ -42,8 +45,8 @@ class GroupDB(GroupBase, table=True):
|
||||
|
||||
class GroupResponse(GroupBase):
|
||||
id: int
|
||||
cards: List["Card"]
|
||||
accessauths: List["AccessAuthorizationDB"]
|
||||
cards: List["Card"] | None
|
||||
accessauths: List["AccessAuthorizationDB"] | None
|
||||
|
||||
#### AccessAuthorization
|
||||
class AccessAuthorizationBase(Base):
|
||||
|
||||
Reference in New Issue
Block a user