first change
This commit is contained in:
169
Modules/Client/app/Http/Controllers/ClientController.php
Normal file
169
Modules/Client/app/Http/Controllers/ClientController.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Client\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Client\Interfaces\ClientInterface;
|
||||
use Modules\Client\Models\Client;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class ClientController extends Controller
|
||||
{
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (request()->ajax()) {
|
||||
$model = Client::query()->orderBy('order');
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('logo', function (Client $client) {
|
||||
return $client->getRawOriginal('logo') ? "<img src='{$client->logo}' alt='{$client->name}' class='rounded avatar-sm material-shadow ms-2 img-thumbnail'>" : '-';
|
||||
|
||||
})
|
||||
->editColumn('title', function (Client $client) {
|
||||
$route = route('client.show', $client->id);
|
||||
$html ="<a class='link link-primary' href='{$route}'>{{ $client->name }}</a>";
|
||||
return $html;
|
||||
})
|
||||
->editColumn('manager_name', function (Client $client) {
|
||||
return $client->manager_name ?? "-";
|
||||
})
|
||||
->editColumn('status', function (Client $client) {
|
||||
$status = $client->status ? 'Published' : 'Draft';
|
||||
$color = $client->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('action', 'client::client.datatable.action')
|
||||
->rawColumns(['status', 'logo', 'action'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('client::client.index', [
|
||||
'title' => 'Client List',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = 'Create Client';
|
||||
$data['editable'] = false;
|
||||
return view('client::client.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$client = null;
|
||||
|
||||
$validated = $request->validate([
|
||||
"name" => ["required", "string", "max:255"],
|
||||
]);
|
||||
|
||||
$request->merge([
|
||||
"company_name" => $request->name,
|
||||
'order' => ($maxOrder = Client::max('order')) !== null ? ++$maxOrder : 1,
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($request, &$client) {
|
||||
$input = $request->only(Client::getFillableFields());
|
||||
$client = Client::create($input);
|
||||
});
|
||||
|
||||
flash()->success("Client {$client->name} has been created");
|
||||
return redirect()->route('client.index');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
flash()->error($e->getMessage());
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$data["title"] = "Show Client";
|
||||
$data["client"] = $this->client->findById($id);
|
||||
return view('client::client.show', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = 'Edit Client';
|
||||
$data['editable'] = true;
|
||||
$data["client"] = $this->client->findById($id);
|
||||
return view('client::client.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
"title" => ["required"],
|
||||
]);
|
||||
|
||||
try {
|
||||
|
||||
DB::transaction(function () use ($request, $id) {
|
||||
$input = $request->only(Client::getFillableFields());
|
||||
$client = $this->client->update($id, $input);
|
||||
});
|
||||
|
||||
flash()->success('Client has been updated!');
|
||||
|
||||
return redirect()->route('client.index');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
flash()->error($th->getMessage());
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
|
||||
DB::transaction(function () use ($id) {
|
||||
$client = $this->client->delete($id);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'message' => 'Client has been deleted!',
|
||||
], 200);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return response()->json([
|
||||
'status' => 500,
|
||||
'message' => 'Failed to delete client!',
|
||||
'error' => $th->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user