From 1f532d12d4fd8df4116a63ea62a8b21eba1d2db3 Mon Sep 17 00:00:00 2001 From: altaf-creator Date: Sat, 29 Nov 2025 23:47:09 +0700 Subject: response codes --- backend/main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'backend/main.py') diff --git a/backend/main.py b/backend/main.py index 0042dba..aed7ce6 100644 --- a/backend/main.py +++ b/backend/main.py @@ -56,6 +56,9 @@ class RequestData(BaseModel): class BlockRequestData(BaseModel): block: int +class FinishRequestData(BaseModel): + id: int + class Status(Enum): EMPTY = 0, @@ -145,6 +148,7 @@ async def start_new_timer(data: RequestData, response: fastapi.Response, session session_key = create_session(response) except Exception as exception: print("err @ key creation //", exception) + response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR return "something went wrong during session key creation" try: @@ -166,12 +170,14 @@ async def start_new_timer(data: RequestData, response: fastapi.Response, session print("timer id", timer_id) except Exception as exception: print("err @ sql stuff //", exception) + response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR return "something went wrong during sql stuff" try: scheduler.add_job(reminder_timer_finished, 'date', run_date=end_date, id=timer_id, args=[timer_id]) except Exception as exception: print("err @ scheduler //", exception) + response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR return "something went wrong during scheduler stuff" try: @@ -180,6 +186,7 @@ async def start_new_timer(data: RequestData, response: fastapi.Response, session machine_durations[data.block - 1][data.machine - 1] = data.duration * 30 except Exception as exception: print("err @ machine_status //", exception) + response.status_code = fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR return "something went wrong during machine_status setting somehow" return "all good bro timer started" @@ -213,7 +220,7 @@ def get_machine_status(data: BlockRequestData): # --- get laundr(y/ies) information of user @app.post("/laundry") -def get_laundry_info(session_key: Annotated[str | None, fastapi.Cookie()] = None): +def get_laundry_info(response: fastapi.Response, session_key: Annotated[str | None, fastapi.Cookie()] = None): if session_key: result = [] @@ -226,14 +233,32 @@ def get_laundry_info(session_key: Annotated[str | None, fastapi.Cookie()] = None "start_time": 0, "machine": 0, "status": "RUNNING", + "id": -1, } curr_timer["duration"] = row[3] curr_timer["start_time"] = row[2] curr_timer["machine"] = row[5] curr_timer["status"] = row[6] + curr_timer["id"] = row[0] result.append(curr_timer) return result else: + response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED return "you got no session key cookie how am i supposed to identify you" + + +# --- finish one's laundry +@app.post("/finish") +def finish_laundry(data: FinishRequestData, response: fastapi.Response, session_key: Annotated[str | None, fastapi.Cookie()] = None): + if session_key: + cursor.execute(f"SELECT * FROM timers WHERE id = '{data.id}'") + row = cursor.fetchall()[0] + + if session_key != row[1]: + response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED + return "session key mismatch with timer id, dubious and hence unauthorised." + else: + response.status_code = fastapi.status.HTTP_401_UNAUTHORIZED + return "you got no session key, cannot" -- cgit v1.2.3