Skip to content

Commit

Permalink
refactor(backend): move error and split router paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Frixxie committed Nov 13, 2024
1 parent b3ed8e6 commit 672e015
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
1 change: 0 additions & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod category;
mod error;
mod file;
mod item;
mod location;
Expand Down
7 changes: 3 additions & 4 deletions backend/src/router/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use axum::{
use sqlx::PgPool;
use tracing::instrument;

use crate::{
category::{Category, NewCategory},
error::HandlerError,
};
use crate::category::{Category, NewCategory};

use super::error::HandlerError;

#[instrument]
pub async fn get_all_categories(
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion backend/src/router/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use axum::{
use sqlx::PgPool;
use tracing::instrument;

use crate::{error::HandlerError, file::FileInfo};
use crate::file::FileInfo;

use super::error::HandlerError;

#[instrument]
pub async fn get_file_by_id(
Expand Down
7 changes: 3 additions & 4 deletions backend/src/router/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use axum::{
use sqlx::PgPool;
use tracing::instrument;

use crate::{
error::HandlerError,
item::{Item, NewItem},
};
use crate::item::{Item, NewItem};

use super::error::HandlerError;

#[instrument]
pub async fn get_all_items(
Expand Down
7 changes: 3 additions & 4 deletions backend/src/router/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use axum::{
use sqlx::PgPool;
use tracing::instrument;

use crate::{
error::HandlerError,
location::{Location, NewLocation},
};
use crate::location::{Location, NewLocation};

use super::error::HandlerError;

#[instrument]
pub async fn get_all_locations(
Expand Down
56 changes: 36 additions & 20 deletions backend/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing::info;

mod category;
mod common;
mod error;
mod file;
mod item;
mod location;
Expand Down Expand Up @@ -52,27 +53,42 @@ async fn profile_endpoint(request: Request, next: Next) -> Response {
}

pub fn create_router(connection: PgPool, metrics_handler: PrometheusHandle) -> Router {
let item_router = Router::new()
.route("/items", get(get_all_items))
.route("/items/:id", get(get_item_by_id))
.route("/items", post(add_item))
.route("/items/:id", delete(delete_item_by_id))
.route("/items", put(update_item))
.with_state(connection.clone());

let location_router = Router::new()
.route("/locations", get(get_all_locations))
.route("/locations/:id", get(get_location_by_id))
.route("/locations", post(add_location))
.route("/locations/:id", delete(delete_location_by_id))
.route("/locations", put(update_location))
.with_state(connection.clone());

let category_router = Router::new()
.route("/categories", get(get_all_categories))
.route("/categories/:id", get(get_category_by_id))
.route("/categories", post(add_category))
.route("/categories/:id", delete(delete_category_by_id))
.route("/categories", put(update_category))
.with_state(connection.clone());

let file_router = Router::new()
.route("/files/:id", get(get_file_by_id))
.route("/files", post(add_file))
.route("/files/:id", delete(delete_file_by_id))
.route("/file_infos", get(get_all_files))
.with_state(connection);

Router::new()
.route("/api/items", get(get_all_items))
.route("/api/items/:id", get(get_item_by_id))
.route("/api/items", post(add_item))
.route("/api/items/:id", delete(delete_item_by_id))
.route("/api/items", put(update_item))
.route("/api/locations", get(get_all_locations))
.route("/api/locations/:id", get(get_location_by_id))
.route("/api/locations", post(add_location))
.route("/api/locations/:id", delete(delete_location_by_id))
.route("/api/locations", put(update_location))
.route("/api/categories", get(get_all_categories))
.route("/api/categories/:id", get(get_category_by_id))
.route("/api/categories", post(add_category))
.route("/api/categories/:id", delete(delete_category_by_id))
.route("/api/categories", put(update_category))
.route("/api/files/:id", get(get_file_by_id))
.route("/api/files", post(add_file))
.route("/api/files/:id", delete(delete_file_by_id))
.route("/api/file_infos", get(get_all_files))
.with_state(connection)
.nest("/api", item_router)
.nest("/api", location_router)
.nest("/api", category_router)
.nest("/api", file_router)
.route("/metrics", get(metrics))
.with_state(metrics_handler)
.route("/status/health", get(status))
Expand Down

0 comments on commit 672e015

Please sign in to comment.