Files
aroginhealthcare/Modules/Banner/app/Http/Controllers/BannerController.php
2025-08-17 16:23:14 +05:45

143 lines
3.6 KiB
PHP

<?php
namespace Modules\Banner\app\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Banner\app\Repositories\BannerRepository;
use Modules\Banner\app\Http\Requests\CreateBannerRequest;
use Modules\Banner\app\Http\Requests\UpdateBannerRequest;
class BannerController extends Controller
{
protected $bannerRepository;
public function __construct()
{
$this->bannerRepository = new BannerRepository;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
$filter = $request->has('filter') ? $request->input('filter') : [];
$banners = $this->bannerRepository->allBanners($perPage, $filter);
return view('banner::index', compact('banners'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('banner::create');
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateBannerRequest $request)
{
try {
$validated = $request->validated();
$this->bannerRepository->storeBanner($validated);
toastr()->success('Banner created successfully.');
return redirect()->route('cms.banners.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
try {
$banner = $this->bannerRepository->findBannerById($id);
if (!$banner) {
toastr()->error('Banner not found.');
return back();
}
return view('banner::show', compact('banner'));
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$banner = $this->bannerRepository->findBannerById($id);
if (!$banner) {
toastr()->error('Banner not found.');
return back();
}
return view('banner::edit', compact('banner'));
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateBannerRequest $request, $uuid)
{
$validated = $request->validated();
try {
$banner = $this->bannerRepository->updateBanner($validated, $uuid);
if (!$banner) {
toastr()->error('Banner not found !');
return back();
}
toastr()->success('Banner updated successfully.');
return redirect()->route('cms.banners.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
try {
$banner = $this->bannerRepository->deleteBanner($uuid);
if (!$banner) {
toastr()->error('Banner not found.');
return back();
}
toastr()->success('Banner deleted successfully.');
return redirect()->route('cms.banners.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}