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

142 lines
3.9 KiB
PHP

<?php
namespace Modules\Gallery\app\Http\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Modules\Gallery\app\Models\Gallery;
use Modules\Gallery\app\Repositories\GalleryRepository;
use Modules\Gallery\app\Services\FileManagementService;
use Modules\Gallery\app\Http\Requests\CreateGalleryRequest;
use Modules\Gallery\app\Models\GalleryCategory;
class GalleryController extends Controller
{
protected $galleryRepository;
public function __construct()
{
$this->galleryRepository = new GalleryRepository;
}
/**
* 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') : [];
$galleries = $this->galleryRepository->allGalleries($perPage, $filter);
return view('gallery::index', compact('galleries'));
}
//-- Find Gallery by uuid
public function findGalleryByUuid($uuid)
{
return Gallery::where('uuid', $uuid)->first();
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$galleryCategories = $this->galleryRepository->getGalleryCategories();
return view('gallery::create', compact('galleryCategories'));
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateGalleryRequest $request): RedirectResponse
{
try {
$validated = $request->validated();
$this->galleryRepository->storeGallery($validated);
toastr()->success('Gallery created successfully.');
return redirect()->route('cms.galleries.index');
} catch (\Throwable $th) {
DB::rollback();
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('gallery::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($uuid)
{
$data['galleryCategories'] = $this->galleryRepository->getGalleryCategories();
$data['gallery'] = Gallery::with('galleryCategory')->where('uuid', $uuid)->first();
if (!$data['gallery']) {
toastr()->error('Gallery not found.');
return back();
}
return view('gallery::edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(CreateGalleryRequest $request, $uuid): RedirectResponse
{
$validated = $request->validated();
try {
$gallery = $this->galleryRepository->updateGallery($validated, $uuid);
if (!$gallery) {
toastr()->error('Gallery not found !');
return back();
}
toastr()->success('Gallery updated successfully.');
return redirect()->route('cms.galleries.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
try {
$gallery = $this->galleryRepository->deleteGallery($uuid);
if (!$gallery) {
toastr()->error('Image/Video not found.');
return back();
}
toastr()->success('Image/Video deleted successfully.');
return redirect()->route('cms.galleries.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}