Finished backend

This commit is contained in:
Leons Aleksandrovs
2025-07-02 19:52:42 +03:00
parent d72735e416
commit ebc0665468
6 changed files with 46 additions and 3 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

2
backend/.gitignore vendored
View File

@@ -1,3 +1,3 @@
.venv
__pychace__/
__pychace__
*.pyc

View File

@@ -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
}

2
backend/dev.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
uvicorn main:app --reload

View File

@@ -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)

4
backend/run.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
# Run the server
uvicorn main:app --port 8000