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 []