from library.librerias import * from models.almacen import Almacen router = APIRouter( prefix="/almacen", tags=["Almacen"], responses={404: {"description": "No encontrado"}}, ) """CREATE TABLE almacen ( ID_Almacen INTEGER PRIMARY KEY AUTOINCREMENT, ID_Insumo INTEGER, Cantidad_Actual INTEGER, Cantidad_Minima INTEGER, FOREIGN KEY (ID_Insumo) REFERENCES insumos (ID_Insumo) );""" @router.get("/") def get_almacen(): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM almacen") almacen = cursor.fetchall() return almacen except Exception as e: print(e) return [] @router.post("/") def post_almacen(almacen: Almacen): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "INSERT INTO almacen (ID_Insumo, Cantidad_Actual, Cantidad_Minima) VALUES (?, ?, ?)", ( almacen.ID_Insumo, almacen.Cantidad_Actual, almacen.Cantidad_Minima, ), ) conn.commit() return {"message": "Almacen creado"} except Exception as e: print(e) return [] # search by id_insumo in almacen table @router.get("/{ID_Insumo}") def get_almacen_by_id(ID_Insumo: int): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM almacen WHERE ID_Insumo = ?", (ID_Insumo,)) almacen = cursor.fetchall() return almacen except Exception as e: print(e) return []