Spaces:
Running
Running
JairoDanielMT
commited on
Commit
•
ad13d86
1
Parent(s):
2259d2e
Update routers/gastos_imprevistos.py
Browse files- routers/gastos_imprevistos.py +86 -86
routers/gastos_imprevistos.py
CHANGED
@@ -1,86 +1,86 @@
|
|
1 |
-
from library.librerias import *
|
2 |
-
from models.gastos_imprevistos import GastosImprevistos
|
3 |
-
|
4 |
-
router = APIRouter(
|
5 |
-
prefix="/gastos_imprevistos",
|
6 |
-
tags=["Gastos_imprevistos"],
|
7 |
-
responses={404: {"description": "No encontrado"}},
|
8 |
-
)
|
9 |
-
|
10 |
-
"""CREATE TABLE gastos_imprevistos (
|
11 |
-
ID_Gasto_Imprevisto INTEGER PRIMARY KEY AUTOINCREMENT,
|
12 |
-
Descripcion TEXT,
|
13 |
-
Monto REAL,
|
14 |
-
Fecha DATE
|
15 |
-
);"""
|
16 |
-
|
17 |
-
|
18 |
-
@router.get("/")
|
19 |
-
def get_gastos_imprevistos():
|
20 |
-
try:
|
21 |
-
with DatabaseConnection().get_connection() as conn:
|
22 |
-
cursor = conn.cursor()
|
23 |
-
cursor.execute("SELECT * FROM gastos_imprevistos")
|
24 |
-
gastos_imprevistos = cursor.fetchall()
|
25 |
-
return gastos_imprevistos
|
26 |
-
except Exception as e:
|
27 |
-
print(e)
|
28 |
-
return []
|
29 |
-
|
30 |
-
|
31 |
-
@router.post("/")
|
32 |
-
def post_gasto_imprevisto(gasto_imprevisto: GastosImprevistos):
|
33 |
-
try:
|
34 |
-
with DatabaseConnection().get_connection() as conn:
|
35 |
-
cursor = conn.cursor()
|
36 |
-
cursor.execute(
|
37 |
-
"INSERT INTO gastos_imprevistos (Descripcion, Monto, Fecha) VALUES (?, ?, ?)",
|
38 |
-
(
|
39 |
-
gasto_imprevisto.Descripcion,
|
40 |
-
gasto_imprevisto.Monto,
|
41 |
-
gasto_imprevisto.Fecha,
|
42 |
-
),
|
43 |
-
)
|
44 |
-
conn.commit()
|
45 |
-
return {"message": "Gasto_imprevisto creado"}
|
46 |
-
except Exception as e:
|
47 |
-
print(e)
|
48 |
-
return []
|
49 |
-
|
50 |
-
|
51 |
-
@router.put("/")
|
52 |
-
def put_gasto_imprevisto(gasto_imprevisto: GastosImprevistos):
|
53 |
-
try:
|
54 |
-
with DatabaseConnection().get_connection() as conn:
|
55 |
-
cursor = conn.cursor()
|
56 |
-
cursor.execute(
|
57 |
-
"UPDATE gastos_imprevistos SET Descripcion = ?, Monto = ?, Fecha = ? WHERE ID_Gasto_Imprevisto = ?",
|
58 |
-
(
|
59 |
-
gasto_imprevisto.Descripcion,
|
60 |
-
gasto_imprevisto.Monto,
|
61 |
-
gasto_imprevisto.Fecha,
|
62 |
-
gasto_imprevisto.ID_Gasto_Imprevisto,
|
63 |
-
),
|
64 |
-
)
|
65 |
-
conn.commit()
|
66 |
-
return {"message": "Gasto_imprevisto actualizado"}
|
67 |
-
except Exception as e:
|
68 |
-
print(e)
|
69 |
-
return []
|
70 |
-
|
71 |
-
|
72 |
-
# search by descripcion in gastos_imprevistos table with like
|
73 |
-
@router.get("/{Descripcion}")
|
74 |
-
def get_gasto_imprevisto_by_descripcion(Descripcion: str):
|
75 |
-
try:
|
76 |
-
with DatabaseConnection().get_connection() as conn:
|
77 |
-
cursor = conn.cursor()
|
78 |
-
cursor.execute(
|
79 |
-
"SELECT * FROM gastos_imprevistos WHERE Descripcion LIKE ?",
|
80 |
-
(f"%{Descripcion}%",),
|
81 |
-
)
|
82 |
-
gasto_imprevisto = cursor.fetchall()
|
83 |
-
return gasto_imprevisto
|
84 |
-
except Exception as e:
|
85 |
-
print(e)
|
86 |
-
return []
|
|
|
1 |
+
from library.librerias import *
|
2 |
+
from models.gastos_imprevistos import GastosImprevistos
|
3 |
+
|
4 |
+
router = APIRouter(
|
5 |
+
prefix="/gastos_imprevistos",
|
6 |
+
tags=["Gastos_imprevistos"],
|
7 |
+
responses={404: {"description": "No encontrado"}},
|
8 |
+
)
|
9 |
+
|
10 |
+
"""CREATE TABLE gastos_imprevistos (
|
11 |
+
ID_Gasto_Imprevisto INTEGER PRIMARY KEY AUTOINCREMENT,
|
12 |
+
Descripcion TEXT,
|
13 |
+
Monto REAL,
|
14 |
+
Fecha DATE
|
15 |
+
);"""
|
16 |
+
|
17 |
+
|
18 |
+
@router.get("/")
|
19 |
+
def get_gastos_imprevistos():
|
20 |
+
try:
|
21 |
+
with DatabaseConnection().get_connection() as conn:
|
22 |
+
cursor = conn.cursor()
|
23 |
+
cursor.execute("SELECT * FROM gastos_imprevistos")
|
24 |
+
gastos_imprevistos = cursor.fetchall()
|
25 |
+
return gastos_imprevistos
|
26 |
+
except Exception as e:
|
27 |
+
print(e)
|
28 |
+
return []
|
29 |
+
|
30 |
+
|
31 |
+
# @router.post("/")
|
32 |
+
def post_gasto_imprevisto(gasto_imprevisto: GastosImprevistos):
|
33 |
+
try:
|
34 |
+
with DatabaseConnection().get_connection() as conn:
|
35 |
+
cursor = conn.cursor()
|
36 |
+
cursor.execute(
|
37 |
+
"INSERT INTO gastos_imprevistos (Descripcion, Monto, Fecha) VALUES (?, ?, ?)",
|
38 |
+
(
|
39 |
+
gasto_imprevisto.Descripcion,
|
40 |
+
gasto_imprevisto.Monto,
|
41 |
+
gasto_imprevisto.Fecha,
|
42 |
+
),
|
43 |
+
)
|
44 |
+
conn.commit()
|
45 |
+
return {"message": "Gasto_imprevisto creado"}
|
46 |
+
except Exception as e:
|
47 |
+
print(e)
|
48 |
+
return []
|
49 |
+
|
50 |
+
|
51 |
+
# @router.put("/")
|
52 |
+
def put_gasto_imprevisto(gasto_imprevisto: GastosImprevistos):
|
53 |
+
try:
|
54 |
+
with DatabaseConnection().get_connection() as conn:
|
55 |
+
cursor = conn.cursor()
|
56 |
+
cursor.execute(
|
57 |
+
"UPDATE gastos_imprevistos SET Descripcion = ?, Monto = ?, Fecha = ? WHERE ID_Gasto_Imprevisto = ?",
|
58 |
+
(
|
59 |
+
gasto_imprevisto.Descripcion,
|
60 |
+
gasto_imprevisto.Monto,
|
61 |
+
gasto_imprevisto.Fecha,
|
62 |
+
gasto_imprevisto.ID_Gasto_Imprevisto,
|
63 |
+
),
|
64 |
+
)
|
65 |
+
conn.commit()
|
66 |
+
return {"message": "Gasto_imprevisto actualizado"}
|
67 |
+
except Exception as e:
|
68 |
+
print(e)
|
69 |
+
return []
|
70 |
+
|
71 |
+
|
72 |
+
# search by descripcion in gastos_imprevistos table with like
|
73 |
+
@router.get("/{Descripcion}")
|
74 |
+
def get_gasto_imprevisto_by_descripcion(Descripcion: str):
|
75 |
+
try:
|
76 |
+
with DatabaseConnection().get_connection() as conn:
|
77 |
+
cursor = conn.cursor()
|
78 |
+
cursor.execute(
|
79 |
+
"SELECT * FROM gastos_imprevistos WHERE Descripcion LIKE ?",
|
80 |
+
(f"%{Descripcion}%",),
|
81 |
+
)
|
82 |
+
gasto_imprevisto = cursor.fetchall()
|
83 |
+
return gasto_imprevisto
|
84 |
+
except Exception as e:
|
85 |
+
print(e)
|
86 |
+
return []
|