Added supplier and updated dependencies.

This commit is contained in:
2026-07-22 19:26:57 +02:00
parent cddd9a3956
commit 581439150f
16 changed files with 3585 additions and 3068 deletions
+88
View File
@@ -18,6 +18,7 @@ use http::header::{AUTHORIZATION, ACCEPT, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_T
use common::models::article;
use common::models::label::{delete_label, insert_label, load_labels, update_label, Label};
use common::models::settings::{load_settings, update_settings, Settings};
use common::models::supplier::{delete_supplier, insert_supplier, load_suppliers, update_supplier, Supplier};
use common::models::unit::Unit;
use common::models::variant::{delete_variant, insert_variant, update_variant, Variant};
@@ -63,6 +64,10 @@ async fn main() {
.route("/label/{id}", delete(delete_label_route))
.route("/settings", get(get_settings_route))
.route("/settings", post(update_settings_route))
.route("/supplier", get(get_suppliers_route))
.route("/supplier", post(add_supplier_route))
.route("/supplier/{id}", post(update_supplier_route))
.route("/supplier/{id}", delete(delete_supplier_route))
.with_state(pool)
.layer(cors);
@@ -425,3 +430,86 @@ async fn update_settings_route(
}
}
}
#[tracing::instrument(ret)]
async fn get_suppliers_route(
State(pool): State<PgPool>,
) -> (StatusCode, Json<Vec<Supplier>>) {
info!("Get all suppliers");
match load_suppliers(pool).await {
Some(suppliers) => {
(StatusCode::OK, Json(suppliers))
}
None => {
(StatusCode::INTERNAL_SERVER_ERROR, Json(vec![]))
}
}
}
#[tracing::instrument(ret)]
async fn add_supplier_route(
State(pool): State<PgPool>,
Json(payload): Json<Supplier>,
) -> (StatusCode, Json<Supplier>) {
info!("Add supplier");
match insert_supplier(&payload, pool).await {
Some(supplier) => {
(StatusCode::OK, Json(supplier))
}
None => {
let supplier = Supplier {
id: 0,
topm_id: "".to_string(),
name: "".to_string(),
bio_info: None,
};
(StatusCode::INTERNAL_SERVER_ERROR, Json(supplier))
}
}
}
#[tracing::instrument(ret)]
async fn update_supplier_route(
State(pool): State<PgPool>,
Path(supplier_id): Path<i64>,
Json(payload): Json<Supplier>,
) -> (StatusCode, Json<Supplier>) {
info!("Update supplier");
match update_supplier(supplier_id, &payload, pool).await {
Some(supplier) => {
(StatusCode::OK, Json(supplier))
}
None => {
let supplier = Supplier {
id: 0,
topm_id: "".to_string(),
name: "".to_string(),
bio_info: None,
};
(StatusCode::INTERNAL_SERVER_ERROR, Json(supplier))
}
}
}
async fn delete_supplier_route(
State(pool): State<PgPool>,
Path(supplier_id): Path<i64>,
) -> StatusCode {
info!("Delete supplier");
let deleted_result = delete_supplier(supplier_id, pool).await;
match deleted_result {
Some(_) => {
StatusCode::OK
}
None => {
StatusCode::INTERNAL_SERVER_ERROR
}
}
}