64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
from fastapi import APIRouter, Depends, Request
|
|
from config import AnonymousIn_schema, Room_schema, RoomIn_schema, User_schema
|
|
from database.auth.models import UserModel
|
|
from database.room.crud import create_room_anonymous_db, create_room_with_user_db, get_room_db
|
|
from database.room.models import Room
|
|
from services.auth import get_current_user_optional
|
|
router = APIRouter()
|
|
|
|
@router.post('/rooms')
|
|
async def create_room(roomData: RoomIn_schema, anonymous: AnonymousIn_schema = None, user: User_schema = Depends(get_current_user_optional)):
|
|
if user is not None:
|
|
room= await create_room_with_user_db(room = roomData, user=user)
|
|
return await Room_schema.from_tortoise_orm(room)
|
|
else:
|
|
room = await create_room_anonymous_db(room = roomData, anonymous = anonymous)
|
|
return await Room_schema.from_tortoise_orm(room)
|
|
|
|
@router.get('/room/{room_id}')
|
|
async def get_room(room_id: str):
|
|
room = await get_room_db(room_id)
|
|
if room is None:
|
|
return None
|
|
return await Room_schema.from_tortoise_orm(room)
|
|
|
|
@router.get('/room/check/{room_id}')
|
|
async def check_room(room_id:str):
|
|
room = await get_room_db(room_id)
|
|
if room is None:
|
|
return False
|
|
return True
|
|
|
|
@router.post('/room/{room_id}/join')
|
|
async def join_room(room_id: str, anonymous: AnonymousIn_schema = None, user: User_schema = Depends(get_current_user_optional)):
|
|
room = await Room.get(id_code=room_id)
|
|
user = await UserModel.get(id=user.id)
|
|
if room.private == True:
|
|
if user is not None:
|
|
await room.users_waiters.add(user)
|
|
return 'waiting'
|
|
else:
|
|
return
|
|
else:
|
|
if user is not None:
|
|
await room.users.add(user)
|
|
return 'logged in'
|
|
|
|
|
|
@router.delete('/room/{room_id}')
|
|
async def delete_room(room_id):
|
|
return
|
|
|
|
|
|
@router.get('/rooms')
|
|
async def get_rooms():
|
|
rooms = Room.all()
|
|
return await Room_schema.from_queryset(rooms)
|
|
|
|
|
|
@router.get('/test/{room_id}')
|
|
async def test(room_id):
|
|
room = await Room.get(id_code=room_id)
|
|
ano = await room.anonymousmembers
|
|
print(await ano.get(id_code='JTSGUC'))
|
|
return "user" |