use std::i32; use axum::{Json, debug_handler, extract::{Query, State}, http::StatusCode, response::IntoResponse}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use utoipa::{IntoParams, IntoResponses, ToSchema}; use crate::{AppState, types::Alarm}; #[derive(ToSchema, Serialize)] pub struct OkResponse { id: i32, title: String, enabled: bool, time: DateTime, } #[derive(IntoResponses)] pub enum Responses { #[response(status = 200)] Ok(#[to_schema] Vec), #[response(status = 500)] DBError(String), } impl IntoResponse for Responses { fn into_response(self) -> axum::response::Response { match self { Responses::Ok(body) => (StatusCode::OK, Json(body)).into_response(), Responses::DBError(message) => { (StatusCode::INTERNAL_SERVER_ERROR, Json(message)).into_response() } } } } #[derive(IntoParams, Deserialize)] pub struct RequestQuery { enabled: Option } impl From for OkResponse { fn from(value: Alarm) -> Self { Self { id: value.id, title: value.title, enabled: value.enabled, time: value.time, } } } #[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))] #[debug_handler] pub async fn get_handler(State(AppState{ scheduler }): State, Query(RequestQuery { enabled }): Query) -> Responses { let result = scheduler.get_alarms(enabled).await; match result { Ok(alarms) => { Responses::Ok(alarms.into_iter().map(|alarm| alarm.into()).collect()) }, Err(error) => { Responses::DBError(error.to_string()) } } }