JairoDanielMT commited on
Commit
8fc0fd9
1 Parent(s): 9ece133

Update routers/ventas.py

Browse files
Files changed (1) hide show
  1. routers/ventas.py +87 -87
routers/ventas.py CHANGED
@@ -1,87 +1,87 @@
1
- from library.librerias import *
2
- from models.ventas import Ventas
3
-
4
- router = APIRouter(
5
- prefix="/ventas",
6
- tags=["Ventas"],
7
- responses={404: {"description": "No encontrado"}},
8
- )
9
-
10
- """CREATE TABLE ventas (
11
- ID_Venta INTEGER PRIMARY KEY AUTOINCREMENT,
12
- Fecha DATE,
13
- ID_Empleado INTEGER,
14
- Total REAL,
15
- FOREIGN KEY (ID_Empleado) REFERENCES empleados (ID_Empleado)
16
- );
17
- """
18
-
19
-
20
- @router.get("/")
21
- def get_ventas():
22
- try:
23
- with DatabaseConnection().get_connection() as conn:
24
- cursor = conn.cursor()
25
- cursor.execute("SELECT * FROM ventas")
26
- ventas = cursor.fetchall()
27
- return ventas
28
- except Exception as e:
29
- print(e)
30
- return []
31
-
32
-
33
- @router.post("/")
34
- def post_venta(venta: Ventas):
35
- try:
36
- with DatabaseConnection().get_connection() as conn:
37
- cursor = conn.cursor()
38
- cursor.execute(
39
- "INSERT INTO ventas (Fecha, ID_Empleado, Total) VALUES (?, ?, ?)",
40
- (
41
- venta["Fecha"],
42
- venta["ID_Empleado"],
43
- venta["Total"],
44
- ),
45
- )
46
- conn.commit()
47
- return {"message": "Venta creada"}
48
- except Exception as e:
49
- print(e)
50
- return []
51
-
52
-
53
- @router.put("/")
54
- def put_venta(venta: Ventas):
55
- try:
56
- with DatabaseConnection().get_connection() as conn:
57
- cursor = conn.cursor()
58
- cursor.execute(
59
- "UPDATE ventas SET Fecha = ?, ID_Empleado = ?, Total = ? WHERE ID_Venta = ?",
60
- (
61
- venta["Fecha"],
62
- venta["ID_Empleado"],
63
- venta["Total"],
64
- venta["ID_Venta"],
65
- ),
66
- )
67
- conn.commit()
68
- return {"message": "Venta actualizada"}
69
- except Exception as e:
70
- print(e)
71
- return []
72
-
73
-
74
- @router.delete("/")
75
- def delete_venta(ID_Venta: int):
76
- try:
77
- with DatabaseConnection().get_connection() as conn:
78
- cursor = conn.cursor()
79
- cursor.execute(
80
- "DELETE FROM ventas WHERE ID_Venta = ?",
81
- (ID_Venta,),
82
- )
83
- conn.commit()
84
- return {"message": "Venta eliminada"}
85
- except Exception as e:
86
- print(e)
87
- return []
 
1
+ from library.librerias import *
2
+ from models.ventas import Ventas
3
+
4
+ router = APIRouter(
5
+ prefix="/ventas",
6
+ tags=["Ventas"],
7
+ responses={404: {"description": "No encontrado"}},
8
+ )
9
+
10
+ """CREATE TABLE ventas (
11
+ ID_Venta INTEGER PRIMARY KEY AUTOINCREMENT,
12
+ Fecha DATE,
13
+ ID_Empleado INTEGER,
14
+ Total REAL,
15
+ FOREIGN KEY (ID_Empleado) REFERENCES empleados (ID_Empleado)
16
+ );
17
+ """
18
+
19
+
20
+ @router.get("/")
21
+ def get_ventas():
22
+ try:
23
+ with DatabaseConnection().get_connection() as conn:
24
+ cursor = conn.cursor()
25
+ cursor.execute("SELECT * FROM ventas")
26
+ ventas = cursor.fetchall()
27
+ return ventas
28
+ except Exception as e:
29
+ print(e)
30
+ return []
31
+
32
+
33
+ # @router.post("/")
34
+ def post_venta(venta: Ventas):
35
+ try:
36
+ with DatabaseConnection().get_connection() as conn:
37
+ cursor = conn.cursor()
38
+ cursor.execute(
39
+ "INSERT INTO ventas (Fecha, ID_Empleado, Total) VALUES (?, ?, ?)",
40
+ (
41
+ venta["Fecha"],
42
+ venta["ID_Empleado"],
43
+ venta["Total"],
44
+ ),
45
+ )
46
+ conn.commit()
47
+ return {"message": "Venta creada"}
48
+ except Exception as e:
49
+ print(e)
50
+ return []
51
+
52
+
53
+ # @router.put("/")
54
+ def put_venta(venta: Ventas):
55
+ try:
56
+ with DatabaseConnection().get_connection() as conn:
57
+ cursor = conn.cursor()
58
+ cursor.execute(
59
+ "UPDATE ventas SET Fecha = ?, ID_Empleado = ?, Total = ? WHERE ID_Venta = ?",
60
+ (
61
+ venta["Fecha"],
62
+ venta["ID_Empleado"],
63
+ venta["Total"],
64
+ venta["ID_Venta"],
65
+ ),
66
+ )
67
+ conn.commit()
68
+ return {"message": "Venta actualizada"}
69
+ except Exception as e:
70
+ print(e)
71
+ return []
72
+
73
+
74
+ # @router.delete("/")
75
+ def delete_venta(ID_Venta: int):
76
+ try:
77
+ with DatabaseConnection().get_connection() as conn:
78
+ cursor = conn.cursor()
79
+ cursor.execute(
80
+ "DELETE FROM ventas WHERE ID_Venta = ?",
81
+ (ID_Venta,),
82
+ )
83
+ conn.commit()
84
+ return {"message": "Venta eliminada"}
85
+ except Exception as e:
86
+ print(e)
87
+ return []