2022-09-18 22:43:04 +02:00
|
|
|
from fastapi import Depends
|
|
|
|
from sqlmodel import Session, select
|
|
|
|
from database.db import get_session
|
|
|
|
from database.room.models import Anonymous, Member, Room, RoomCreate
|
2022-09-16 21:50:55 +02:00
|
|
|
from database.auth.models import User
|
2022-09-18 22:43:04 +02:00
|
|
|
from services.database import generate_unique_code
|
2022-09-16 21:50:55 +02:00
|
|
|
|
2022-09-18 22:43:04 +02:00
|
|
|
def create_room_db(*,room: RoomCreate, user: User | None = None, username: str | None = None, db: Session):
|
|
|
|
id_code = generate_unique_code(Room,s=db)
|
|
|
|
room_obj = Room(**room.dict(exclude_unset=True), id_code=id_code)
|
|
|
|
if user is not None:
|
2022-09-21 22:31:50 +02:00
|
|
|
member = Member(user_id=user.id, room=room_obj, is_admin=True)
|
2022-09-18 22:43:04 +02:00
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
if username is not None:
|
|
|
|
reconnect_code = generate_unique_code(Anonymous, s=db, field_name='reconnect_code')
|
|
|
|
anonymous = Anonymous(username=username, reconnect_code=reconnect_code)
|
2022-09-21 22:31:50 +02:00
|
|
|
member = Member(anonymous=anonymous, room=room_obj, is_admin=True)
|
2022-09-18 22:43:04 +02:00
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
if username is None and user is None:
|
|
|
|
raise ValueError('Username or user required')
|
2022-09-21 22:31:50 +02:00
|
|
|
|
2022-09-18 22:43:04 +02:00
|
|
|
return {"room": room_obj, "member": member}
|
|
|
|
|
|
|
|
def check_room(room_id: str, db: Session = Depends(get_session)):
|
|
|
|
room = db.exec(select(Room).where(Room.id_code==room_id)).first()
|
|
|
|
return room
|
|
|
|
|
|
|
|
def userInRoom(room: Room, user: User, db: Session):
|
|
|
|
member = db.exec(select(Member).where(Member.room_id == room.id, Member.user_id == user.id)).first()
|
|
|
|
return member
|