150 lines
4.3 KiB
PHP
150 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admins;
|
|
|
|
use App\Helpers\ResponseFormatter;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\MasterDistributor;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class MasterDistributorController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('pages.admin.master_distributor.index');
|
|
}
|
|
|
|
|
|
public function getDataDistributor()
|
|
{
|
|
$data = MasterDistributor::get();
|
|
|
|
return datatables()->of($data)
|
|
->addColumn('action', function ($data) {
|
|
return '
|
|
<button class="btn btn-sm btn-warning btn-edit-distributor" data-distributor=' . $data . ' data-idDistributor=' . $data->id . '><i class="fas fa-edit"></i></button>
|
|
<button class="btn btn-sm btn-danger btn-hapus-distributor" data-distributor=' . $data . ' data-idDistributor=' . $data->id . '><i class="fas fa-trash"></i></button>
|
|
';
|
|
})
|
|
->rawColumns(['action' => 'action'])
|
|
->addIndexColumn()
|
|
->make(true);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_distributor' => 'required',
|
|
], [
|
|
'nama_distributor.required' => 'Nama Distributor tidak boleh kosong!',
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// create new data
|
|
$distributor = MasterDistributor::create([
|
|
'nama_distributor' => $request->nama_distributor,
|
|
'alamat' => $request->alamat,
|
|
'telepon' => $request->telepon,
|
|
]);
|
|
|
|
DB::commit();
|
|
return ResponseFormatter::success($distributor, "Distributor berhasil ditambahkan");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
// get data from distributor
|
|
$distributor = MasterDistributor::findOrFail($id);
|
|
return ResponseFormatter::success([
|
|
'distributor' => $distributor
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_distributor_edit' => 'required',
|
|
], [
|
|
'nama_distributor_edit.required' => 'Nama Distributor tidak boleh kosong!',
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// create new data
|
|
$distributor = MasterDistributor::findOrFail($id);
|
|
$distributor->update([
|
|
'nama_distributor' => $request->nama_distributor_edit,
|
|
'alamat' => $request->alamat_edit,
|
|
'telepon' => $request->telepon_edit
|
|
]);
|
|
|
|
DB::commit();
|
|
return ResponseFormatter::success($distributor, "Distributor berhasil diubah");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
// get data from distributor
|
|
$distributor = MasterDistributor::findOrFail($id);
|
|
|
|
// check distributor
|
|
if (!$distributor) {
|
|
return ResponseFormatter::error("Data distributor tidak ditemukan!");
|
|
}
|
|
|
|
// delete distributor
|
|
$distributor->deleted_by = Auth::user()->id;
|
|
$distributor->save();
|
|
$distributor->delete();
|
|
|
|
return ResponseFormatter::success(null, "Distributor berhasil dihapus!");
|
|
}
|
|
}
|