Spaces:
Sleeping
Sleeping
File size: 2,685 Bytes
ad13d86 325fa40 ad13d86 325fa40 ad13d86 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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
);"""
@router.get("/")
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
@router.get("/{Descripcion}")
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 []
|