Spaces:
Sleeping
Sleeping
from library.librerias import * | |
from models.gastos_imprevistos import GastosImprevistos | |
router = APIRouter( | |
prefix="/gastos_imprevistos", | |
tags=["Gastos_imprevistos"], | |
responses={404: {"description": "No encontrado"}}, | |
) | |
"""CREATE TABLE gastos_imprevistos ( | |
ID_Gasto_Imprevisto INTEGER PRIMARY KEY AUTOINCREMENT, | |
Descripcion TEXT, | |
Monto REAL, | |
Fecha DATE | |
);""" | |
def get_gastos_imprevistos(): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute("SELECT * FROM gastos_imprevistos") | |
gastos_imprevistos = cursor.fetchall() | |
return gastos_imprevistos | |
except Exception as e: | |
print(e) | |
return [] | |
# @router.post("/") | |
def post_gasto_imprevisto(gasto_imprevisto: GastosImprevistos): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"INSERT INTO gastos_imprevistos (Descripcion, Monto, Fecha) VALUES (?, ?, ?)", | |
( | |
gasto_imprevisto.Descripcion, | |
gasto_imprevisto.Monto, | |
gasto_imprevisto.Fecha, | |
), | |
) | |
conn.commit() | |
return {"message": "Gasto_imprevisto creado"} | |
except Exception as e: | |
print(e) | |
return [] | |
# @router.put("/") | |
def put_gasto_imprevisto(gasto_imprevisto: GastosImprevistos): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"UPDATE gastos_imprevistos SET Descripcion = ?, Monto = ?, Fecha = ? WHERE ID_Gasto_Imprevisto = ?", | |
( | |
gasto_imprevisto.Descripcion, | |
gasto_imprevisto.Monto, | |
gasto_imprevisto.Fecha, | |
gasto_imprevisto.ID_Gasto_Imprevisto, | |
), | |
) | |
conn.commit() | |
return {"message": "Gasto_imprevisto actualizado"} | |
except Exception as e: | |
print(e) | |
return [] | |
# search by descripcion in gastos_imprevistos table with like | |
def get_gasto_imprevisto_by_descripcion(Descripcion: str): | |
try: | |
with DatabaseConnection().get_connection() as conn: | |
cursor = conn.cursor() | |
cursor.execute( | |
"SELECT * FROM gastos_imprevistos WHERE Descripcion LIKE ?", | |
(f"%{Descripcion}%",), | |
) | |
gasto_imprevisto = cursor.fetchall() | |
return gasto_imprevisto | |
except Exception as e: | |
print(e) | |
return [] | |