From e6af784ca35601206bfec43b8551f6f27b87ca68 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sat, 28 Dec 2024 13:34:23 +0100 Subject: [PATCH] refactor: extract route/request handlers --- src/main.rs | 28 +++------------------------- src/routes.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 src/routes.rs diff --git a/src/main.rs b/src/main.rs index c1e9987..5e990f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,10 @@ use axum::body::Body; -use axum::extract::Path; use axum::http::header::AUTHORIZATION; use axum::http::{Request, StatusCode}; use axum::middleware::{from_fn, Next}; use axum::response::{IntoResponse, Response}; use axum::routing::{delete, post}; -use axum::{Extension, Json, Router}; -use bwhc_dto::MtbFile; +use axum::{Extension, Router}; use clap::Parser; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; @@ -14,11 +12,13 @@ use serde::{Deserialize, Serialize}; use tower_http::trace::TraceLayer; use crate::cli::Cli; +use crate::routes::{handle_delete, handle_post}; use crate::sender::MtbFileSender; use crate::AppResponse::{Accepted, InternalServerError, Unauthorized}; mod auth; mod cli; +mod routes; mod sender; #[derive(Serialize, Deserialize)] @@ -94,28 +94,6 @@ async fn check_basic_auth(request: Request, next: Next) -> Response { Unauthorized.into_response() } -async fn handle_delete( - Path(patient_id): Path, - Extension(sender): Extension, -) -> Response { - let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id); - - match sender.send(delete_mtb_file).await { - Ok(request_id) => Accepted(&request_id).into_response(), - _ => InternalServerError.into_response(), - } -} - -async fn handle_post( - Extension(sender): Extension, - Json(mtb_file): Json, -) -> Response { - match sender.send(mtb_file).await { - Ok(request_id) => Accepted(&request_id).into_response(), - _ => InternalServerError.into_response(), - } -} - #[cfg(test)] mod tests { use axum::http::StatusCode; diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..8d80869 --- /dev/null +++ b/src/routes.rs @@ -0,0 +1,28 @@ +use crate::sender::MtbFileSender; +use crate::AppResponse::{Accepted, InternalServerError}; +use axum::extract::Path; +use axum::response::{IntoResponse, Response}; +use axum::{Extension, Json}; +use bwhc_dto::MtbFile; + +pub async fn handle_delete( + Path(patient_id): Path, + Extension(sender): Extension, +) -> Response { + let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id); + + match sender.send(delete_mtb_file).await { + Ok(request_id) => Accepted(&request_id).into_response(), + _ => InternalServerError.into_response(), + } +} + +pub async fn handle_post( + Extension(sender): Extension, + Json(mtb_file): Json, +) -> Response { + match sender.send(mtb_file).await { + Ok(request_id) => Accepted(&request_id).into_response(), + _ => InternalServerError.into_response(), + } +}