136 lines
3.6 KiB
PHP
136 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Testimonial\app\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Testimonial\app\Http\Requests\CreateTestimonialRequest;
|
|
use Modules\Testimonial\app\Repositories\TestimonialRepository;
|
|
|
|
class TestimonialController extends Controller
|
|
{
|
|
protected $testimonialRepository;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->testimonialRepository = new TestimonialRepository;
|
|
}
|
|
|
|
/**
|
|
* 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') : [];
|
|
$testimonials = $this->testimonialRepository->allTestimonials($perPage, $filter);
|
|
|
|
return view('testimonial::index', compact('testimonials'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('testimonial::create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(CreateTestimonialRequest $request): RedirectResponse
|
|
{
|
|
|
|
try {
|
|
$validated = $request->validated();
|
|
|
|
$this->testimonialRepository->storeTestimonial($validated);
|
|
toastr()->success('Testimonial created successfully!');
|
|
|
|
return redirect()->route('cms.testimonials.index');
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
|
|
return back();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return view('testimonial::show');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($uuid)
|
|
{
|
|
$testimonial = $this->testimonialRepository->findTestimonialByUuid($uuid);
|
|
if (!$testimonial) {
|
|
toastr()->error('Testimonial not found');
|
|
return back();
|
|
}
|
|
|
|
return view('testimonial::edit', compact('testimonial'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(CreateTestimonialRequest $request, $uuid): RedirectResponse
|
|
{
|
|
|
|
try {
|
|
$validated = $request->validated();
|
|
|
|
$testimonial = $this->testimonialRepository->updateTestimonial($validated, $uuid);
|
|
|
|
if (!$testimonial) {
|
|
toastr()->error('Testimonial not found !');
|
|
|
|
return null;
|
|
}
|
|
|
|
toastr()->success('Testimonial updated successfully.');
|
|
|
|
return redirect()->route('cms.testimonials.index');
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$testimonial = $this->testimonialRepository->deleteTestimonial($uuid);
|
|
if (!$testimonial) {
|
|
toastr()->error('Testimonial not found');
|
|
return back();
|
|
}
|
|
DB::commit();
|
|
toastr()->success('Testimonial deleted successfully.');
|
|
|
|
return redirect()->route('cms.testimonials.index');
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
|
|
return back()->error('Something went wrong');
|
|
}
|
|
}
|
|
}
|