Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Add User in Event Endpoint #50

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
get_event_results,
update_avail,
fix_event,
check_user_in_event,
)
from event import Event
from availability import Availability
Expand Down Expand Up @@ -238,3 +239,20 @@ async def get_results(request: Request):
return {"message": "Invalid event code"}

return get_event_results(body["event_code"])


@app.post("/user_in_event")
async def user_in_event(request: Request):
body: dict = await request.json()
user_id = check_login(body)
if user_id < 0:
return {"message": "Login failed"}
else:
body["account_id"] = user_id

if "event_code" not in body:
return {"message": "Missing field: event_code"}
if not check_code_event(body["event_code"]):
return {"message": "Invalid event code"}

return check_user_in_event(body)
4 changes: 2 additions & 2 deletions backend/availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def from_sql(cursor: MySQLCursorDict, code: str) -> List["Availability"]:
time_row,
is_available
FROM
availability
user_event_availability
INNER JOIN url_code USING (user_event_id)
INNER JOIN user_event_participant USING (user_event_id, user_account_id)
WHERE
Expand All @@ -57,7 +57,7 @@ def from_sql(cursor: MySQLCursorDict, code: str) -> List["Availability"]:
curr_date = None
for row in cursor.fetchall():
if row["nickname"] not in avail_grids:
avail_grids[row["nickname"]] = [[]]
avail_grids[row["nickname"]] = []
user_ids[row["nickname"]] = row["user_account_id"]
curr_nickname = row["nickname"]
if row["date_column"] != curr_date:
Expand Down
30 changes: 30 additions & 0 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,33 @@ def get_event_results(code: str) -> List[Availability]:
avail_json[avail.nickname] = avail.to_json()["availability"]

return avail_json


def check_user_in_event(json: dict) -> dict:
query = """
SELECT
user_account_id
FROM
user_event_participant
WHERE
user_account_id = %s
AND user_event_id = (
SELECT
user_event_id
FROM
url_code
WHERE
url_code = %s
)
"""
try:
DB_CURSOR.execute(query, (json["account_id"], json["event_code"]))
if DB_CURSOR.fetchone() is None:
return {"message": "User not in event"}
avails = Availability.from_sql(DB_CURSOR, json["event_code"])
for avail in avails:
if avail.user.id == json["account_id"]:
return avail.to_json()
except MySQL.Error as e:
print(e)
return {"message": "Database error"}