feat: ✨ contact routing
This commit is contained in:
parent
64bf22ba68
commit
a7d246d4f7
4 changed files with 149 additions and 0 deletions
|
@ -64,3 +64,14 @@ pub async fn delete(
|
|||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Contact>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Contact,
|
||||
r#"
|
||||
SELECT * FROM "contact"
|
||||
"#,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -1,7 +1,47 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::database::contact;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Contact {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Contact {
|
||||
pub async fn create(
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
contact::create(name, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
contact::read(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: &i64,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
contact::update(id, name, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
contact::delete(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read_all(
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<Contact>, sqlx::Error> {
|
||||
contact::read_all(database_connection).await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
pub mod comment;
|
||||
pub mod comment_interaction;
|
||||
pub mod contact;
|
||||
pub mod interaction;
|
||||
pub mod permission;
|
||||
pub mod post;
|
||||
|
@ -48,6 +49,10 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
|
|||
"/role_permissions",
|
||||
role_permission::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.nest(
|
||||
"/contacts",
|
||||
contact::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.layer(CorsLayer::permissive())
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
|
93
src/routing/contact.rs
Normal file
93
src/routing/contact.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{delete, get, patch, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{feature::contact::Contact, AppState};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct CreateContact {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UpdateContact {
|
||||
id: i64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
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_contact): Json<CreateContact>,
|
||||
) -> impl IntoResponse {
|
||||
match Contact::create(&create_contact.name, &app_state.database_connection).await {
|
||||
Ok(contact) => (StatusCode::CREATED, Json(serde_json::json!(contact))),
|
||||
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 Contact::read(&id, &app_state.database_connection).await {
|
||||
Ok(contact) => (StatusCode::OK, Json(serde_json::json!(contact))),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn update(
|
||||
State(app_state): State<AppState>,
|
||||
Json(update_contact): Json<UpdateContact>,
|
||||
) -> impl IntoResponse {
|
||||
match Contact::update(
|
||||
&update_contact.id,
|
||||
&update_contact.name,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(contact) => (StatusCode::ACCEPTED, Json(serde_json::json!(contact))),
|
||||
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 Contact::delete(&id, &app_state.database_connection).await {
|
||||
Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))),
|
||||
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 Contact::read_all(&app_state.database_connection).await {
|
||||
Ok(contacts) => (StatusCode::OK, Json(serde_json::json!(contacts))),
|
||||
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