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