Merge pull request #9 from arieeefajar/fix/master-rule
fix(master-rule): create store and delete func
This commit is contained in:
commit
57da2fdc41
|
@ -3,8 +3,8 @@
|
|||
namespace App\Http\Controllers\MasterData;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Indicator;
|
||||
use App\Models\Rule;
|
||||
use Barryvdh\Debugbar\Facades\Debugbar;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
|
@ -12,32 +12,77 @@ class RuleController extends Controller
|
|||
{
|
||||
public function index()
|
||||
{
|
||||
$rules = Rule::with('indicator')->get();
|
||||
Debugbar::info($rules);
|
||||
return view('master-data.aturan.index', compact('rules'));
|
||||
$rules = Rule::with('indicator')->orderBy('created_at', 'desc')->get();
|
||||
$indicators = Indicator::select('id', 'name')->get();
|
||||
return view('master-data.aturan.index', compact('rules', 'indicators'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$customMessage = [
|
||||
'indicator.required' => 'Harap pilih indikator',
|
||||
'indicator.exists' => 'Indikator tidak ditemukan',
|
||||
|
||||
'ideal_min.required' => 'Nilai minimum wajib diisi',
|
||||
'ideal_min.numeric' => 'Nilai minimum harus berupa angka',
|
||||
|
||||
'ideal_max.required' => 'Nilai maksimum wajib diisi',
|
||||
'ideal_max.numeric' => 'Nilai maksimum harus berupa angka',
|
||||
|
||||
'mb.required' => 'Nilai MB wajib diisi',
|
||||
'mb.numeric' => 'Nilai MB harus berupa angka',
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'indicator' => 'required|exists:indicators,id',
|
||||
'ideal_min' => 'required|numeric',
|
||||
'ideal_max' => 'required|numeric',
|
||||
'mb' => 'required|numeric',
|
||||
], $customMessage);
|
||||
|
||||
if ($validator->fails()) {
|
||||
toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
|
||||
$rule = new Rule();
|
||||
|
||||
$rule->indicator_id = $request->indicator;
|
||||
$rule->ideal_min = $request->ideal_min;
|
||||
$rule->ideal_max = $request->ideal_max;
|
||||
$rule->mb = $request->mb;
|
||||
|
||||
try {
|
||||
$rule->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 = [
|
||||
'range_min.required' => 'Nilai minimum wajib diisi',
|
||||
'range_min.numeric' => 'Nilai minimum harus berupa angka',
|
||||
'indicator.required' => 'Harap pilih indikator',
|
||||
'indicator.exists' => 'Indikator tidak ditemukan',
|
||||
|
||||
'range_max.required' => 'Nilai maksimum wajib diisi',
|
||||
'range_max.numeric' => 'Nilai maksimum harus berupa angka',
|
||||
'ideal_min.required' => 'Nilai minimum wajib diisi',
|
||||
'ideal_min.numeric' => 'Nilai minimum harus berupa angka',
|
||||
|
||||
'ideal_max.required' => 'Nilai maksimum wajib diisi',
|
||||
'ideal_max.numeric' => 'Nilai maksimum harus berupa angka',
|
||||
|
||||
'mb.required' => 'Nilai MB wajib diisi',
|
||||
'mb.numeric' => 'Nilai MB harus berupa angka',
|
||||
|
||||
'md.required' => 'Nilai MD wajib diisi',
|
||||
'md.numeric' => 'Nilai MD harus berupa angka',
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'range_min' => 'required|numeric',
|
||||
'range_max' => 'required|numeric',
|
||||
'indicator' => 'required|exists:indicators,id',
|
||||
'ideal_min' => 'required|numeric',
|
||||
'ideal_max' => 'required|numeric',
|
||||
'mb' => 'required|numeric',
|
||||
'md' => 'required|numeric',
|
||||
], $customMessage);
|
||||
|
||||
if ($validator->fails()) {
|
||||
|
@ -46,10 +91,9 @@ public function update(Request $request, $id)
|
|||
}
|
||||
|
||||
$rule = Rule::find($id);
|
||||
$rule->range_min = $request->range_min;
|
||||
$rule->range_max = $request->range_max;
|
||||
$rule->ideal_min = $request->ideal_min;
|
||||
$rule->ideal_max = $request->ideal_max;
|
||||
$rule->mb = $request->mb;
|
||||
$rule->md = $request->md;
|
||||
|
||||
try {
|
||||
$rule->save();
|
||||
|
@ -60,4 +104,17 @@ public function update(Request $request, $id)
|
|||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$rule = Rule::find($id);
|
||||
try {
|
||||
$rule->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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,11 +10,7 @@ class Rule extends Model
|
|||
use HasFactory;
|
||||
|
||||
protected $table = 'rule';
|
||||
|
||||
protected $fillable = [
|
||||
'indicator_id',
|
||||
'mb',
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
public function indicator()
|
||||
{
|
||||
|
|
|
@ -14,6 +14,8 @@ public function up(): void
|
|||
Schema::create('rule', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('indicator_id');
|
||||
$table->float('ideal_min');
|
||||
$table->float('ideal_max');
|
||||
$table->float('mb');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
|
|
@ -14,11 +14,11 @@ class RuleSeeder extends Seeder
|
|||
public function run(): void
|
||||
{
|
||||
$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],
|
||||
['indicator_id' => 1, 'ideal_min' => 5.5, 'ideal_max' => 7, 'mb' => 0.9],
|
||||
['indicator_id' => 2, 'ideal_min' => 0, 'ideal_max' => 1500, 'mb' => 0.8],
|
||||
['indicator_id' => 3, 'ideal_min' => 70, 'ideal_max' => 100, 'mb' => 0.7],
|
||||
['indicator_id' => 4, 'ideal_min' => 1000, 'ideal_max' => 2000, 'mb' => 0.9],
|
||||
['indicator_id' => 5, 'ideal_min' => 0, 'ideal_max' => 100, 'mb' => 0.8],
|
||||
];
|
||||
|
||||
foreach ($rules as $rule) {
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
document.addEventListener("DOMContentLoaded", function () {
|
||||
const inputs = document.querySelectorAll(
|
||||
"#rangemin-field, #rangemax-field",
|
||||
"#mb-field",
|
||||
"#md-field"
|
||||
"#idealmin-field, #idealmax-field",
|
||||
"#mb-field"
|
||||
);
|
||||
|
||||
const inputEdit = document.querySelectorAll(
|
||||
"#idealmin-edit-field, #idealmax-edit-field",
|
||||
"#mb-edit-field"
|
||||
);
|
||||
var indicatorField = document.getElementById("indicator-field");
|
||||
var indicatorVal = new Choices(indicatorField);
|
||||
var indicatorEditField = document.getElementById("indicator-edit-field");
|
||||
var indicatorEditVal = new Choices(indicatorEditField);
|
||||
|
||||
inputs.forEach((input) => {
|
||||
input.addEventListener("input", function () {
|
||||
this.value = this.value.replace(/[^0-9.]/g, "");
|
||||
|
@ -17,25 +25,40 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
inputEdit.forEach((input) => {
|
||||
input.addEventListener("input", function () {
|
||||
this.value = this.value.replace(/[^0-9.]/g, "");
|
||||
this.value = this.value.replace(/^(\.)/, "");
|
||||
if ((this.value.match(/\./g) || []).length > 1) {
|
||||
this.value = this.value.substring(
|
||||
0,
|
||||
this.value.lastIndexOf(".")
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function updateData(rule) {
|
||||
console.log(rule);
|
||||
var form = document.getElementById("edit-form");
|
||||
var indicatorName = form.querySelector('input[id="indicatorname-field"]');
|
||||
var rangMin = form.querySelector('input[id="rangemin-field"]');
|
||||
var rangMax = form.querySelector('input[id="rangemax-field"]');
|
||||
var mb = form.querySelector('input[id="mb-field"]');
|
||||
var md = form.querySelector('input[id="md-field"]');
|
||||
// var indicatorName = form.querySelector('input[id="indicatorname-field"]');
|
||||
var idealMin = form.querySelector('input[id="idealmin-edit-field"]');
|
||||
var idealMax = form.querySelector('input[id="idealmax-edit-field"]');
|
||||
var mb = form.querySelector('input[id="mb-edit-field"]');
|
||||
|
||||
indicatorName.value = rule.indicator.name;
|
||||
rangMin.value = rule.range_min;
|
||||
rangMax.value = rule.range_max;
|
||||
// indicatorName.value = rule.indicator.name;
|
||||
idealMin.value = rule.ideal_max;
|
||||
idealMax.value = rule.ideal_min;
|
||||
mb.value = rule.mb;
|
||||
md.value = rule.md;
|
||||
form.action = "/data-aturan/" + rule.id;
|
||||
}
|
||||
|
||||
function deleteData(id) {
|
||||
var form = document.getElementById("delete-form");
|
||||
form.action = "/data-aturan/" + id;
|
||||
}
|
||||
|
||||
var perPage = 10,
|
||||
options = {
|
||||
valueNames: [
|
||||
|
|
|
@ -33,14 +33,14 @@
|
|||
<div class="card-body">
|
||||
<div id="customerList">
|
||||
<div class="row g-4 mb-3">
|
||||
{{-- <div class="col-sm-auto">
|
||||
<div class="col-sm-auto">
|
||||
<div>
|
||||
<button type="button" class="btn btn-primary add-btn" data-bs-toggle="modal"
|
||||
id="create-btn" data-bs-target="#showModal">
|
||||
<i class="ri-add-line align-bottom me-1"></i> Add
|
||||
<button type="button" class="btn btn-success add-btn" data-bs-toggle="modal"
|
||||
id="create-btn" data-bs-target="#addModal">
|
||||
<i class="ri-add-line align-bottom me-1"></i> Tambah
|
||||
</button>
|
||||
</div>
|
||||
</div> --}}
|
||||
</div>
|
||||
<div class="col-sm">
|
||||
<div class="d-flex justify-content-sm-end">
|
||||
<div class="search-box ms-2">
|
||||
|
@ -61,13 +61,10 @@
|
|||
<th class="sort" data-sort="indicator_name">
|
||||
Indikator
|
||||
</th>
|
||||
<th class="sort" data-sort="range_min">Range Min</th>
|
||||
<th class="sort" data-sort="range_max">Range Max</th>
|
||||
<th class="sort" data-sort="mb">
|
||||
Meansure Belief
|
||||
</th>
|
||||
<th class="sort" data-sort="range_min">Ideal Min</th>
|
||||
<th class="sort" data-sort="range_max">Ideal Max</th>
|
||||
<th class="sort" data-sort="status">
|
||||
Meansure Disbelief
|
||||
Meansure Belief
|
||||
</th>
|
||||
<th class="sort" data-sort="action">Action</th>
|
||||
</tr>
|
||||
|
@ -81,11 +78,10 @@
|
|||
class="fw-medium link-primary">#VZ2101</a>
|
||||
</td>
|
||||
<td class="indicator_name">{{ $rule->indicator->name }}</td>
|
||||
<td class="range_min">{{ $rule->range_min }}</td>
|
||||
<td class="range_max">{{ $rule->range_max }}</td>
|
||||
<td class="mb">{{ $rule->mb }}</td>
|
||||
<td class="range_min">{{ $rule->ideal_min }}</td>
|
||||
<td class="range_max">{{ $rule->ideal_max }}</td>
|
||||
<td class="status">
|
||||
<span>{{ $rule->md }}</span>
|
||||
<span>{{ $rule->mb }}</span>
|
||||
</td>
|
||||
<td>
|
||||
<div class="d-flex gap-2 justify-content-center">
|
||||
|
@ -96,12 +92,14 @@ class="fw-medium link-primary">#VZ2101</a>
|
|||
Edit
|
||||
</button>
|
||||
</div>
|
||||
{{-- <div class="remove">
|
||||
<button class="btn btn-sm btn-success remove-item-btn"
|
||||
data-bs-toggle="modal" data-bs-target="#deleteRecordModal">
|
||||
Remove
|
||||
<div class="remove">
|
||||
<button class="btn btn-sm btn-danger remove-item-btn"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#deleteRecordModal"
|
||||
onclick="deleteData({{ $rule->id }})">
|
||||
Hapus
|
||||
</button>
|
||||
</div> --}}
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -141,12 +139,87 @@ class="fw-medium link-primary">#VZ2101</a>
|
|||
</div>
|
||||
<!-- end row -->
|
||||
|
||||
{{-- edit modal --}}
|
||||
<div class="modal fade" id="showModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
||||
{{-- add modal --}}
|
||||
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header bg-light p-3">
|
||||
<h5 class="modal-title" id="exampleModalLabel"></h5>
|
||||
<h5 class="modal-title" id="exampleModalLabel">Tambah Data Rule</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"
|
||||
id="close-modal"></button>
|
||||
</div>
|
||||
<form action="{{ route('master_data.aturan.store') }}" class="needs-validation" method="POST"
|
||||
novalidate id="add-form">
|
||||
@csrf
|
||||
<div class="modal-body">
|
||||
<div class="mb-3" id="modal-id" style="display: none">
|
||||
<label for="id-field" class="form-label">ID</label>
|
||||
<input type="text" id="id-field" class="form-control" placeholder="ID"
|
||||
readonly />
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="indicatorname-field" class="form-label">Indikator</label>
|
||||
<select name="indicator" class="form-control" id="indicator-field" required>
|
||||
<option value="" selected disabled>Pilih Indikator</option>
|
||||
@foreach ($indicators as $indicator)
|
||||
<option value="{{ $indicator->id }}">{{ $indicator->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<div class="invalid-feedback">
|
||||
Pilih Indikator
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="idealmin-field" class="form-label">Ideal Min</label>
|
||||
<input type="text" id="idealmin-field" class="form-control" name="ideal_min"
|
||||
placeholder="Masukan Nilai Ideal Min" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Nilai Ideal Min
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="idealmax-field" class="form-label">Ideal Max</label>
|
||||
<input type="text" id="idealmax-field" class="form-control" name="ideal_max"
|
||||
placeholder="Masukan Nilai Ideal Max" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Nilai Ideal Max
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="mb-field" class="form-label">Meansure Belief</label>
|
||||
<input type="text" id="mb-field" class="form-control" name="mb"
|
||||
placeholder="Masukan Meansure Belief" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Meansure Belief
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div class="hstack gap-2 justify-content-end">
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal">
|
||||
Tutup
|
||||
</button>
|
||||
<button type="submit" class="btn btn-success" id="edit-btn">
|
||||
Simpan
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{-- edit modal --}}
|
||||
<div class="modal fade" id="showModal" tabindex="-1" aria-labelledby="exampleModalLabel"
|
||||
aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header bg-light p-3">
|
||||
<h5 class="modal-title" id="exampleModalLabel">Edit Data Rule</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"
|
||||
id="close-modal"></button>
|
||||
</div>
|
||||
|
@ -162,32 +235,42 @@ class="fw-medium link-primary">#VZ2101</a>
|
|||
|
||||
<div class="mb-3">
|
||||
<label for="indicatorname-field" class="form-label">Indikator</label>
|
||||
<input type="text" id="indicatorname-field" class="form-control"
|
||||
placeholder="Masukan Nama Indikator" readonly />
|
||||
<select name="indicator" id="indicator-edit-field" class="form-control">
|
||||
<option value="" selected disabled>Pilih Indikator</option>
|
||||
@foreach ($indicators as $indicator)
|
||||
<option value="{{ $indicator->id }}">{{ $indicator->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<div class="invalid-feedback">
|
||||
Pilih Indikator
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="rangemin-field" class="form-label">Range Min</label>
|
||||
<input type="text" id="rangemin-field" class="form-control" name="range_min"
|
||||
placeholder="Masukan Range Min" required />
|
||||
<label for="idealmin-field" class="form-label">Ideal Min</label>
|
||||
<input type="text" id="idealmin-edit-field" class="form-control" name="ideal_min"
|
||||
placeholder="Masukan Nilai Ideal Min" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Nilai Ideal Min
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="rangemax-field" class="form-label">Range Max</label>
|
||||
<input type="text" id="rangemax-field" class="form-control" name="range_max"
|
||||
placeholder="Masukan Range Max" required />
|
||||
<label for="idealmax-edit-field" class="form-label">Ideal Max</label>
|
||||
<input type="text" id="idealmax-edit-field" class="form-control" name="ideal_max"
|
||||
placeholder="Masukan Nilai Ideal Max" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Nilai Ideal Max
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="mb-field" class="form-label">Meansure Belief</label>
|
||||
<input type="text" id="mb-field" class="form-control" name="mb"
|
||||
placeholder="Masukan Meansure Belief" required />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="md-field" class="form-label">Meansure Disbelief</label>
|
||||
<input type="text" id="md-field" class="form-control" name="md"
|
||||
placeholder="Masukan Meansure Disbelief" required>
|
||||
<label for="mb-edit-field" class="form-label">Meansure Belief</label>
|
||||
<input type="text" id="mb-edit-field" class="form-control" name="mb"
|
||||
placeholder="Masukan Nilai Meansure Belief" required />
|
||||
<div class="invalid-feedback">
|
||||
Masukan Meansure Belief
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
|
@ -219,19 +302,23 @@ class="fw-medium link-primary">#VZ2101</a>
|
|||
colors="primary:#25a0e2,secondary:#00bd9d"
|
||||
style="width: 100px; height: 100px"></lord-icon>
|
||||
<div class="mt-4 pt-2 fs-15 mx-4 mx-sm-5">
|
||||
<h4>Are you sure ?</h4>
|
||||
<h4>Anda yakin ?</h4>
|
||||
<p class="text-muted mx-4 mb-0">
|
||||
Are you sure you want to remove this record ?
|
||||
Anda yakin akan menghapus data ini ?
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex gap-2 justify-content-center mt-4 mb-2">
|
||||
<button type="button" class="btn w-sm btn-light" data-bs-dismiss="modal">
|
||||
Close
|
||||
Tutup
|
||||
</button>
|
||||
<button type="button" class="btn w-sm btn-primary" id="delete-record">
|
||||
Yes, Delete It!
|
||||
<form action="" method="POST" id="delete-form">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn w-sm btn-danger" id="delete-record">
|
||||
Ya, Hapus!
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -86,9 +86,9 @@
|
|||
|
||||
Route::prefix('data-aturan')->controller(RuleController::class)->name('aturan.')->group(function () {
|
||||
Route::get('/', 'index')->name('index');
|
||||
// Route::post('/', 'store')->name('store');
|
||||
Route::post('/', 'store')->name('store');
|
||||
Route::put('/{id}', 'update')->name('update');
|
||||
// Route::delete('/{id}', 'destroy')->name('destroy');
|
||||
Route::delete('/{id}', 'destroy')->name('destroy');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue