File size: 2,314 Bytes
8fc0fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
from library.librerias import *
from models.ventas import Ventas

router = APIRouter(
    prefix="/ventas",
    tags=["Ventas"],
    responses={404: {"description": "No encontrado"}},
)

"""CREATE TABLE ventas (
  ID_Venta INTEGER PRIMARY KEY AUTOINCREMENT,
  Fecha DATE,
  ID_Empleado INTEGER,
  Total REAL,
  FOREIGN KEY (ID_Empleado) REFERENCES empleados (ID_Empleado)
);
"""


@router.get("/")
def get_ventas():
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM ventas")
            ventas = cursor.fetchall()
            return ventas
    except Exception as e:
        print(e)
        return []


# @router.post("/")
def post_venta(venta: Ventas):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO ventas (Fecha, ID_Empleado, Total) VALUES (?, ?, ?)",
                (
                    venta["Fecha"],
                    venta["ID_Empleado"],
                    venta["Total"],
                ),
            )
            conn.commit()
            return {"message": "Venta creada"}
    except Exception as e:
        print(e)
        return []


# @router.put("/")
def put_venta(venta: Ventas):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "UPDATE ventas SET Fecha = ?, ID_Empleado = ?, Total = ? WHERE ID_Venta = ?",
                (
                    venta["Fecha"],
                    venta["ID_Empleado"],
                    venta["Total"],
                    venta["ID_Venta"],
                ),
            )
            conn.commit()
            return {"message": "Venta actualizada"}
    except Exception as e:
        print(e)
        return []


# @router.delete("/")
def delete_venta(ID_Venta: int):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "DELETE FROM ventas WHERE ID_Venta = ?",
                (ID_Venta,),
            )
            conn.commit()
            return {"message": "Venta eliminada"}
    except Exception as e:
        print(e)
        return []