Finished backend
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.DS_Store
|
||||
2
backend/.gitignore
vendored
2
backend/.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
.venv
|
||||
__pychace__/
|
||||
__pychace__
|
||||
*.pyc
|
||||
|
||||
@@ -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
2
backend/dev.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
uvicorn main:app --reload
|
||||
@@ -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
4
backend/run.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Run the server
|
||||
uvicorn main:app --port 8000
|
||||
Reference in New Issue
Block a user