feat: ✨ user routing
feat: ✨ contact
This commit is contained in:
parent
d0187b1b42
commit
aa679b5a05
18 changed files with 304 additions and 64 deletions
|
@ -5,21 +5,36 @@ use axum::{
|
|||
routing::{delete, get, patch, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{feature::role::Role, AppState};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct CreateRole {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UpdateRole {
|
||||
id: i64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/:name", post(create))
|
||||
.route("/", post(create))
|
||||
.route("/:id", get(read))
|
||||
.route("/:id/:name", patch(update))
|
||||
.route("/:id", delete(_delete))
|
||||
.route("/", patch(update))
|
||||
.route("/:id", delete(delete_))
|
||||
.route("/", get(read_all))
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
||||
async fn create(Path(name): Path<String>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
match Role::create(&name, &app_state.database_connection).await {
|
||||
async fn create(
|
||||
State(app_state): State<AppState>,
|
||||
Json(create_role): Json<CreateRole>,
|
||||
) -> impl IntoResponse {
|
||||
match Role::create(&create_role.name, &app_state.database_connection).await {
|
||||
Ok(role) => (StatusCode::CREATED, Json(serde_json::json!(role))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
|
@ -28,7 +43,7 @@ async fn create(Path(name): Path<String>, State(app_state): State<AppState>) ->
|
|||
}
|
||||
}
|
||||
|
||||
async fn read(Path(id): Path<i64>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
async fn read(State(app_state): State<AppState>, Path(id): Path<i64>) -> impl IntoResponse {
|
||||
match Role::read(&id, &app_state.database_connection).await {
|
||||
Ok(role) => (StatusCode::OK, Json(serde_json::json!(role))),
|
||||
Err(err_val) => (
|
||||
|
@ -39,10 +54,16 @@ async fn read(Path(id): Path<i64>, State(app_state): State<AppState>) -> impl In
|
|||
}
|
||||
|
||||
async fn update(
|
||||
Path((id, name)): Path<(i64, String)>,
|
||||
State(app_state): State<AppState>,
|
||||
Json(update_role): Json<UpdateRole>,
|
||||
) -> impl IntoResponse {
|
||||
match Role::update(&id, &name, &app_state.database_connection).await {
|
||||
match Role::update(
|
||||
&update_role.id,
|
||||
&update_role.name,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(role) => (StatusCode::ACCEPTED, Json(serde_json::json!(role))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
|
@ -51,7 +72,7 @@ async fn update(
|
|||
}
|
||||
}
|
||||
|
||||
async fn _delete(Path(id): Path<i64>, State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
async fn delete_(State(app_state): State<AppState>, Path(id): Path<i64>) -> impl IntoResponse {
|
||||
match Role::delete(&id, &app_state.database_connection).await {
|
||||
Ok(role) => (StatusCode::NO_CONTENT, Json(serde_json::json!(role))),
|
||||
Err(err_val) => (
|
||||
|
|
113
src/routing/user.rs
Normal file
113
src/routing/user.rs
Normal file
|
@ -0,0 +1,113 @@
|
|||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{delete, get, patch, post},
|
||||
Json, Router,
|
||||
};
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{feature::user::User, AppState};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct CreateUser {
|
||||
name: String,
|
||||
surname: String,
|
||||
gender: bool,
|
||||
birth_date: NaiveDate,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UpdateUser {
|
||||
id: i64,
|
||||
name: String,
|
||||
surname: String,
|
||||
gender: bool,
|
||||
birth_date: NaiveDate,
|
||||
role_id: i64,
|
||||
}
|
||||
|
||||
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", post(create))
|
||||
.route("/:id", get(read))
|
||||
.route("/", patch(update))
|
||||
.route("/:id", delete(delete_))
|
||||
.route("/", get(read_all))
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
||||
async fn create(
|
||||
State(app_state): State<AppState>,
|
||||
Json(create_user): Json<CreateUser>,
|
||||
) -> impl IntoResponse {
|
||||
match User::create(
|
||||
&create_user.name,
|
||||
&create_user.surname,
|
||||
&create_user.gender,
|
||||
&create_user.birth_date,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(user) => (StatusCode::CREATED, Json(serde_json::json!(user))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read(State(app_state): State<AppState>, Path(id): Path<i64>) -> impl IntoResponse {
|
||||
match User::read(&id, &app_state.database_connection).await {
|
||||
Ok(user) => (StatusCode::OK, Json(serde_json::json!(user))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn update(
|
||||
State(app_state): State<AppState>,
|
||||
Json(update_user): Json<UpdateUser>,
|
||||
) -> impl IntoResponse {
|
||||
match User::update(
|
||||
&update_user.id,
|
||||
&update_user.name,
|
||||
&update_user.surname,
|
||||
&update_user.gender,
|
||||
&update_user.birth_date,
|
||||
&update_user.role_id,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(user) => (StatusCode::ACCEPTED, Json(serde_json::json!(user))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_(State(app_state): State<AppState>, Path(id): Path<i64>) -> impl IntoResponse {
|
||||
match User::delete(&id, &app_state.database_connection).await {
|
||||
Ok(user) => (StatusCode::NO_CONTENT, Json(serde_json::json!(user))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_all(State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
match User::read_all(&app_state.database_connection).await {
|
||||
Ok(users) => (StatusCode::OK, Json(serde_json::json!(users))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue