update rooms.py/main.py
This commit is contained in:
+69
-2
@@ -4,6 +4,7 @@ from pydantic import BaseModel
|
|||||||
from database import fetch_all
|
from database import fetch_all
|
||||||
from events import create_event
|
from events import create_event
|
||||||
from auth import create_user, verify_user
|
from auth import create_user, verify_user
|
||||||
|
from rooms import create_room, search_room, accept_room
|
||||||
|
|
||||||
class Login(BaseModel):
|
class Login(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
@@ -25,11 +26,23 @@ class EventCreate(BaseModel):
|
|||||||
status: str
|
status: str
|
||||||
min_people: int
|
min_people: int
|
||||||
|
|
||||||
|
class RoomCreate(BaseModel):
|
||||||
|
room_name: str
|
||||||
|
creator_id: int
|
||||||
|
invite_code: str
|
||||||
|
|
||||||
|
class RoomSearch(BaseModel):
|
||||||
|
user_id: int
|
||||||
|
invite_code: str
|
||||||
|
|
||||||
|
class RoomJoin(BaseModel):
|
||||||
|
user_id: int
|
||||||
|
invite_code: str
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Calendar API",
|
title="Calendar API",
|
||||||
description="Room based agenda system",
|
description="Room based agenda system",
|
||||||
version="0.1"
|
version="1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -88,7 +101,7 @@ def api_create_user(user: UserCreate):
|
|||||||
|
|
||||||
user_id = create_user(
|
user_id = create_user(
|
||||||
username=user.username,
|
username=user.username,
|
||||||
password=user.password
|
password=user.password,
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -112,4 +125,58 @@ def api_login(login: Login):
|
|||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"user_id": user_id
|
"user_id": user_id
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.post("/create_room")
|
||||||
|
def api_create_room(room: RoomCreate):
|
||||||
|
|
||||||
|
room_id = create_room(
|
||||||
|
room.room_name,
|
||||||
|
room.creator_id,
|
||||||
|
room.invite_code
|
||||||
|
)
|
||||||
|
|
||||||
|
if room_id is None:
|
||||||
|
return {
|
||||||
|
"status": "failed"
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"room_id": room_id
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.post("/search-room")
|
||||||
|
def api_search_room(search_for_room: RoomSearch):
|
||||||
|
room_name = search_room(
|
||||||
|
search_for_room.invite_code
|
||||||
|
)
|
||||||
|
|
||||||
|
if room_name is None:
|
||||||
|
return {
|
||||||
|
"status": "failed"
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"room_name": room_name
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.post("/join-room")
|
||||||
|
def api_join_room(room: RoomJoin):
|
||||||
|
|
||||||
|
result = accept_room(
|
||||||
|
room.user_id,
|
||||||
|
room.invite_code
|
||||||
|
)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return {
|
||||||
|
"status": "failed",
|
||||||
|
"reason": "room_not_found"
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "joined",
|
||||||
|
**result
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from database import execute, fetch_one
|
||||||
|
|
||||||
|
DEFAULT_ROLE = "member"
|
||||||
|
|
||||||
|
def create_room(
|
||||||
|
room_name,
|
||||||
|
creator_id,
|
||||||
|
invite_code
|
||||||
|
):
|
||||||
|
query = """
|
||||||
|
INSERT INTO "main"."rooms"
|
||||||
|
("name", "owner_id", "invite_code")
|
||||||
|
VALUES (?, ?, ?);
|
||||||
|
"""
|
||||||
|
room_id = execute(
|
||||||
|
query,
|
||||||
|
(
|
||||||
|
room_name,
|
||||||
|
creator_id,
|
||||||
|
invite_code
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return room_id
|
||||||
|
|
||||||
|
def search_room(
|
||||||
|
invite_code
|
||||||
|
):
|
||||||
|
query = """
|
||||||
|
SELECT name
|
||||||
|
FROM rooms
|
||||||
|
WHERE invite_code = ?
|
||||||
|
"""
|
||||||
|
room = fetch_one(
|
||||||
|
query,
|
||||||
|
(
|
||||||
|
invite_code,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if room is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return room_name["name"]
|
||||||
|
|
||||||
|
|
||||||
|
def accept_room(
|
||||||
|
user_id,
|
||||||
|
invite_code
|
||||||
|
):
|
||||||
|
query = """
|
||||||
|
SELECT id
|
||||||
|
FROM rooms
|
||||||
|
WHERE invite_code = ?
|
||||||
|
"""
|
||||||
|
room = fetch_one(
|
||||||
|
query,
|
||||||
|
(
|
||||||
|
invite_code,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if room is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
room_id = room["id"]
|
||||||
|
role = DEFAULT_ROLE
|
||||||
|
query2 = """
|
||||||
|
INSERT INTO "main"."room_members"
|
||||||
|
("room_id", "user_id", "role")
|
||||||
|
VALUES (?, ?, ?);
|
||||||
|
"""
|
||||||
|
|
||||||
|
member_id = execute(
|
||||||
|
query2,
|
||||||
|
(
|
||||||
|
room_id,
|
||||||
|
user_id,
|
||||||
|
role
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"room_id": room_id,
|
||||||
|
"member_id": member_id
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user