same functionality now works with axum the same way it did with actix
This commit is contained in:
@@ -1,32 +1,51 @@
|
||||
use axum::Json;
|
||||
use axum::extract::State;
|
||||
use axum::{Json};
|
||||
use axum::http::StatusCode;
|
||||
use axum::debug_handler;
|
||||
use axum::response::IntoResponse;
|
||||
use chrono::{DateTime, Local};
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::{IntoResponses, ToSchema};
|
||||
|
||||
use crate::AppState;
|
||||
use crate::types::Alarm;
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema, ToSchema)]
|
||||
pub struct RequestBody {
|
||||
time: DateTime<Local>,
|
||||
}
|
||||
|
||||
#[derive(ToSchema, Serialize)]
|
||||
pub struct OkResponseBody {
|
||||
time: DateTime<Local>,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(IntoResponses)]
|
||||
pub enum Responses {
|
||||
#[response(status = 200)]
|
||||
Ok
|
||||
Ok(#[to_schema] OkResponseBody),
|
||||
#[response(status = 500)]
|
||||
Error(String)
|
||||
}
|
||||
|
||||
impl IntoResponse for Responses {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self {
|
||||
Responses::Ok => (StatusCode::OK).into_response(),
|
||||
Responses::Ok(body) => (StatusCode::OK, Json(body)).into_response(),
|
||||
Responses::Error(message) => (StatusCode::INTERNAL_SERVER_ERROR, Json(message)).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl From<Alarm> for OkResponseBody {
|
||||
fn from(value: Alarm) -> Self {
|
||||
OkResponseBody { time: value.time, enabled: value.enabled }
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "",
|
||||
@@ -35,6 +54,7 @@ impl IntoResponse for Responses {
|
||||
)
|
||||
)]
|
||||
#[debug_handler]
|
||||
pub async fn post_handler(Json(body): Json<RequestBody>) -> Responses {
|
||||
todo!()
|
||||
pub async fn post_handler(State(AppState { scheduler, ..}): State<AppState>, Json(body): Json<RequestBody>) -> Responses {
|
||||
let alarm = scheduler.add_alarm(body.time).await.unwrap();
|
||||
Responses::Ok(alarm.into())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user