54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
# This file is for database functions
|
|
from db.db import get_db
|
|
|
|
conn = get_db()
|
|
|
|
def get_users(search):
|
|
# Get search param
|
|
search_param = f"%{search}%"
|
|
print(search_param)
|
|
|
|
# Generate cursor
|
|
cursor = conn.cursor()
|
|
|
|
# Search if needed
|
|
if search:
|
|
cursor.execute("SELECT * FROM Employees WHERE username LIKE ?", (search_param,))
|
|
else:
|
|
cursor.execute("SELECT * FROM Employees")
|
|
|
|
# Fetch users from database
|
|
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
|
|
}
|
|
|