Spaces:
Sleeping
Sleeping
JairoDanielMT
commited on
Commit
•
2259d2e
1
Parent(s):
a79e795
Update routers/gastos.py
Browse files- routers/gastos.py +89 -89
routers/gastos.py
CHANGED
@@ -1,89 +1,89 @@
|
|
1 |
-
from library.librerias import *
|
2 |
-
from models.gastos import Gastos
|
3 |
-
|
4 |
-
router = APIRouter(
|
5 |
-
prefix="/gastos",
|
6 |
-
tags=["Gastos"],
|
7 |
-
responses={404: {"description": "No encontrado"}},
|
8 |
-
)
|
9 |
-
|
10 |
-
"""CREATE TABLE gastos (
|
11 |
-
ID_Gasto INTEGER PRIMARY KEY AUTOINCREMENT,
|
12 |
-
Tipo TEXT,
|
13 |
-
Monto REAL,
|
14 |
-
Fecha DATE
|
15 |
-
);
|
16 |
-
"""
|
17 |
-
|
18 |
-
|
19 |
-
@router.get("/")
|
20 |
-
def get_gastos():
|
21 |
-
try:
|
22 |
-
with DatabaseConnection().get_connection() as conn:
|
23 |
-
cursor = conn.cursor()
|
24 |
-
cursor.execute("SELECT * FROM gastos")
|
25 |
-
gastos = cursor.fetchall()
|
26 |
-
return gastos
|
27 |
-
except Exception as e:
|
28 |
-
print(e)
|
29 |
-
return []
|
30 |
-
|
31 |
-
|
32 |
-
@router.post("/")
|
33 |
-
def post_gasto(gasto: Gastos):
|
34 |
-
try:
|
35 |
-
with DatabaseConnection().get_connection() as conn:
|
36 |
-
cursor = conn.cursor()
|
37 |
-
cursor.execute(
|
38 |
-
"INSERT INTO gastos (Tipo, Monto, Fecha) VALUES (?, ?, ?)",
|
39 |
-
(gasto.Tipo, gasto.Monto, gasto.Fecha),
|
40 |
-
)
|
41 |
-
conn.commit()
|
42 |
-
return {"message": "Gasto creado"}
|
43 |
-
except Exception as e:
|
44 |
-
print(e)
|
45 |
-
return []
|
46 |
-
|
47 |
-
|
48 |
-
@router.put("/")
|
49 |
-
def put_gasto(gasto: Gastos):
|
50 |
-
try:
|
51 |
-
with DatabaseConnection().get_connection() as conn:
|
52 |
-
cursor = conn.cursor()
|
53 |
-
cursor.execute(
|
54 |
-
"UPDATE gastos SET Tipo = ?, Monto = ?, Fecha = ? WHERE ID_Gasto = ?",
|
55 |
-
(gasto.Tipo, gasto.Monto, gasto.Fecha, gasto.ID_Gasto),
|
56 |
-
)
|
57 |
-
conn.commit()
|
58 |
-
return {"message": "Gasto actualizado"}
|
59 |
-
except Exception as e:
|
60 |
-
print(e)
|
61 |
-
return []
|
62 |
-
|
63 |
-
|
64 |
-
@router.delete("/{ID_Gasto}")
|
65 |
-
def delete_gasto(ID_Gasto: int):
|
66 |
-
try:
|
67 |
-
with DatabaseConnection().get_connection() as conn:
|
68 |
-
cursor = conn.cursor()
|
69 |
-
cursor.execute("DELETE FROM gastos WHERE ID_Gasto = ?", (ID_Gasto,))
|
70 |
-
conn.commit()
|
71 |
-
return {"message": "Gasto eliminado"}
|
72 |
-
except Exception as e:
|
73 |
-
print(e)
|
74 |
-
return []
|
75 |
-
|
76 |
-
|
77 |
-
# listar gastos de una fecha en especifico con formato YYYY-MM-DD
|
78 |
-
@router.get("/fecha/{Fecha}")
|
79 |
-
def get_gastos_by_date(Fecha: str):
|
80 |
-
try:
|
81 |
-
fecha = datetime.strptime(Fecha, "%Y-%m-%d").date()
|
82 |
-
with DatabaseConnection().get_connection() as conn:
|
83 |
-
cursor = conn.cursor()
|
84 |
-
cursor.execute("SELECT * FROM gastos WHERE Fecha = ?", (fecha,))
|
85 |
-
gastos = cursor.fetchall()
|
86 |
-
return gastos
|
87 |
-
except Exception as e:
|
88 |
-
print(e)
|
89 |
-
return []
|
|
|
1 |
+
from library.librerias import *
|
2 |
+
from models.gastos import Gastos
|
3 |
+
|
4 |
+
router = APIRouter(
|
5 |
+
prefix="/gastos",
|
6 |
+
tags=["Gastos"],
|
7 |
+
responses={404: {"description": "No encontrado"}},
|
8 |
+
)
|
9 |
+
|
10 |
+
"""CREATE TABLE gastos (
|
11 |
+
ID_Gasto INTEGER PRIMARY KEY AUTOINCREMENT,
|
12 |
+
Tipo TEXT,
|
13 |
+
Monto REAL,
|
14 |
+
Fecha DATE
|
15 |
+
);
|
16 |
+
"""
|
17 |
+
|
18 |
+
|
19 |
+
@router.get("/")
|
20 |
+
def get_gastos():
|
21 |
+
try:
|
22 |
+
with DatabaseConnection().get_connection() as conn:
|
23 |
+
cursor = conn.cursor()
|
24 |
+
cursor.execute("SELECT * FROM gastos")
|
25 |
+
gastos = cursor.fetchall()
|
26 |
+
return gastos
|
27 |
+
except Exception as e:
|
28 |
+
print(e)
|
29 |
+
return []
|
30 |
+
|
31 |
+
|
32 |
+
# @router.post("/")
|
33 |
+
def post_gasto(gasto: Gastos):
|
34 |
+
try:
|
35 |
+
with DatabaseConnection().get_connection() as conn:
|
36 |
+
cursor = conn.cursor()
|
37 |
+
cursor.execute(
|
38 |
+
"INSERT INTO gastos (Tipo, Monto, Fecha) VALUES (?, ?, ?)",
|
39 |
+
(gasto.Tipo, gasto.Monto, gasto.Fecha),
|
40 |
+
)
|
41 |
+
conn.commit()
|
42 |
+
return {"message": "Gasto creado"}
|
43 |
+
except Exception as e:
|
44 |
+
print(e)
|
45 |
+
return []
|
46 |
+
|
47 |
+
|
48 |
+
# @router.put("/")
|
49 |
+
def put_gasto(gasto: Gastos):
|
50 |
+
try:
|
51 |
+
with DatabaseConnection().get_connection() as conn:
|
52 |
+
cursor = conn.cursor()
|
53 |
+
cursor.execute(
|
54 |
+
"UPDATE gastos SET Tipo = ?, Monto = ?, Fecha = ? WHERE ID_Gasto = ?",
|
55 |
+
(gasto.Tipo, gasto.Monto, gasto.Fecha, gasto.ID_Gasto),
|
56 |
+
)
|
57 |
+
conn.commit()
|
58 |
+
return {"message": "Gasto actualizado"}
|
59 |
+
except Exception as e:
|
60 |
+
print(e)
|
61 |
+
return []
|
62 |
+
|
63 |
+
|
64 |
+
# @router.delete("/{ID_Gasto}")
|
65 |
+
def delete_gasto(ID_Gasto: int):
|
66 |
+
try:
|
67 |
+
with DatabaseConnection().get_connection() as conn:
|
68 |
+
cursor = conn.cursor()
|
69 |
+
cursor.execute("DELETE FROM gastos WHERE ID_Gasto = ?", (ID_Gasto,))
|
70 |
+
conn.commit()
|
71 |
+
return {"message": "Gasto eliminado"}
|
72 |
+
except Exception as e:
|
73 |
+
print(e)
|
74 |
+
return []
|
75 |
+
|
76 |
+
|
77 |
+
# listar gastos de una fecha en especifico con formato YYYY-MM-DD
|
78 |
+
@router.get("/fecha/{Fecha}")
|
79 |
+
def get_gastos_by_date(Fecha: str):
|
80 |
+
try:
|
81 |
+
fecha = datetime.strptime(Fecha, "%Y-%m-%d").date()
|
82 |
+
with DatabaseConnection().get_connection() as conn:
|
83 |
+
cursor = conn.cursor()
|
84 |
+
cursor.execute("SELECT * FROM gastos WHERE Fecha = ?", (fecha,))
|
85 |
+
gastos = cursor.fetchall()
|
86 |
+
return gastos
|
87 |
+
except Exception as e:
|
88 |
+
print(e)
|
89 |
+
return []
|