StocksNew/Modules/Admin/app/Http/Controllers/DropdownController.php
Sampanna Rimal 53c0140f58 first commit
2024-08-27 17:48:06 +05:45

112 lines
3.0 KiB
PHP

<?php
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Admin\Repositories\DropdownInterface;
use Modules\Admin\Repositories\FieldInterface;
class DropdownController extends Controller
{
private $fieldRepository;
private $dropdownRepository;
public function __construct(FieldInterface $fieldRepository,
DropdownInterface $dropdownRepository) {
$this->fieldRepository = $fieldRepository;
$this->dropdownRepository = $dropdownRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'Dropdown List';
$data['fields'] = $this->fieldRepository->findAll();
$data['fieldLists'] = $data['fields']->pluck('title', 'id');
return view('admin::dropdown.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin::create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
try {
// $inputData['status'] = $request->status ?? 10;
$this->dropdownRepository->create($inputData);
toastr()->success('Dropdown Created Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('dropdown.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('admin::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$dropdownModel = $this->dropdownRepository->getDropdownById($id);
$fieldLists = $this->fieldRepository->getList();
return response()->json([
'view' => view('admin::dropdown.edit', compact('dropdownModel', 'fieldLists'))->render(),
'status' => true,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->except(['_method', '_token']);
try {
$inputData['status'] = $request->status ?? 10;
$this->dropdownRepository->update($id, $inputData);
toastr()->success('Dropdown Update Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('dropdown.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->dropdownRepository->delete($id);
toastr()->success('Dropdown Deleted Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return response()->json([
'status' => true,
]);
}
}