summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraltaf-creator <dev@altafcreator.com>2025-11-29 23:47:09 +0700
committeraltaf-creator <dev@altafcreator.com>2025-11-29 23:47:09 +0700
commit1f532d12d4fd8df4116a63ea62a8b21eba1d2db3 (patch)
treee32f64fcc26f16b4ba4025deb3c1d6a2431e10ef
parentbbf906b8f3b6a29ec3b903a99bd5a266ae66d134 (diff)
response codes
-rw-r--r--backend/__pycache__/main.cpython-314.pycbin0 -> 13768 bytes
-rw-r--r--backend/db.dbbin0 -> 8192 bytes
-rw-r--r--backend/main.py27
-rw-r--r--frontend/main.js12
-rw-r--r--frontend/start.js12
-rw-r--r--frontend/start/index.html9
6 files changed, 41 insertions, 19 deletions
diff --git a/backend/__pycache__/main.cpython-314.pyc b/backend/__pycache__/main.cpython-314.pyc
new file mode 100644
index 0000000..803bbdc
--- /dev/null
+++ b/backend/__pycache__/main.cpython-314.pyc
Binary files differ
diff --git a/backend/db.db b/backend/db.db
new file mode 100644
index 0000000..a943443
--- /dev/null
+++ b/backend/db.db
Binary files differ
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"
diff --git a/frontend/main.js b/frontend/main.js
index 6f953ce..40a965b 100644
--- a/frontend/main.js
+++ b/frontend/main.js
@@ -7,16 +7,6 @@ const data = {
const API_URL = "https://backend.laundryweb.altafcreator.com"
-// --- permission request
-const btn = document.getElementById("notbtn")
-if (btn) {
- btn.addEventListener("click", () => requestPermission())
- function requestPermission() {
- console.log("Requesting permission...");
- OneSignal.Notifications.requestPermission();
- }
-}
-
// --- check user status
// returns true if there exists a timer, and returns false otherwise.
// cookie need to be included.
@@ -216,7 +206,7 @@ async function fetchTimers() {
method: "POST",
credentials: "include",
});
- return await response.json();
+ return [response.status, response.json()];
}
const delay = (durationMs) => {
diff --git a/frontend/start.js b/frontend/start.js
new file mode 100644
index 0000000..f760f11
--- /dev/null
+++ b/frontend/start.js
@@ -0,0 +1,12 @@
+document.getElementById("startbtn").addEventListener("click", () => {
+ start();
+});
+startUpdateMachines();
+
+const notbtn = document.getElementById("notbtn")
+notbtn.addEventListener("click", () => requestPermission())
+function requestPermission() {
+ console.log("Requesting permission...");
+ OneSignal.Notifications.requestPermission();
+}
+const notif = document.getElementById("notif-panel");
diff --git a/frontend/start/index.html b/frontend/start/index.html
index 7567ad5..bd7e407 100644
--- a/frontend/start/index.html
+++ b/frontend/start/index.html
@@ -62,7 +62,7 @@
</div>
<div class="section-container no-pad">
<h1>Start</h1>
- <div class="section-container row bg-red">
+ <div class="section-container row bg-red" id="notif-panel">
<div class="flex-center-container">
<span class="icon">🔔</span>
</div>
@@ -74,11 +74,6 @@
<button class="button bg-1" id="startbtn">Start</button>
</div>
<script src="/main.js"></script>
- <script>
- document.getElementById("startbtn").addEventListener("click", () => {
- start();
- });
- startUpdateMachines();
- </script>
+ <script src="/start.js"></script>
</body>
</html>