Added variants base functionality.

This commit is contained in:
2026-01-22 20:13:38 +01:00
parent a87500fb24
commit 95fa29a6b3
10 changed files with 407 additions and 23 deletions
+30 -12
View File
@@ -15,6 +15,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, Variant};
#[tokio::main]
async fn main() {
@@ -28,11 +29,6 @@ async fn main() {
// initialize tracing
tracing_subscriber::fmt().init();
/*
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
*/
let pool = PgPoolOptions::new()
.max_connections(50)
@@ -49,11 +45,10 @@ async fn main() {
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/", get(root))
.route("/articles", get(get_articles))
.route("/article", post(create_article))
.route("/article/{id}", get(get_article))
.route("/article/{id}/variant", post(add_variant))
.with_state(pool)
.layer(cors);
@@ -63,11 +58,6 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
// basic handler that responds with a static string
async fn root() -> &'static str {
"Hello, World!"
}
#[tracing::instrument(ret)]
async fn get_articles(
State(pool): State<PgPool>,
@@ -136,3 +126,31 @@ async fn create_article(
(StatusCode::CREATED, Json(article))
}
async fn add_variant(
State(pool): State<PgPool>,
Path(article_id): Path<i64>,
Json(payload): Json<Variant>,
) -> (StatusCode, Json<Variant>) {
info!("Add variant");
let created_variant = insert_variant(article_id, &payload, pool).await;
match created_variant {
Some(variant) => {
(StatusCode::OK, Json(variant))
}
None => {
let variant = Variant {
id: 0,
weight: "".to_string(),
ean: None,
name: None,
description: None,
ingredients: None,
};
(StatusCode::INTERNAL_SERVER_ERROR, Json(variant))
}
}
}