from library.librerias import * from models.insumos import Insumos router = APIRouter( prefix="/insumos", tags=["Insumos"], responses={404: {"description": "No encontrado"}}, ) """CREATE TABLE insumos ( ID_Insumo INTEGER PRIMARY KEY AUTOINCREMENT, Descripcion TEXT, Tipo TEXT, Costo REAL, Cantidad INTEGER );""" @router.get("/") def get_insumos(): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM insumos") insumos = cursor.fetchall() return insumos except Exception as e: print(e) return [] # @router.post("/") def post_insumo(insumo: Insumos): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "INSERT INTO insumos (Descripcion, Tipo, Costo, Cantidad) VALUES (?, ?, ?, ?)", ( insumo.Descripcion, insumo.Tipo, insumo.Costo, insumo.Cantidad, ), ) conn.commit() return {"message": "Insumo creado"} except Exception as e: print(e) return [] # @router.put("/") def put_insumo(insumo: Insumos): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "UPDATE insumos SET Descripcion = ?, Tipo = ?, Costo = ?, Cantidad = ? WHERE ID_Insumo = ?", ( insumo.Descripcion, insumo.Tipo, insumo.Costo, insumo.Cantidad, insumo.ID_Insumo, ), ) conn.commit() return {"message": "Insumo actualizado"} except Exception as e: print(e) return [] # search by descripcion in insumos table with like @router.get("/{Descripcion}") def get_insumo_by_descripcion(Descripcion: str): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "SELECT * FROM insumos WHERE Descripcion LIKE ?", (f"%{Descripcion}%",) ) insumo = cursor.fetchall() return insumo except Exception as e: print(e) return []