from __future__ import annotations from datetime import datetime from fastapi import APIRouter from pydantic import BaseModel, Field from app.db import get_pool from app.services.scheduler import reschedule router = APIRouter(tags=["schedule"]) class ScheduleOut(BaseModel): enabled: bool interval_days: int start_hour: int updated_at: datetime class ScheduleIn(BaseModel): enabled: bool interval_days: int = Field(ge=1) start_hour: int = Field(ge=0, le=23) @router.get("/schedule", response_model=ScheduleOut) async def get_schedule() -> ScheduleOut: pool = get_pool() async with pool.acquire() as conn: row = await conn.fetchrow("SELECT id, enabled, interval_days, start_hour, updated_at FROM scrape_schedule WHERE id = 1") return ScheduleOut(**dict(row)) @router.put("/schedule", response_model=ScheduleOut) async def update_schedule(body: ScheduleIn) -> ScheduleOut: pool = get_pool() async with pool.acquire() as conn: row = await conn.fetchrow( """UPDATE scrape_schedule SET enabled=$1, interval_days=$2, start_hour=$3, updated_at=NOW() WHERE id=1 RETURNING id, enabled, interval_days, start_hour, updated_at""", body.enabled, body.interval_days, body.start_hour, ) reschedule(body.interval_days, body.start_hour) return ScheduleOut(**dict(row))