diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/backend/.gitignore b/backend/.gitignore index 062e3f6..bff427b 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,3 @@ .venv -__pychace__/ +__pychace__ *.pyc diff --git a/backend/db/users.py b/backend/db/users.py index ffdef6e..a8eb4d4 100644 --- a/backend/db/users.py +++ b/backend/db/users.py @@ -1,9 +1,41 @@ # This file is for database functions from db.db import get_db +conn = get_db() + def get_users(): - conn = get_db() cursor = conn.cursor() cursor.execute("SELECT * FROM Employees") users = cursor.fetchall() return users + +def get_attendance(id): + # Set up query for worked hours, group by and ordered by date + cursor = conn.cursor() + cursor.execute(""" + SELECT date, sum(hours_worked) as hours_worked, e.username + FROM Attendance a + JOIN Employees e ON a.employee_id = e.id + WHERE a.employee_id = ? + GROUP BY date + ORDER BY date DESC + """, (id,)) + data = cursor.fetchall() + + # Check if there's any attendance + if not data: + return {"username": None, "attendance": []} + + # Remove repetetive username, and return clean api response + username = data[0]["username"] # get username from first row + attendance = [ + {"date": row["date"], "hours_worked": row["hours_worked"]} + for row in data + ] + + # Return clean api response + return { + "username": username, + "attendance": attendance + } + diff --git a/backend/dev.sh b/backend/dev.sh new file mode 100755 index 0000000..061ca9f --- /dev/null +++ b/backend/dev.sh @@ -0,0 +1,2 @@ +#!/bin/bash +uvicorn main:app --reload diff --git a/backend/main.py b/backend/main.py index aa30b7b..5666f10 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,6 +1,6 @@ from fastapi import FastAPI import sqlite3 -from db.users import get_users +from db.users import get_users, get_attendance # Get fastapi app app = FastAPI() @@ -8,3 +8,7 @@ app = FastAPI() @app.get("/") def list_users(): return get_users() + +@app.get("/{eployee_id}") +def list_attendance(eployee_id: int): + return get_attendance(eployee_id) diff --git a/backend/run.sh b/backend/run.sh new file mode 100755 index 0000000..11d9f8c --- /dev/null +++ b/backend/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# Run the server +uvicorn main:app --port 8000