from library.librerias import * from models.maquinas import Maquinas router = APIRouter( prefix="/maquinas", tags=["Maquinas"], responses={404: {"description": "No encontrado"}}, ) """CREATE TABLE maquinas ( ID_Maquina INTEGER PRIMARY KEY AUTOINCREMENT, Tipo TEXT, Capacidad INTEGER, Consumo_Energetico REAL );""" @router.get("/") def get_maquinas(): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM maquinas") maquinas = cursor.fetchall() return maquinas except Exception as e: print(e) return [] # @router.post("/") def post_maquina(maquina: Maquinas): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "INSERT INTO maquinas (Tipo, Capacidad, Consumo_Energetico) VALUES (?, ?, ?)", ( maquina.Tipo, maquina.Capacidad, maquina.Consumo_Energetico, ), ) conn.commit() return {"message": "Maquina creada"} except Exception as e: print(e) return [] # @router.put("/") def put_maquina(maquina: Maquinas): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "UPDATE maquinas SET Tipo = ?, Capacidad = ?, Consumo_Energetico = ? WHERE ID_Maquina = ?", ( maquina.Tipo, maquina.Capacidad, maquina.Consumo_Energetico, maquina.ID_Maquina, ), ) conn.commit() return {"message": "Maquina actualizada"} except Exception as e: print(e) return [] # search by tipo de maquina @router.get("/{tipo}") def get_maquina(tipo: str): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM maquinas WHERE Tipo = ?", (tipo,)) maquina = cursor.fetchall() return maquina except Exception as e: print(e) return []