Spaces:
Sleeping
Sleeping
from library.librerias import * | |
from models.empleados import Empleados | |
router = APIRouter( | |
prefix="/empleados", | |
tags=["Empleados"], | |
responses={404: {"description": "No encontrado"}}, | |
) | |
"""CREATE TABLE empleados ( | |
ID_Empleado INTEGER PRIMARY KEY AUTOINCREMENT, | |
Nombre TEXT, | |
Apellido TEXT, | |
Sueldo REAL, | |
Fecha_Inicio DATE | |
);""" | |
def get_empleados(): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute("SELECT * FROM empleados") | |
empleados = cursor.fetchall() | |
return empleados | |
except Exception as e: | |
print(e) | |
return [] | |
def post_empleado(empleado: Empleados): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"INSERT INTO empleados (Nombre, Apellido, Sueldo, Fecha_Inicio) VALUES (?, ?, ?, ?)", | |
( | |
empleado.Nombre, | |
empleado.Apellido, | |
empleado.Sueldo, | |
empleado.Fecha_Inicio, | |
), | |
) | |
conn.commit() | |
return {"message": "Empleado creado"} | |
except Exception as e: | |
print(e) | |
return [] | |
def put_empleado(empleado: Empleados): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"UPDATE empleados SET Nombre = ?, Apellido = ?, Sueldo = ?, Fecha_Inicio = ? WHERE ID_Empleado = ?", | |
( | |
empleado.Nombre, | |
empleado.Apellido, | |
empleado.Sueldo, | |
empleado.Fecha_Inicio, | |
empleado.ID_Empleado, | |
), | |
) | |
conn.commit() | |
return {"message": "Empleado actualizado"} | |
except Exception as e: | |
print(e) | |
return [] | |
# search by id_empleado in empleados table | |
def get_empleado_by_id(ID_Empleado: int): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"SELECT * FROM empleados WHERE ID_Empleado = ?", (ID_Empleado,) | |
) | |
empleado = cursor.fetchall() | |
return empleado | |
except Exception as e: | |
print(e) | |
return [] | |
# search by nombre in empleados table | |
def get_empleado_by_name(Nombre: str): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute("SELECT * FROM empleados WHERE Nombre = ?", (Nombre,)) | |
empleado = cursor.fetchall() | |
return empleado | |
except Exception as e: | |
print(e) | |
return [] | |