File size: 1,754 Bytes
eddae92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57ac0f2
eddae92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from library.librerias import *
from models.detalle_ventas import DetalleVentas

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

"""CREATE TABLE detalle_ventas (
  ID_Venta INTEGER,
  ID_Producto INTEGER,
  Cantidad INTEGER,
  Precio_Unitario REAL,
  PRIMARY KEY (ID_Venta, ID_Producto),
  FOREIGN KEY (ID_Venta) REFERENCES ventas (ID_Venta),
  FOREIGN KEY (ID_Producto) REFERENCES productos (ID_Producto)
);"""


# get/detalle_ventas -> obtener todos los detalle_ventas con with connection as conn, manejo de errores
@router.get("/")
def get_detalle_ventas():
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM detalle_ventas")
            detalle_ventas = cursor.fetchall()
            return detalle_ventas
    except Exception as e:
        print(e)
        return []


# post/detalle_ventas -> crear un detalle_venta con with connection as conn, manejo de errores
@router.post("/")
def post_detalle_venta(detalle_venta: DetalleVentas):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO detalle_ventas (ID_Venta, ID_Producto, Cantidad, Precio_Unitario) VALUES (?, ?, ?, ?)",
                (
                    detalle_venta.ID_Venta,
                    detalle_venta.ID_Producto,
                    detalle_venta.Cantidad,
                    detalle_venta.Precio_Unitario,
                ),
            )
            conn.commit()
            return {"message": "Detalle_venta creado"}
    except Exception as e:
        print(e)
        return []