Added functionality to delete variant.
This commit is contained in:
+23
-3
@@ -5,6 +5,7 @@ use axum::{
|
||||
Json, Router,
|
||||
};
|
||||
use axum::extract::{Path, State};
|
||||
use axum::routing::delete;
|
||||
use dotenv::dotenv;
|
||||
use env_logger::fmt::style::Reset;
|
||||
use http::{header, Method};
|
||||
@@ -15,7 +16,7 @@ use tower_http::cors::{AllowOrigin, Any, CorsLayer};
|
||||
use tracing::info;
|
||||
use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, ACCESS_CONTROL_ALLOW_CREDENTIALS};
|
||||
use common::models::article;
|
||||
use common::models::variant::{insert_variant, update_variant, Variant};
|
||||
use common::models::variant::{delete_variant, insert_variant, update_variant, Variant};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -39,7 +40,7 @@ async fn main() {
|
||||
sqlx::migrate!().run(&pool).await.expect("Migrations failed");
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods([Method::GET, Method::POST])
|
||||
.allow_methods([Method::GET, Method::POST, Method::DELETE])
|
||||
.allow_headers([ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE])
|
||||
.allow_origin(AllowOrigin::any());
|
||||
|
||||
@@ -50,6 +51,7 @@ async fn main() {
|
||||
.route("/article/{id}", get(get_article))
|
||||
.route("/article/{id}/variant", post(add_variant))
|
||||
.route("/variant/{id}", post(update_variant_route))
|
||||
.route("/variant/{id}", delete(delete_variant_route))
|
||||
.with_state(pool)
|
||||
.layer(cors);
|
||||
|
||||
@@ -187,4 +189,22 @@ async fn update_variant_route(
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_variant_route(
|
||||
State(pool): State<PgPool>,
|
||||
Path(variant_id): Path<i64>,
|
||||
) -> StatusCode {
|
||||
info!("Update variant");
|
||||
|
||||
let delete_variant_result = delete_variant(variant_id, pool).await;
|
||||
|
||||
match delete_variant_result {
|
||||
Some(_) => {
|
||||
StatusCode::OK
|
||||
}
|
||||
None => {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user