From b668848e63eefe862e06f00a4d2ca10dc353802c Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Wed, 31 Jan 2024 09:11:44 +0100 Subject: [PATCH] Added a migration to update existing nutritionnal values --- .../Bo/Products/StoreProductRequest.php | 2 +- database/factories/ProductFactory.php | 2 +- ..._01_31_085105_update_nutrionnal_values.php | 110 ++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2024_01_31_085105_update_nutrionnal_values.php diff --git a/app/Http/Requests/Bo/Products/StoreProductRequest.php b/app/Http/Requests/Bo/Products/StoreProductRequest.php index 82909c9..a52701d 100644 --- a/app/Http/Requests/Bo/Products/StoreProductRequest.php +++ b/app/Http/Requests/Bo/Products/StoreProductRequest.php @@ -101,7 +101,7 @@ class StoreProductRequest extends FormRequest "value" => $this->nutritionnal_values['fat'], ], "saturated-fat" => [ - "label" => "ACIDES GRAS SATURÉS (g)", + "label" => "ACIDE GRAS SATURÉ (g)", "value" => $this->nutritionnal_values['saturated_fat'], ], "carbohydrate" => [ diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index 3a19831..0bfd526 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -32,7 +32,7 @@ class ProductFactory extends Factory 'value' => fake()->randomFloat(2, 0, 70) ], 'saturated-fat' => [ - 'label' => 'ACIDES GRAS SATURÉS (g)', + 'label' => 'ACIDE GRAS SATURÉ (g)', 'value' => fake()->randomFloat(2, 0, 70) ], 'carbohydrate' => [ diff --git a/database/migrations/2024_01_31_085105_update_nutrionnal_values.php b/database/migrations/2024_01_31_085105_update_nutrionnal_values.php new file mode 100644 index 0000000..a2ae02c --- /dev/null +++ b/database/migrations/2024_01_31_085105_update_nutrionnal_values.php @@ -0,0 +1,110 @@ +each(function (Product $product) { + $oldValues = json_decode($product->nutritionnal_values, true); + + $product->nutritionnal_values = [ + 'energyKj' => [ + 'label' => 'ÉNERGIE (kj)', + 'value' => $oldValues['energyKj']['value'] + ], + 'energyKcal' => [ + 'label' => 'ÉNERGIE (kcal)', + 'value' => $oldValues['energyKcal']['value'] + ], + 'fat' => [ + 'label' => 'MATIÈRES GRASSES (g)', + 'value' => $oldValues['fat']['value'] + ], + 'saturated-fat' => [ + 'label' => 'ACIDE GRAS SATURÉ (g)', + 'value' => $oldValues['saturated-fat']['value'] + ], + 'carbohydrate' => [ + 'label' => 'GLUCIDES (g)', + 'value' => $oldValues['carbohydrate']['value'] + ], + 'sugar' => [ + 'label' => 'SUCRES (g)', + 'value' => $oldValues['sugar']['value'] + ], + 'proteins' => [ + 'label' => 'PROTÉINES (g)', + 'value' => $oldValues['proteins']['value'] + ], + 'salt' => [ + 'label' => 'SEL (g)', + 'value' => $oldValues['salt']['value'] + ], + ]; + $product->save(); + }); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + DB::transaction(function () { + Product::all()->each(function (Product $product) { + $oldValues = json_decode($product->nutritionnal_values, true); + + $product->nutritionnal_values = [ + 'energyKj' => [ + 'label' => 'Energie en Kj', + 'value' => $oldValues['energyKj']['value'] + ], + 'energyKcal' => [ + 'label' => 'Energie en kcal', + 'value' => $oldValues['energyKcal']['value'] + ], + 'fat' => [ + 'label' => 'Matières grasses', + 'value' => $oldValues['fat']['value'] + ], + 'saturated-fat' => [ + 'label' => 'dont acides gras saturés', + 'value' => $oldValues['saturated-fat']['value'] + ], + 'carbohydrate' => [ + 'label' => 'Glucides', + 'value' => $oldValues['carbohydrate']['value'] + ], + 'sugar' => [ + 'label' => 'dont sucres', + 'value' => $oldValues['sugar']['value'] + ], + 'proteins' => [ + 'label' => 'Protéines', + 'value' => $oldValues['proteins']['value'] + ], + 'salt' => [ + 'label' => 'Sel', + 'value' => $oldValues['salt']['value'] + ], + ]; + $product->save(); + }); + }); + } +};