From cf874a7b8db60e65fde249fb67681e588a279eb5 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Mon, 1 Dec 2025 10:25:33 +0100 Subject: [PATCH] Refactor map_row_to_production_plan in DirectorWorker: Simplified the parsing of database fields by introducing local variables for required and optional fields. This change enhances readability and maintains default values for optional fields, improving the robustness of production plan creation. --- src/worker/director.rs | 52 ++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/worker/director.rs b/src/worker/director.rs index 1b5dfa6..eac05a9 100644 --- a/src/worker/director.rs +++ b/src/worker/director.rs @@ -408,21 +408,45 @@ impl DirectorWorker { } fn map_row_to_production_plan(row: &Row) -> Option { + // Pflichtfelder: ohne diese können wir keinen sinnvollen Plan erstellen. + let falukant_user_id: i32 = row.get("falukant_user_id")?.parse().ok()?; + let certificate: i32 = row.get("certificate")?.parse().ok()?; + let branch_id: i32 = row.get("branch_id")?.parse().ok()?; + let product_id: i32 = row.get("product_id")?.parse().ok()?; + + // Optionale/abgeleitete Felder: hier sind wir tolerant und verwenden + // Default-Werte, falls NULL oder nicht parsbar. + let money: f64 = row + .get("money") + .and_then(|v| v.parse::().ok()) + .unwrap_or(0.0); + let stock_size: i32 = row + .get("stock_size") + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + let used_in_stock: i32 = row + .get("used_in_stock") + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + let running_productions: i32 = row + .get("running_productions") + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + let running_productions_quantity: i32 = row + .get("running_productions_quantity") + .and_then(|v| v.parse::().ok()) + .unwrap_or(0); + Some(ProductionPlan { - falukant_user_id: row.get("falukant_user_id")?.parse().ok()?, - // money kommt in der DB typischerweise als Dezimalzahl (z.B. "96284894.40"). - // Daher als f64 parsen; bei Parsefehlern den Plan verwerfen. - money: row.get("money")?.parse::().ok()?, - certificate: row.get("certificate")?.parse().ok()?, - branch_id: row.get("branch_id")?.parse().ok()?, - product_id: row.get("product_id")?.parse().ok()?, - stock_size: row.get("stock_size")?.parse().ok()?, - used_in_stock: row.get("used_in_stock")?.parse().ok()?, - running_productions: row.get("running_productions")?.parse().ok()?, - running_productions_quantity: row - .get("running_productions_quantity")? - .parse() - .ok()?, + falukant_user_id, + money, + certificate, + branch_id, + product_id, + stock_size, + used_in_stock, + running_productions, + running_productions_quantity, }) }