feat: ✨ interaction routing
This commit is contained in:
parent
0d9cef79f3
commit
0f09dd6a82
4 changed files with 154 additions and 3 deletions
|
@ -20,15 +20,15 @@ pub async fn create(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read(
|
pub async fn read(
|
||||||
name: &String,
|
id: &i64,
|
||||||
database_connection: &Pool<Postgres>,
|
database_connection: &Pool<Postgres>,
|
||||||
) -> Result<Interaction, sqlx::Error> {
|
) -> Result<Interaction, sqlx::Error> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Interaction,
|
Interaction,
|
||||||
r#"
|
r#"
|
||||||
SELECT * FROM "interaction" WHERE "name" = $1
|
SELECT * FROM "interaction" WHERE "id" = $1
|
||||||
"#,
|
"#,
|
||||||
name
|
id
|
||||||
)
|
)
|
||||||
.fetch_one(database_connection)
|
.fetch_one(database_connection)
|
||||||
.await
|
.await
|
||||||
|
@ -67,3 +67,16 @@ pub async fn delete(
|
||||||
.fetch_one(database_connection)
|
.fetch_one(database_connection)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn read_all(
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Vec<Interaction>, sqlx::Error> {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Interaction,
|
||||||
|
r#"
|
||||||
|
SELECT * FROM "interaction"
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(database_connection)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,47 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
|
use crate::database::interaction;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Interaction {
|
pub struct Interaction {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Interaction {
|
||||||
|
pub async fn create(
|
||||||
|
name: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Interaction, sqlx::Error> {
|
||||||
|
interaction::create(name, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read(
|
||||||
|
id: &i64,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Interaction, sqlx::Error> {
|
||||||
|
interaction::read(id, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update(
|
||||||
|
id: &i64,
|
||||||
|
name: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Interaction, sqlx::Error> {
|
||||||
|
interaction::update(id, name, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(
|
||||||
|
id: &i64,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Interaction, sqlx::Error> {
|
||||||
|
interaction::delete(id, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_all(
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Vec<Interaction>, sqlx::Error> {
|
||||||
|
interaction::read_all(database_connection).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod comment;
|
pub mod comment;
|
||||||
|
pub mod interaction;
|
||||||
pub mod post;
|
pub mod post;
|
||||||
pub mod role;
|
pub mod role;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
@ -27,6 +28,10 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
|
||||||
"/comments",
|
"/comments",
|
||||||
comment::route(axum::extract::State(app_state.clone())),
|
comment::route(axum::extract::State(app_state.clone())),
|
||||||
)
|
)
|
||||||
|
.nest(
|
||||||
|
"/interactions",
|
||||||
|
interaction::route(axum::extract::State(app_state.clone())),
|
||||||
|
)
|
||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
.with_state(app_state)
|
.with_state(app_state)
|
||||||
}
|
}
|
||||||
|
|
93
src/routing/interaction.rs
Normal file
93
src/routing/interaction.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::interaction::Interaction, AppState};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct CreateInteraction {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct UpdateInteraction {
|
||||||
|
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_interaction): Json<CreateInteraction>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Interaction::create(&create_interaction.name, &app_state.database_connection).await {
|
||||||
|
Ok(interaction) => (StatusCode::CREATED, Json(serde_json::json!(interaction))),
|
||||||
|
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 Interaction::read(&id, &app_state.database_connection).await {
|
||||||
|
Ok(interaction) => (StatusCode::OK, Json(serde_json::json!(interaction))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Json(update_interaction): Json<UpdateInteraction>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Interaction::update(
|
||||||
|
&update_interaction.id,
|
||||||
|
&update_interaction.name,
|
||||||
|
&app_state.database_connection,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(interaction) => (StatusCode::ACCEPTED, Json(serde_json::json!(interaction))),
|
||||||
|
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 Interaction::delete(&id, &app_state.database_connection).await {
|
||||||
|
Ok(interaction) => (StatusCode::NO_CONTENT, Json(serde_json::json!(interaction))),
|
||||||
|
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 Interaction::read_all(&app_state.database_connection).await {
|
||||||
|
Ok(interactions) => (StatusCode::OK, Json(serde_json::json!(interactions))),
|
||||||
|
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