switched back to axum wip
This commit is contained in:
@@ -1,56 +1,40 @@
|
||||
use std::{fmt::Display};
|
||||
|
||||
use actix_web::{ResponseError, web::{Data, Json}};
|
||||
use apistos::{ApiComponent, ApiErrorComponent, actix::CreatedJson};
|
||||
use axum::Json;
|
||||
use axum::http::StatusCode;
|
||||
use axum::debug_handler;
|
||||
use axum::response::IntoResponse;
|
||||
use chrono::{DateTime, Local};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::Deserialize;
|
||||
use utoipa::{IntoResponses, ToSchema};
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema, ApiComponent)]
|
||||
#[derive(Debug, Deserialize, JsonSchema, ToSchema)]
|
||||
pub struct RequestBody {
|
||||
time: DateTime<Local>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema, ApiComponent)]
|
||||
pub struct SuccessResponse {
|
||||
pub time: DateTime<Local>,
|
||||
pub enabled: bool
|
||||
#[derive(IntoResponses)]
|
||||
pub enum Responses {
|
||||
#[response(status = 200)]
|
||||
Ok
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, ApiErrorComponent)]
|
||||
#[openapi_error(status(code = 500, description = "The alarm could not be created"))]
|
||||
pub enum ErrorResponse {
|
||||
InternalError(String)
|
||||
}
|
||||
|
||||
impl Display for ErrorResponse {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
impl IntoResponse for Responses {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self {
|
||||
ErrorResponse::InternalError(e) => write!(f, "Internal error: {}", e),
|
||||
Responses::Ok => (StatusCode::OK).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl ResponseError for ErrorResponse {
|
||||
fn status_code(&self) -> actix_web::http::StatusCode {
|
||||
match self {
|
||||
ErrorResponse::InternalError(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[apistos::api_operation(
|
||||
summary = "Add new alarm",
|
||||
description = r###"Creates new Alarm"###,
|
||||
error_code= 500,
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "",
|
||||
responses(
|
||||
Responses
|
||||
)
|
||||
)]
|
||||
pub async fn post(data: Data<AppState>, body: Json<RequestBody>) -> Result<CreatedJson<SuccessResponse>, ErrorResponse> {
|
||||
let result = data.scheduler.add_alarm(body.time);
|
||||
|
||||
match result {
|
||||
Ok(alarm) => Ok(CreatedJson(SuccessResponse { time: alarm.time, enabled: alarm.enabled })),
|
||||
Err(e) => Err(ErrorResponse::InternalError(e)),
|
||||
}
|
||||
#[debug_handler]
|
||||
pub async fn post_handler(Json(body): Json<RequestBody>) -> Responses {
|
||||
todo!()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user