from library.librerias import * from models.produccion import Produccion router = APIRouter( prefix="/produccion", tags=["Produccion"], responses={404: {"description": "No encontrado"}}, ) """CREATE TABLE produccion ( ID_Produccion INTEGER PRIMARY KEY AUTOINCREMENT, ID_Maquina INTEGER, Fecha DATE, Cantidad_Producida INTEGER, FOREIGN KEY (ID_Maquina) REFERENCES maquinas (ID_Maquina) ); """ @router.get("/") def get_produccion(): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute("SELECT * FROM produccion") produccion = cursor.fetchall() return produccion except Exception as e: print(e) return [] # @router.post("/") def post_produccion(produccion: Produccion): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "INSERT INTO produccion (ID_Maquina, Fecha, Cantidad_Producida) VALUES (?, ?, ?)", ( produccion.ID_Maquina, produccion.Fecha, produccion.Cantidad_Producida, ), ) conn.commit() return {"message": "Produccion creada"} except Exception as e: print(e) return [] # @router.put("/") def put_produccion(produccion: Produccion): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "UPDATE produccion SET ID_Maquina = ?, Fecha = ?, Cantidad_Producida = ? WHERE ID_Produccion = ?", ( produccion.ID_Maquina, produccion.Fecha, produccion.Cantidad_Producida, produccion.ID_Produccion, ), ) conn.commit() return {"message": "Produccion actualizada"} except Exception as e: print(e) return [] # @router.delete("/") def delete_produccion(ID_Produccion: int): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "DELETE FROM produccion WHERE ID_Produccion = ?", (ID_Produccion,), ) conn.commit() return {"message": "Produccion eliminada"} except Exception as e: print(e) return [] # @router.get("/maquina/{ID_Maquina}") def get_produccion_maquina(ID_Maquina: int): try: with DatabaseConnection().get_connection() as conn: cursor = conn.cursor() cursor.execute( "SELECT * FROM produccion WHERE ID_Maquina = ?", (ID_Maquina,) ) produccion = cursor.fetchall() return produccion except Exception as e: print(e) return [] @router.get("/fecha/{Fecha}") def get_produccion_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 produccion WHERE Fecha = ?", (Fecha,)) produccion = cursor.fetchall() return produccion except Exception as e: print(e) return []