From 2119973dbb560b09ff09a5e21a95e939ead1ea48 Mon Sep 17 00:00:00 2001 From: Lilian Date: Tue, 28 Feb 2023 11:55:44 +0100 Subject: [PATCH] Refactor --- backend/api/database/room/crud.py | 25 ++++++++++--------------- backend/api/routes/room/consumer.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/backend/api/database/room/crud.py b/backend/api/database/room/crud.py index f6514a9..32e476e 100644 --- a/backend/api/database/room/crud.py +++ b/backend/api/database/room/crud.py @@ -1,6 +1,7 @@ import uuid from copy import deepcopy from typing import List +from uuid import uuid4 from fastapi import Depends, HTTPException, status, Query from pydantic import BaseModel @@ -16,7 +17,6 @@ from database.room.models import Anonymous, Challenge, Challenges, CorrigedGener TmpCorrection, Waiter, MemberRead, CorrigedData, CorrectionData, Challenger from services.auth import get_current_user_optional from services.database import generate_unique_code -from uuid import UUID, uuid4 def create_room_db(*, room: RoomCreate, user: User | None = None, username: str | None = None, db: Session): @@ -172,14 +172,21 @@ def validate_username(username: str, room: Room, db: Session = Depends(get_sessi 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): +def create_anonymous(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, clientId=uuid4()) + db.add(anonymous) + db.commit() + db.refresh(anonymous) + return anonymous + + +def create_anonymous_member(username: str, room: Room, db: Session): + anonymous = create_anonymous(username, room, db) member_id = generate_unique_code(Member, s=db) member = Member(room=room, anonymous=anonymous, id_code=member_id) db.add(member) @@ -188,18 +195,6 @@ def create_anonymous_member(username: str, room: Room, db: Session): return member -def create_anonymous(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) - db.add(anonymous) - db.commit() - db.refresh(anonymous) - return anonymous - def check_user_in_room(user_id: int, room_id: int, db: Session): user = db.exec(select(Member).where(Member.user_id == diff --git a/backend/api/routes/room/consumer.py b/backend/api/routes/room/consumer.py index 9067d56..5d1c41d 100644 --- a/backend/api/routes/room/consumer.py +++ b/backend/api/routes/room/consumer.py @@ -88,7 +88,7 @@ class RoomConsumer(Consumer): await self.send_to_admin(type="disconnect_waiter", payload={"waiter": serialize_member(self.member)}) async def loginMember(self, member: Member): - if member.room_id == self.room.id and member.waiting == False: + if member.room_id == self.room.id and member.waiting is False: self.member = member await self.connect_self() self.add_to_group() @@ -147,6 +147,8 @@ class RoomConsumer(Consumer): @Consumer.event('join') async def join(self, token: str | None = None, username: str | None = None): + waiter = None + if token is not None: user = get_user_from_token(token, self.db) @@ -168,8 +170,9 @@ class RoomConsumer(Consumer): elif username is not None: if len(username) < 4 or len(username) > 15: - await self.send_error("Nom d'utilisateur invalide ou indisponible") + await self.send_error("Nom invalide (4-15 caractères)") return + anonymous = create_anonymous(username, self.room, self.db) if anonymous is None: await self.send_error("Nom d'utilisateur invalide ou indisponible") @@ -178,6 +181,10 @@ class RoomConsumer(Consumer): waiter = create_member( anonymous=anonymous, room=self.room, waiting=self.room.public is False, db=self.db) + if member is None: + await self.send_error("Une erreur est survenue") + return + self.member = waiter self.add_to_group()