diff --git a/apps/app_api/src/api.rs b/apps/app_api/src/api.rs index 97c8167..3402e84 100644 --- a/apps/app_api/src/api.rs +++ b/apps/app_api/src/api.rs @@ -1,5 +1,6 @@ use crate::state::AppState; -use axum::Router; +use axum::{Json, Router, http::StatusCode, response::IntoResponse}; +use serde::Serialize; use std::sync::Arc; pub mod dictionary; @@ -12,3 +13,32 @@ pub fn routes() -> Router> { .nest("/api", dictionary::routes()) .nest("/api", major_pl::routes()) } + +// --- Error Response --- + +#[derive(Debug, Serialize)] +pub struct ErrorResponse { + pub error: String, +} + +impl IntoResponse for ErrorResponse { + fn into_response(self) -> axum::response::Response { + (StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response() + } +} + +impl From for ErrorResponse { + fn from(err: anyhow::Error) -> Self { + Self { + error: err.to_string(), + } + } +} + +impl From for ErrorResponse { + fn from(err: applib::RepositoryError) -> Self { + Self { + error: err.to_string(), + } + } +} diff --git a/apps/app_api/src/api/dictionary.rs b/apps/app_api/src/api/dictionary.rs index ae7afcb..dc3c067 100644 --- a/apps/app_api/src/api/dictionary.rs +++ b/apps/app_api/src/api/dictionary.rs @@ -1,7 +1,8 @@ -use axum::{Json, Router, extract::State, http::StatusCode, response::IntoResponse, routing::get}; +use axum::{Json, Router, extract::State, routing::get}; use serde::Serialize; use std::sync::Arc; +use super::ErrorResponse; use crate::state::AppState; // --- DTOs --- @@ -17,33 +18,6 @@ pub struct DictListEntryResponse { pub entry_count: u64, } -#[derive(Debug, Serialize)] -pub struct ErrorResponse { - pub error: String, -} - -impl IntoResponse for ErrorResponse { - fn into_response(self) -> axum::response::Response { - (StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response() - } -} - -impl From for ErrorResponse { - fn from(err: anyhow::Error) -> Self { - Self { - error: err.to_string(), - } - } -} - -impl From for ErrorResponse { - fn from(err: applib::RepositoryError) -> Self { - Self { - error: err.to_string(), - } - } -} - // --- Handlers --- pub async fn list_dicts_handler( diff --git a/apps/app_api/src/api/health.rs b/apps/app_api/src/api/health.rs index d4b9bc1..99834d5 100644 --- a/apps/app_api/src/api/health.rs +++ b/apps/app_api/src/api/health.rs @@ -1,8 +1,6 @@ use axum::{ Json, Router, extract::State, - http::StatusCode, - response::IntoResponse, routing::{get, post}, }; use chrono::Utc; @@ -10,6 +8,7 @@ use serde::Serialize; use serde_json::Value; use std::sync::Arc; +use super::ErrorResponse; use crate::state::AppState; // --- DTOs --- @@ -26,25 +25,6 @@ pub struct VersionResponse { pub version: String, } -#[derive(Debug, Serialize)] -pub struct ErrorResponse { - pub error: String, -} - -impl IntoResponse for ErrorResponse { - fn into_response(self) -> axum::response::Response { - (StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response() - } -} - -impl From for ErrorResponse { - fn from(err: E) -> Self { - Self { - error: err.to_string(), - } - } -} - // --- Handlers --- pub async fn echo_handler( diff --git a/apps/app_api/src/api/major_pl.rs b/apps/app_api/src/api/major_pl.rs index c4caad0..c93917b 100644 --- a/apps/app_api/src/api/major_pl.rs +++ b/apps/app_api/src/api/major_pl.rs @@ -1,8 +1,7 @@ +use super::ErrorResponse; use axum::{ Json, Router, extract::{Path, Query, State}, - http::StatusCode, - response::IntoResponse, routing::get, }; use serde::{Deserialize, Serialize}; @@ -16,9 +15,7 @@ pub struct EncodeQuery { } #[derive(Debug, Deserialize)] -pub struct DecodeQuery { - pub dict: Option, -} +pub struct DecodeQuery {} #[derive(Debug, Serialize)] pub struct EncodeResponse { @@ -39,25 +36,6 @@ pub struct DecodeResponse { pub result: String, } -#[derive(Debug, Serialize)] -pub struct ErrorResponse { - pub error: String, -} - -impl IntoResponse for ErrorResponse { - fn into_response(self) -> axum::response::Response { - (StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response() - } -} - -impl From for ErrorResponse { - fn from(err: anyhow::Error) -> Self { - Self { - error: err.to_string(), - } - } -} - pub async fn encode_handler( State(state): State>, Path(input): Path, diff --git a/apps/app_api/src/container.rs b/apps/app_api/src/container.rs index 6f6be09..37b5972 100644 --- a/apps/app_api/src/container.rs +++ b/apps/app_api/src/container.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use applib::DictImporter; use applib::DictRepository; use applib::SqliteDictRepository; use applib::SystemDecoder; @@ -15,11 +14,6 @@ impl Container { Ok(Self) } - pub async fn create_dict_importer(&self, dict_name: &str) -> anyhow::Result { - let repo = self.create_dict_repo(dict_name).await?; - Ok(DictImporter::new(repo)) - } - pub async fn create_dict_repo( &self, dict_name: &str,