from library.librerias import * from models.registro_energetico import RegistroEnergetico router = APIRouter( prefix="/registro_energetico", tags=["Registro_energetico"], responses={404: {"description": "No encontrado"}}, ) """CREATE TABLE registro_energetico ( ID_Registro INTEGER PRIMARY KEY AUTOINCREMENT, ID_Maquina INTEGER, Fecha DATE, Consumo REAL, FOREIGN KEY (ID_Maquina) REFERENCES maquinas (ID_Maquina) ); """ @router.get("/") def get_registro_energetico(): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM registro_energetico") registro_energetico = cursor.fetchall() return registro_energetico except Exception as e: print(e) return [] # @router.post("/") def post_registro_energetico(registro_energetico: RegistroEnergetico): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "INSERT INTO registro_energetico (ID_Maquina, Fecha, Consumo) VALUES (?, ?, ?)", ( registro_energetico.ID_Maquina, registro_energetico.Fecha, registro_energetico.Consumo, ), ) conn.commit() return {"message": "Registro_energetico creado"} except Exception as e: print(e) return [] # @router.put("/") def put_registro_energetico(registro_energetico: RegistroEnergetico): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "UPDATE registro_energetico SET ID_Maquina = ?, Fecha = ?, Consumo = ? WHERE ID_Registro = ?", ( registro_energetico.ID_Maquina, registro_energetico.Fecha, registro_energetico.Consumo, registro_energetico.ID_Registro, ), ) conn.commit() return {"message": "Registro_energetico actualizado"} except Exception as e: print(e) return [] # search by fecha @router.get("/fecha") def get_registro_energetico_fecha(fecha: str): try: with DatabaseConnection().get_connection() as conn: fecha = datetime.strptime(fecha, "%Y-%m-%d").date() cursor = conn.cursor() cursor.execute( "SELECT * FROM registro_energetico WHERE Fecha = ?", (fecha,) ) registro_energetico = cursor.fetchall() return registro_energetico except Exception as e: print(e) return []