Functionality to create, update and delete label.
This commit is contained in:
@@ -16,6 +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::label::{delete_label, insert_label, load_labels, update_label, Label};
|
||||
use common::models::unit::Unit;
|
||||
use common::models::variant::{delete_variant, insert_variant, update_variant, Variant};
|
||||
|
||||
@@ -55,6 +56,10 @@ async fn main() {
|
||||
.route("/article/{id}/variant", post(add_variant))
|
||||
.route("/variant/{id}", post(update_variant_route))
|
||||
.route("/variant/{id}", delete(delete_variant_route))
|
||||
.route("/label", get(get_labels_route))
|
||||
.route("/label", post(add_label_route))
|
||||
.route("/label/{id}", post(update_label_route))
|
||||
.route("/label/{id}", delete(delete_label_route))
|
||||
.with_state(pool)
|
||||
.layer(cors);
|
||||
|
||||
@@ -268,3 +273,82 @@ async fn delete_variant_route(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret)]
|
||||
async fn get_labels_route(
|
||||
State(pool): State<PgPool>,
|
||||
) -> (StatusCode, Json<Vec<Label>>) {
|
||||
info!("Get all labels");
|
||||
|
||||
match load_labels(pool).await {
|
||||
Some(labels) => {
|
||||
(StatusCode::OK, Json(labels))
|
||||
}
|
||||
None => {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(vec![]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret)]
|
||||
async fn add_label_route(
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<Label>,
|
||||
) -> (StatusCode, Json<Label>) {
|
||||
info!("Add label");
|
||||
|
||||
match insert_label(&payload, pool).await {
|
||||
Some(variant) => {
|
||||
(StatusCode::OK, Json(variant))
|
||||
}
|
||||
None => {
|
||||
let label = Label {
|
||||
id: 0,
|
||||
name: "".to_string(),
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(label))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret)]
|
||||
async fn update_label_route(
|
||||
State(pool): State<PgPool>,
|
||||
Path(label_id): Path<i64>,
|
||||
Json(payload): Json<Label>,
|
||||
) -> (StatusCode, Json<Label>) {
|
||||
info!("Update label");
|
||||
|
||||
match update_label(label_id, &payload, pool).await {
|
||||
Some(variant) => {
|
||||
(StatusCode::OK, Json(variant))
|
||||
}
|
||||
None => {
|
||||
let label = Label {
|
||||
id: 0,
|
||||
name: "".to_string(),
|
||||
};
|
||||
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(label))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_label_route(
|
||||
State(pool): State<PgPool>,
|
||||
Path(label_id): Path<i64>,
|
||||
) -> StatusCode {
|
||||
info!("Delete label");
|
||||
|
||||
let delete_variant_result = delete_label(label_id, pool).await;
|
||||
|
||||
match delete_variant_result {
|
||||
Some(_) => {
|
||||
StatusCode::OK
|
||||
}
|
||||
None => {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user