change structure to make package build
This commit is contained in:
6
app/__init__.py
Normal file
6
app/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from fastapi import FastAPI
|
||||
from .controllers import userManager, cardManager
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(userManager.user_router)
|
||||
app.include_router(cardManager.card_router)
|
||||
56
app/controllers/cardManager.py
Normal file
56
app/controllers/cardManager.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select
|
||||
from typing import List
|
||||
|
||||
from ..model.dbModels import Card, AccessAuthorization
|
||||
from ..model.card import CardBase, AccessAuthorizationCreate, AccessAuthorization as AccessSchema
|
||||
from ..services.database import SessionLocal, engine
|
||||
import uuid as gen_uuid
|
||||
|
||||
card_router = APIRouter(tags=["Card"])
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def register_card(db: Session, name: str):
|
||||
uuid = str(gen_uuid.uuid4()) #hier code für mifare registrierung
|
||||
card = Card(user_id=name, uuid=uuid)
|
||||
return card
|
||||
|
||||
@card_router.get("/cards", response_model=List[AccessSchema])
|
||||
def get_accesses(db: Session = Depends(get_db)):
|
||||
accesses = db.query(AccessAuthorization).all()
|
||||
if accesses is None:
|
||||
raise HTTPException(status_code=404, detail="N/A")
|
||||
return accesses
|
||||
|
||||
@card_router.post("/cards", response_model=AccessSchema)
|
||||
def create_access(access: AccessAuthorizationCreate, db: Session = Depends(get_db)):
|
||||
db_access = AccessAuthorization(**access.dict())
|
||||
card = register_card(db, access.name)
|
||||
db.add(db_access)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
db.refresh(db_access)
|
||||
return db_access
|
||||
|
||||
@card_router.get("/cards/{auth_name}", response_model=List[CardBase])
|
||||
def get_cards(auth_name: str, db: Session = Depends(get_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=CardBase)
|
||||
def add_card(auth_name: str, db: Session = Depends(get_db)):
|
||||
card = register_card(db, auth_name)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
db.refresh(card)
|
||||
return card
|
||||
56
app/controllers/userManager.py
Normal file
56
app/controllers/userManager.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from ..model.dbModels import User
|
||||
from ..model.user import UserCreate, User as UserSchema
|
||||
from ..services.database import SessionLocal, engine
|
||||
|
||||
user_router = APIRouter(tags=["users"])
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@user_router.post("/users/", response_model=UserSchema)
|
||||
def create_user(user: UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = User(**user.dict())
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
|
||||
@user_router.get("/users/", response_model=List[UserSchema])
|
||||
def read_users(db: Session = Depends(get_db)):
|
||||
users = db.query(User).all()
|
||||
return users
|
||||
|
||||
@user_router.get("/users/{user_id}", response_model=UserSchema)
|
||||
def read_user(user_id: int, db: Session = Depends(get_db)):
|
||||
db_user = db.query(User).filter(User.id == user_id).first()
|
||||
if db_user is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return db_user
|
||||
|
||||
@user_router.put("/users/{user_id}", response_model=UserSchema)
|
||||
def update_user(user_id: int, user: UserCreate, db: Session = Depends(get_db)):
|
||||
db_user = db.query(User).filter(User.id == user_id).first()
|
||||
if db_user is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
for key, value in user.dict().items():
|
||||
setattr(db_user, key, value)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
|
||||
@user_router.delete("/users/{user_id}")
|
||||
def delete_user(user_id: int, db: Session = Depends(get_db)):
|
||||
db_user = db.query(User).filter(User.id == user_id).first()
|
||||
if db_user is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
db.delete(db_user)
|
||||
db.commit()
|
||||
return {"message": "User deleted successfully"}
|
||||
6
app/main.py
Normal file
6
app/main.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from fastapi import FastAPI
|
||||
from .controllers import userManager, cardManager
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(userManager.user_router)
|
||||
app.include_router(cardManager.card_router)
|
||||
17
app/model/card.py
Normal file
17
app/model/card.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class CardBase(BaseModel):
|
||||
id: int
|
||||
uuid: str
|
||||
user_id: str
|
||||
|
||||
class AccessAuthorizationBase(BaseModel):
|
||||
name: str
|
||||
is_active: bool
|
||||
|
||||
class AccessAuthorizationCreate(AccessAuthorizationBase):
|
||||
pass
|
||||
|
||||
class AccessAuthorization(AccessAuthorizationBase):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
35
app/model/dbModels.py
Normal file
35
app/model/dbModels.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy import Column, Integer, String, Boolean
|
||||
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, relationship
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
||||
name: Mapped[str]
|
||||
email: Mapped[Optional[str]]
|
||||
password: Mapped[str]
|
||||
is_admin: Mapped[bool]
|
||||
|
||||
class AccessAuthorization(Base): #parent
|
||||
__tablename__ = "access_authorizations"
|
||||
|
||||
name: Mapped[str] = mapped_column(primary_key=True, index=True)
|
||||
is_active: Mapped[bool]
|
||||
card_id: Mapped[List["Card"]] = relationship(back_populates="user")
|
||||
|
||||
class Card(Base): #child
|
||||
__tablename__ = "cards"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
||||
uuid: Mapped[str] = mapped_column(index=True)
|
||||
user_id = mapped_column(ForeignKey("access_authorizations.name"))
|
||||
user: Mapped["AccessAuthorization"] = relationship(back_populates="card_id")
|
||||
|
||||
|
||||
14
app/model/user.py
Normal file
14
app/model/user.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class UserBase(BaseModel):
|
||||
name: str
|
||||
email: str | None
|
||||
is_admin: bool
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
class User(UserBase):
|
||||
id: int
|
||||
class Config:
|
||||
from_attributes = True
|
||||
13
app/services/database.py
Normal file
13
app/services/database.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from ..model.dbModels import Base
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./gatekeeper.db"
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||
)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
Reference in New Issue
Block a user