From 20d38dc4a2b2e15d22679a0bdc8a5721c8775934 Mon Sep 17 00:00:00 2001 From: arieeefajar Date: Thu, 6 Feb 2025 20:43:27 +0700 Subject: [PATCH] fix(master-indicator): create store and delete func --- .../MasterData/IndicatorController.php | 73 ++++++--- app/Models/EvaluationDetail.php | 12 +- app/Models/Evalutaion.php | 4 +- app/Models/Indicator.php | 9 +- app/Models/Rule.php | 5 +- config/debugbar.php | 2 +- ...25_01_28_120615_create_indicator_table.php | 3 - ...> 2025_02_06_084905_create_rule_table.php} | 9 +- ..._02_06_085110_create_evaluation_table.php} | 8 +- ...85614_create_evaluation_details_table.php} | 5 +- database/seeders/IndicatorSeeder.php | 39 ++--- database/seeders/RuleSeeder.php | 67 ++------- .../customJs/master-data/indikator/index.js | 31 ++-- .../master-data/indikator/index.blade.php | 141 ++++++++++-------- routes/web.php | 4 +- 15 files changed, 189 insertions(+), 223 deletions(-) rename database/migrations/{2025_01_29_054112_create_rules_table.php => 2025_02_06_084905_create_rule_table.php} (69%) rename database/migrations/{2025_02_05_141957_create_evaluations_table.php => 2025_02_06_085110_create_evaluation_table.php} (69%) rename database/migrations/{2025_02_05_142241_create_evaluation_details_table.php => 2025_02_06_085614_create_evaluation_details_table.php} (89%) diff --git a/app/Http/Controllers/MasterData/IndicatorController.php b/app/Http/Controllers/MasterData/IndicatorController.php index b5bf99f..5d8dca7 100644 --- a/app/Http/Controllers/MasterData/IndicatorController.php +++ b/app/Http/Controllers/MasterData/IndicatorController.php @@ -15,34 +15,57 @@ public function index() return view('master-data.indikator.index', compact('indicators')); } + public function store(Request $request) + { + $customMessage = [ + "name.required" => "Nama wajib diisi", + "name.max" => "Nama maksimal 25 karakter", + "name.string" => "Nama harus berupa string", + + "description.required" => "Deskripsi wajib diisi", + "description.max" => "Deskripsi maksimal 50 karakter", + "description.string" => "Deskripsi harus berupa string", + ]; + + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|max:25', + 'description' => 'required|string|max:50', + ], $customMessage); + + if ($validator->fails()) { + toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000); + return redirect()->back()->withInput(); + } + + $indicator = new Indicator(); + $indicator->name = $request->name; + $indicator->description = $request->description; + + try { + $indicator->save(); + toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000); + return redirect()->back(); + } catch (\Throwable $th) { + toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000); + return redirect()->back(); + } + } + public function update(Request $request, $id) { $customMessage = [ "name.required" => "Nama wajib diisi", - "name.max" => "Nama maksimal 255 karakter", + "name.max" => "Nama maksimal 25 karakter", "name.string" => "Nama harus berupa string", "description.required" => "Deskripsi wajib diisi", - "description.max" => "Deskripsi maksimal 255 karakter", + "description.max" => "Deskripsi maksimal 50 karakter", "description.string" => "Deskripsi harus berupa string", - - "ideal_min.required" => "Nilai ideal minimum wajib diisi", - "ideal_min.numeric" => "Nilai ideal minimum harus berupa angka", - - "ideal_max.required" => "Nilai ideal maksimum wajib diisi", - "ideal_max.numeric" => "Nilai ideal maksimum harus berupa angka", - - "unit.required" => "Unit wajib diisi", - "unit.max" => "Unit maksimal 255 karakter", - "unit.string" => "Unit harus berupa string", ]; $validator = Validator::make($request->all(), [ - 'name' => 'required|string|max:255', - 'description' => 'required|string|max:255', - 'ideal_min' => 'required|numeric', - 'ideal_max' => 'required|numeric', - 'unit' => 'required|string|max:255', + 'name' => 'required|string|max:25', + 'description' => 'required|string|max:50', ], $customMessage); if ($validator->fails()) { @@ -53,9 +76,6 @@ public function update(Request $request, $id) $indicator = Indicator::find($id); $indicator->name = $request->name; $indicator->description = $request->description; - $indicator->ideal_min = $request->ideal_min; - $indicator->ideal_max = $request->ideal_max; - $indicator->unit = $request->unit; try { $indicator->save(); @@ -66,4 +86,17 @@ public function update(Request $request, $id) return redirect()->back(); } } + + public function destroy($id) + { + $indicator = Indicator::find($id); + try { + $indicator->delete(); + toast('Data berhasil dihapus', 'success')->position('top-right')->autoclose(3000); + return redirect()->back(); + } catch (\Throwable $th) { + toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000); + return redirect()->back(); + } + } } diff --git a/app/Models/EvaluationDetail.php b/app/Models/EvaluationDetail.php index fd87074..18388fa 100644 --- a/app/Models/EvaluationDetail.php +++ b/app/Models/EvaluationDetail.php @@ -13,15 +13,13 @@ class EvaluationDetail extends Model protected $primaryKey = 'id'; protected $guarded = []; - // protected $fillable = [ - // 'evaluation_id', - // 'indicator_id', - // 'value', - // 'cf', - // ]; - public function evaluation() { return $this->belongsTo(Evalutaion::class); } + + public function indicator() + { + return $this->belongsTo(Indicator::class); + } } diff --git a/app/Models/Evalutaion.php b/app/Models/Evalutaion.php index 76c680e..96852e4 100644 --- a/app/Models/Evalutaion.php +++ b/app/Models/Evalutaion.php @@ -9,7 +9,9 @@ class Evalutaion extends Model { use HasFactory; - protected $table = 'evaluations'; + protected $table = 'evaluation'; + + protected $guarded = []; public function land() { diff --git a/app/Models/Indicator.php b/app/Models/Indicator.php index ed2f4f3..50062cb 100644 --- a/app/Models/Indicator.php +++ b/app/Models/Indicator.php @@ -10,12 +10,5 @@ class Indicator extends Model use HasFactory; protected $table = 'indicators'; - - protected $fillable = [ - 'name', - 'description', - 'ideal_min', - 'ideal_max', - 'unit', - ]; + protected $guarded = []; } diff --git a/app/Models/Rule.php b/app/Models/Rule.php index aeebc0e..ad350ff 100644 --- a/app/Models/Rule.php +++ b/app/Models/Rule.php @@ -9,14 +9,11 @@ class Rule extends Model { use HasFactory; - protected $table = 'rules'; + protected $table = 'rule'; protected $fillable = [ 'indicator_id', - 'range_min', - 'range_max', 'mb', - 'md', ]; public function indicator() diff --git a/config/debugbar.php b/config/debugbar.php index b428115..87bdd47 100644 --- a/config/debugbar.php +++ b/config/debugbar.php @@ -14,7 +14,7 @@ | */ - 'enabled' => env('DEBUGBAR_ENABLED', true), + 'enabled' => env('DEBUGBAR_ENABLED', false), 'hide_empty_tabs' => false, // Hide tabs until they have content 'except' => [ 'telescope*', diff --git a/database/migrations/2025_01_28_120615_create_indicator_table.php b/database/migrations/2025_01_28_120615_create_indicator_table.php index 408252a..b89d5c9 100644 --- a/database/migrations/2025_01_28_120615_create_indicator_table.php +++ b/database/migrations/2025_01_28_120615_create_indicator_table.php @@ -15,9 +15,6 @@ public function up(): void $table->id(); $table->string('name'); $table->string('description'); - $table->float('ideal_min'); - $table->float('ideal_max'); - $table->string('unit'); $table->timestamps(); }); } diff --git a/database/migrations/2025_01_29_054112_create_rules_table.php b/database/migrations/2025_02_06_084905_create_rule_table.php similarity index 69% rename from database/migrations/2025_01_29_054112_create_rules_table.php rename to database/migrations/2025_02_06_084905_create_rule_table.php index 85fa65c..1748969 100644 --- a/database/migrations/2025_01_29_054112_create_rules_table.php +++ b/database/migrations/2025_02_06_084905_create_rule_table.php @@ -11,17 +11,14 @@ */ public function up(): void { - Schema::create('rules', function (Blueprint $table) { + Schema::create('rule', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('indicator_id'); - $table->float('range_min'); - $table->float('range_max'); $table->float('mb'); - $table->float('md'); $table->timestamps(); }); - Schema::table('rules', function (Blueprint $table) { + Schema::table('rule', function (Blueprint $table) { $table->foreign('indicator_id')->references('id')->on('indicators')->onDelete('cascade'); }); } @@ -31,6 +28,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('rules'); + Schema::dropIfExists('rule'); } }; diff --git a/database/migrations/2025_02_05_141957_create_evaluations_table.php b/database/migrations/2025_02_06_085110_create_evaluation_table.php similarity index 69% rename from database/migrations/2025_02_05_141957_create_evaluations_table.php rename to database/migrations/2025_02_06_085110_create_evaluation_table.php index f17c5fe..eb1c94d 100644 --- a/database/migrations/2025_02_05_141957_create_evaluations_table.php +++ b/database/migrations/2025_02_06_085110_create_evaluation_table.php @@ -11,13 +11,15 @@ */ public function up(): void { - Schema::create('evaluations', function (Blueprint $table) { + Schema::create('evaluation', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('land_id'); + $table->float('cf_value'); + $table->string('conclusion'); $table->timestamps(); }); - Schema::table('evaluations', function (Blueprint $table) { + Schema::table('evaluation', function (Blueprint $table) { $table->foreign('land_id')->references('id')->on('land')->onDelete('cascade'); }); } @@ -27,6 +29,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('evaluations'); + Schema::dropIfExists('evaluation'); } }; diff --git a/database/migrations/2025_02_05_142241_create_evaluation_details_table.php b/database/migrations/2025_02_06_085614_create_evaluation_details_table.php similarity index 89% rename from database/migrations/2025_02_05_142241_create_evaluation_details_table.php rename to database/migrations/2025_02_06_085614_create_evaluation_details_table.php index fbd9083..417f33c 100644 --- a/database/migrations/2025_02_05_142241_create_evaluation_details_table.php +++ b/database/migrations/2025_02_06_085614_create_evaluation_details_table.php @@ -15,13 +15,12 @@ public function up(): void $table->id(); $table->unsignedBigInteger('evaluation_id'); $table->unsignedBigInteger('indicator_id'); - $table->float('value'); - $table->float('cf'); + $table->float('md_value'); $table->timestamps(); }); Schema::table('evaluation_details', function (Blueprint $table) { - $table->foreign('evaluation_id')->references('id')->on('evaluations')->onDelete('cascade'); + $table->foreign('evaluation_id')->references('id')->on('evaluation')->onDelete('cascade'); $table->foreign('indicator_id')->references('id')->on('indicators')->onDelete('cascade'); }); } diff --git a/database/seeders/IndicatorSeeder.php b/database/seeders/IndicatorSeeder.php index 8c77765..702aaaa 100644 --- a/database/seeders/IndicatorSeeder.php +++ b/database/seeders/IndicatorSeeder.php @@ -13,35 +13,16 @@ class IndicatorSeeder extends Seeder */ public function run(): void { - [ - Indicator::create([ - "name" => "pH Tanah", - "description" => "Tingkat keasaman tanah.", - "ideal_min" => 5.5, - "ideal_max" => 7.0, - "unit" => "pH" - ]), - Indicator::create([ - "name" => "Ketinggian Tempat", - "description" => "Ketinggian dari permukaan laut..", - "ideal_min" => 0, - "ideal_max" => 1000, - "unit" => "meter" - ]), - Indicator::create([ - "name" => "Ketersediaan Air", - "description" => "Ketersediaan air di lahan.", - "ideal_min" => 70, - "ideal_max" => 100, - "unit" => "persen" - ]), - Indicator::create([ - "name" => "Curah Hujan", - "description" => "Intensitas curah hujan.", - "ideal_min" => 1000, - "ideal_max" => 2000, - "unit" => "mm/tahun" - ]), + $indicator = [ + ['name' => 'pH Tanah', 'description' => 'Indicator 1 Description'], + ['name' => 'Ketinggian Tempat', 'description' => 'Indicator 2 Description'], + ['name' => 'Ketersediaan Air', 'description' => 'Indicator 3 Description'], + ['name' => 'Curah Hujan', 'description' => 'Indicator 4 Description'], + ['name' => 'Isolasi', 'description' => 'Indicator 5 Description'] ]; + + foreach ($indicator as $item) { + Indicator::create($item); + } } } diff --git a/database/seeders/RuleSeeder.php b/database/seeders/RuleSeeder.php index acf87ba..75da062 100644 --- a/database/seeders/RuleSeeder.php +++ b/database/seeders/RuleSeeder.php @@ -13,63 +13,16 @@ class RuleSeeder extends Seeder */ public function run(): void { - [ - Rule::create([ - 'indicator_id' => 1, - 'range_min' => 5.5, - 'range_max' => 6.0, - 'mb' => 0.8, - 'md' => 0.2 - ]), - Rule::create([ - 'indicator_id' => 1, - 'range_min' => 6.0, - 'range_max' => 7.0, - 'mb' => 1.0, - 'md' => 0.0 - ]), - Rule::create([ - 'indicator_id' => 2, - 'range_min' => 0, - 'range_max' => 500, - 'mb' => 0.9, - 'md' => 0.1 - ]), - Rule::create([ - 'indicator_id' => 2, - 'range_min' => 500, - 'range_max' => 1000, - 'mb' => 0.8, - 'md' => 0.2 - ]), - Rule::create([ - 'indicator_id' => 3, - 'range_min' => 70, - 'range_max' => 85, - 'mb' => 0.7, - 'md' => 0.3 - ]), - Rule::create([ - 'indicator_id' => 3, - 'range_min' => 85, - 'range_max' => 100, - 'mb' => 1.0, - 'md' => 0.0 - ]), - Rule::create([ - 'indicator_id' => 4, - 'range_min' => 1000, - 'range_max' => 1500, - 'mb' => 0.9, - 'md' => 0.1 - ]), - Rule::create([ - 'indicator_id' => 4, - 'range_min' => 1500, - 'range_max' => 2000, - 'mb' => 1.0, - 'md' => 0.0 - ]), + $rules = [ + ['indicator_id' => 1, 'mb' => 0.5], + ['indicator_id' => 2, 'mb' => 0.5], + ['indicator_id' => 3, 'mb' => 0.5], + ['indicator_id' => 4, 'mb' => 0.5], + ['indicator_id' => 5, 'mb' => 0.5], ]; + + foreach ($rules as $rule) { + Rule::create($rule); + } } } diff --git a/public/assets/js/pages/customJs/master-data/indikator/index.js b/public/assets/js/pages/customJs/master-data/indikator/index.js index a29c033..bf46060 100644 --- a/public/assets/js/pages/customJs/master-data/indikator/index.js +++ b/public/assets/js/pages/customJs/master-data/indikator/index.js @@ -18,22 +18,20 @@ document.addEventListener("DOMContentLoaded", function () { }); function updateData(indicator) { - console.log(indicator); var form = document.getElementById("edit-form"); var indicatorName = form.querySelector('input[name="name"]'); var description = form.querySelector('textarea[name="description"]'); - var idealMin = form.querySelector('input[name="ideal_min"]'); - var idealMax = form.querySelector('input[name="ideal_max"]'); - var unit = form.querySelector('input[name="unit"]'); indicatorName.value = indicator.name; description.value = indicator.description; - idealMin.value = indicator.ideal_min; - idealMax.value = indicator.ideal_max; - unit.value = indicator.unit; form.action = "/data-indikator/" + indicator.id; } +function deleteData(id) { + var form = document.getElementById("delete-form"); + form.action = "/data-indikator/" + id; +} + var checkAll = document.getElementById("checkAll"); checkAll && (checkAll.onclick = function () { @@ -52,14 +50,7 @@ checkAll && }); var perPage = 8, options = { - valueNames: [ - "id", - "indicator_name", - "description", - "ideal_min", - "ideal_max", - "status", - ], + valueNames: ["id", "indicator_name", "status"], page: perPage, pagination: !0, plugins: [ListPagination({ left: 2, right: 2 })], @@ -180,11 +171,11 @@ refreshCallbacks(), .querySelector(".modal-footer").style.display = "none")); }), ischeckboxcheck(), - document - .getElementById("showModal") - .addEventListener("hidden.bs.modal", function () { - clearFields(); - }), + // document + // .getElementById("showModal") + // .addEventListener("hidden.bs.modal", function () { + // clearFields(); + // }), document .querySelector("#customerList") .addEventListener("click", function () { diff --git a/resources/views/master-data/indikator/index.blade.php b/resources/views/master-data/indikator/index.blade.php index 61dd05d..9b12bdf 100644 --- a/resources/views/master-data/indikator/index.blade.php +++ b/resources/views/master-data/indikator/index.blade.php @@ -33,14 +33,14 @@
- {{--
+
-
-
--}} +
@@ -141,12 +133,65 @@ class="fw-medium link-primary">#VZ2101
- {{-- edit modal --}} -