get endpoint works
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
use axum::{Json, debug_handler, extract::Query, http::StatusCode, response::IntoResponse};
|
||||
use chrono::{DateTime, Local};
|
||||
use axum::{Json, debug_handler, extract::{Query, State}, http::StatusCode, response::IntoResponse};
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::{IntoParams, IntoResponses, ToSchema};
|
||||
|
||||
use crate::{AppState, types::Alarm};
|
||||
|
||||
#[derive(ToSchema, Serialize)]
|
||||
pub struct OkResponse {
|
||||
name: String,
|
||||
enabled: bool,
|
||||
time: DateTime<Local>,
|
||||
time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(IntoResponses)]
|
||||
@@ -34,16 +36,26 @@ pub struct RequestQuery {
|
||||
enabled: Option<bool>
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "", responses(Responses), params(RequestQuery))]
|
||||
#[debug_handler]
|
||||
pub async fn get_handler(Query(RequestQuery { enabled }): Query<RequestQuery>) -> Responses {
|
||||
match enabled {
|
||||
Some(enabled) => {
|
||||
let alarms = crate::db::alarm::get_alarms(enabled).await;
|
||||
todo!()
|
||||
},
|
||||
None => {
|
||||
todo!()
|
||||
impl From<Alarm> for OkResponse {
|
||||
fn from(value: Alarm) -> Self {
|
||||
Self {
|
||||
name: value.time.to_string(),
|
||||
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<AppState>, Query(RequestQuery { enabled }): Query<RequestQuery>) -> 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user