backend for queues & minor front tweaks

This commit is contained in:
2024-04-11 16:39:08 +03:00
parent 8b8124d58d
commit 57965fc147
21 changed files with 210 additions and 35 deletions

View File

@ -14,3 +14,21 @@ class User(Base):
username = Column(String(length=24), unique=True, index=True)
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
owns_queues = relationship("Queue", backref="owner", lazy="dynamic")
class AnonymousUser(Base):
__tablename__ = "anonymoususers"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, index=True)
class Queue(Base):
__tablename__ = "queues"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, index=True)
description = Column(String, index=True)
owner_id = Column(UUID(as_uuid=True), ForeignKey("users.id"))

View File

@ -6,10 +6,12 @@ from .db.database import SessionLocal, engine
from .dependencies import get_db
from .views.auth.api import router as auth_router
from .views.queue.api import router as queue_router
app = FastAPI(dependencies=[Depends(get_db)])
models.Base.metadata.create_all(bind=engine)
app.include_router(queue_router)
app.include_router(auth_router)

View File

@ -68,10 +68,3 @@ async def read_users_me(
current_user: Annotated[schemas.User, Depends(services.get_current_active_user)],
):
return current_user
# @app.get("/users/me/items/")
# async def read_own_items(
# current_user: Annotated[User, Depends(get_current_active_user)],
# ):
# return [{"item_id": "Foo", "owner": current_user.username}]

View File

@ -1,5 +1,6 @@
from typing import Union
from pydantic import BaseModel
from uuid import UUID
class User(BaseModel):
@ -8,7 +9,7 @@ class User(BaseModel):
class UserInDB(User):
hashed_password: str
id: UUID
class Config:
from_attributes = True

View File

@ -20,15 +20,15 @@ def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
def get_password_hash(password) -> str:
return pwd_context.hash(password)
def get_user_by_id(db: Session, user_id: uuid.uuid4):
def get_user_by_id(db: Session, user_id: uuid.uuid4) -> models.User:
return db.query(models.User).filter(models.User.id == user_id).first()
def get_user_by_username(db: Session, username: int):
def get_user_by_username(db: Session, username: int) -> models.User:
return db.query(models.User).filter(models.User.username == username).first()
@ -62,15 +62,13 @@ def create_user(db: Session, user_data: schemas.UserRegister) -> schemas.UserInD
)
db.add(user)
db.commit()
return schemas.UserInDB(
username=user.username, name=user.name, hashed_password=user.hashed_password
)
return schemas.UserInDB.model_validate(user)
async def get_current_user(
token: Annotated[str, Depends(oauth2_scheme)],
db: Annotated[Session, Depends(get_db)],
) -> schemas.User:
) -> schemas.UserInDB:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",

View File

View File

@ -0,0 +1,41 @@
from datetime import datetime, timedelta, timezone
from typing import Annotated, Union
from sqlalchemy.orm import Session
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from pydantic import BaseModel
from ...config import jwt_config
from ...dependencies import get_db
from . import schemas
from . import services
from ..auth import services as auth_services
from ..auth import schemas as auth_schemas
router = APIRouter(
prefix="/queue",
tags=["queue"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)
@router.get("/")
async def user_queues_list(
queues: Annotated[schemas.Queue, Depends(services.get_user_queues)],
) -> list[schemas.QueueInDb]:
return queues
@router.post("/")
async def create_queue(
new_queue: schemas.Queue,
current_user: Annotated[auth_schemas.User, Depends(auth_services.get_current_user)],
db: Annotated[Session, Depends(get_db)],
) -> schemas.QueueInDb:
return services.create_queue(new_queue=new_queue, current_user=current_user, db=db)

View File

@ -0,0 +1,27 @@
from typing import Union
from pydantic import BaseModel
from uuid import UUID
class ParticipantInfo(BaseModel):
total: int
remaining: int
class Queue(BaseModel):
name: str
description: Union[str, None] = None
class QueueInList(Queue):
participants: ParticipantInfo
class Config:
from_attributes = True
class QueueInDb(Queue):
id: UUID
class Config:
from_attributes = True

View File

@ -0,0 +1,30 @@
from fastapi import Depends
from typing import Annotated
from sqlalchemy.orm import Session
from ...dependencies import get_db
from ...db import models
from ..auth import services as auth_services
from ..auth import schemas as auth_schemas
from . import schemas
def get_user_queues(
current_user: Annotated[auth_schemas.User, Depends(auth_services.get_current_user)]
) -> list[schemas.QueueInDb]:
return [schemas.QueueInDb.model_validate(q) for q in current_user.owns_queues]
def create_queue(
new_queue: schemas.Queue,
current_user: auth_schemas.UserInDB,
db: Session,
) -> schemas.QueueInDb:
q = models.Queue(
name=new_queue.name, description=new_queue.description, owner_id=current_user.id
)
db.add(q)
db.commit()
return schemas.QueueInDb.model_validate(q)