2022-09-18 22:43:04 +02:00
|
|
|
from fastapi import Depends
|
2022-09-25 22:32:19 +02:00
|
|
|
from sqlmodel import Session, select, col
|
2022-09-18 22:43:04 +02:00
|
|
|
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-25 22:32:19 +02:00
|
|
|
from database.auth.crud import get_user_from_token
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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)
|
2022-09-25 22:32:19 +02:00
|
|
|
member_id = generate_unique_code(Member, s=db)
|
2022-09-18 22:43:04 +02:00
|
|
|
room_obj = Room(**room.dict(exclude_unset=True), id_code=id_code)
|
|
|
|
if user is not None:
|
2022-09-25 22:32:19 +02:00
|
|
|
member = Member(user_id=user.id, room=room_obj, is_admin=True, id_code=member_id)
|
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-25 22:32:19 +02:00
|
|
|
member = Member(anonymous=anonymous, room=room_obj, is_admin=True, id_code=member_id)
|
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}
|
|
|
|
|
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def get_member_from_user(user_id: int, room_id: int, db: Session):
|
|
|
|
member = db.exec(select(Member).where(Member.room_id ==
|
|
|
|
room_id, Member.user_id == user_id)).first()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_token(token: str, room_id: int, db: Session):
|
|
|
|
user = get_user_from_token(token, db)
|
|
|
|
if user is False:
|
|
|
|
return False
|
|
|
|
if user is None:
|
|
|
|
return None
|
|
|
|
member = get_member_from_user(user.id, room_id, db)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_anonymous(anonymous_id: int, room_id: int, db: Session):
|
|
|
|
member = db.exec(select(Member).where(Member.room_id ==
|
|
|
|
room_id, Member.anonymous_id == anonymous_id)).first()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_reconnect_code(reconnect_code: str, room_id: int, db: Session):
|
|
|
|
anonymous = get_anonymous_from_code(reconnect_code, db)
|
|
|
|
if anonymous is None:
|
|
|
|
return None
|
|
|
|
member = get_member_from_anonymous(anonymous.id, room_id, db)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_anonymous_from_code(reconnect_code: str, db: Session):
|
|
|
|
anonymous = db.exec(select(Anonymous).where(
|
|
|
|
Anonymous.reconnect_code == reconnect_code)).first()
|
|
|
|
return anonymous
|
|
|
|
|
|
|
|
|
|
|
|
def connect_member(member: Member, db: Session):
|
|
|
|
member.online = True
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def disconnect_member(member: Member, db: Session):
|
|
|
|
if member.waiting == False:
|
|
|
|
member.online = False
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
else:
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def validate_username(username: str, room: Room, db: Session = Depends(get_session)):
|
|
|
|
print('VALIDATE', username)
|
|
|
|
if len(username) > 20:
|
|
|
|
return None
|
|
|
|
members = select(Member.anonymous_id).where(
|
|
|
|
Member.room_id == room.id, Member.anonymous_id != None)
|
|
|
|
anonymous = select(Anonymous).where(
|
|
|
|
col(Anonymous.id).in_(members), Anonymous.username == username)
|
|
|
|
username_anonymous = db.exec(anonymous).first()
|
|
|
|
return None if username_anonymous is not None else username
|
|
|
|
|
|
|
|
|
|
|
|
def create_anonymous_member(username: str, room: Room, db: Session):
|
|
|
|
username = validate_username(username, room, db)
|
|
|
|
if username is None:
|
|
|
|
return None
|
|
|
|
reconnect_code = generate_unique_code(
|
|
|
|
Anonymous, s=db, field_name="reconnect_code")
|
|
|
|
anonymous = Anonymous(username=username, reconnect_code=reconnect_code)
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, anonymous=anonymous, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def create_user_member(user: User, room: Room, db: Session):
|
|
|
|
member = get_member_from_user(user.id, room.id, db)
|
|
|
|
if member is not None:
|
|
|
|
return None
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, user=user, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def create_anonymous_waiter(username: str, room: Room, db: Session):
|
|
|
|
username = validate_username(username, room, db)
|
|
|
|
if username is None:
|
|
|
|
return None
|
|
|
|
reconnect_code = generate_unique_code(
|
|
|
|
Anonymous, s=db, field_name="reconnect_code")
|
|
|
|
anonymous = Anonymous(username=username, reconnect_code=reconnect_code)
|
|
|
|
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, anonymous=anonymous,
|
|
|
|
waiting=True, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def create_user_waiter(user: User, room: Room, db: Session):
|
|
|
|
member = get_member_from_user(user.id, room.id, db)
|
|
|
|
if member is not None:
|
|
|
|
return member
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, user=user, waiting=True,
|
|
|
|
id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_waiter(waiter_code: str, db: Session):
|
|
|
|
return db.exec(select(Member).where(Member.id_code == waiter_code)).first()
|
|
|
|
|
|
|
|
|
|
|
|
def get_member(id_code: str,room_id:str, db: Session):
|
|
|
|
return db.exec(select(Member).where(Member.id_code == id_code, Member.room_id== room_id)).first()
|
|
|
|
def delete_member(member: Member, db: Session):
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def accept_waiter(member: Member, db: Session):
|
|
|
|
member.waiting = False
|
|
|
|
member.waiter_code = None
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def refuse_waiter(member: Member, db: Session):
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def leave_room(member: Member, db: Session):
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|