Add AccessAuthManager mit erstellen, zuweißen und auflist funktionen
This commit is contained in:
46
app/controllers/aaManager.py
Normal file
46
app/controllers/aaManager.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlmodel import Session, select
|
||||
from sqlalchemy.orm import selectinload
|
||||
from typing import List
|
||||
|
||||
from ..model.models import *
|
||||
from ..services.database import engine, get_session, add_and_refresh
|
||||
import uuid as gen_uuid
|
||||
|
||||
aa_router = APIRouter(prefix="/aa", tags=["AccessAuth"])
|
||||
|
||||
#Needs
|
||||
#create AA (with linked timetable)
|
||||
#Assign AA to group
|
||||
#Modify timetable
|
||||
|
||||
@aa_router.post("/add", response_model=AccessAuthorizationResponse)
|
||||
def add_aa(*, db: Session = Depends(get_session), aa: AccessAuthorizationCreate):
|
||||
print("Creating accessauth with data: ", aa)
|
||||
timetables = [Timetable.model_validate(t) for t in aa.timetables]
|
||||
db_aa = AccessAuthorizationDB(
|
||||
name=aa.name,
|
||||
is_active=aa.is_active,
|
||||
timetables=timetables
|
||||
)
|
||||
return add_and_refresh(db, db_aa)
|
||||
|
||||
@aa_router.put("/assign/{group_id}/{aa_id}", response_model=GroupResponse)
|
||||
def assign_aa(*, db: Session = Depends(get_session), group_id: int, aa_id: int):
|
||||
db_group = db.get(GroupDB, group_id)
|
||||
if db_group is None:
|
||||
raise HTTPException(status_code=404, detail="Group not found")
|
||||
db_aa = db.get(AccessAuthorizationDB, aa_id)
|
||||
if db_aa is None:
|
||||
raise HTTPException(status_code=404, detail="AA not found")
|
||||
if db_aa in db_group.accessauths:
|
||||
raise HTTPException(status_code=400, detail="AA already assigned to group")
|
||||
db_group.accessauths.append(db_aa)
|
||||
return add_and_refresh(db, db_group)
|
||||
|
||||
@aa_router.get("/", response_model=List[AccessAuthorizationResponse])
|
||||
def get_all_aa(db: Session = Depends(get_session)):
|
||||
return db.exec(
|
||||
select(AccessAuthorizationDB)
|
||||
.options(selectinload(AccessAuthorizationDB.timetables))
|
||||
).all()
|
||||
Reference in New Issue
Block a user