summaryrefslogtreecommitdiffstats
path: root/src/routes/mod.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-15 14:31:30 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-15 14:31:30 +0000
commit3aae8ec6da9c1a1a5dffb4e8cd2ffc2560e5b9f1 (patch)
tree5c394957d980165efc174f58c56af7a1d90a472a /src/routes/mod.rs
parentef8a0f433878b01cd247a009ae93d95b559d0abc (diff)
Add `JsonCreate` trait which is a Json with 201 status
Diffstat (limited to 'src/routes/mod.rs')
-rw-r--r--src/routes/mod.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 0de7291..c69f18e 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -3,8 +3,25 @@ pub mod model;
pub mod user;
use crate::errors::AppError;
-use axum::response::IntoResponse;
+use axum::{
+ http::StatusCode,
+ response::{IntoResponse, Response},
+ Json,
+};
+use serde::Serialize;
pub async fn page_404() -> impl IntoResponse {
AppError::NotFound("Route not found".to_string())
}
+
+/// Extension of `Json` which returns the CREATED status code
+pub struct JsonCreate<T>(pub T);
+
+impl<T> IntoResponse for JsonCreate<T>
+where
+ T: Serialize,
+{
+ fn into_response(self) -> Response {
+ (StatusCode::CREATED, Json(self.0)).into_response()
+ }
+}