Files
aroginhealthcare/Modules/Testimonial/app/Repositories/TestimonialRepository.php
2025-08-17 16:23:14 +05:45

131 lines
3.9 KiB
PHP

<?php
namespace Modules\Testimonial\app\Repositories;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Banner\app\Services\FileManagementService;
use Modules\Testimonial\app\Models\Testimonial;
class TestimonialRepository
{
//-- Retrieve all Services
public function allTestimonials($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
{
return Testimonial::when(array_keys($filter, true), function ($query) use ($filter) {
if (! empty($filter['name'])) {
$query->where('name', $filter['name']);
}
if (! empty($filter['designation'])) {
$query->where('designation', 'like', '%'.$filter['designation'].'%');
}
})
->orderBy($sort['by'], $sort['sort'])
->paginate($perPage ?: env('PAGE_LIMIT', 999));
}
//-- Find Service by uuid
public function findTestimonialByUuid($uuid)
{
return Testimonial::where('uuid', $uuid)->first();
}
public function storeTestimonial(array $validated)
{
DB::beginTransaction();
try {
$testimonial = new Testimonial();
$testimonial->uuid = Str::uuid();
$testimonial->name = $validated['name'];
$testimonial->designation = $validated['designation'];
$testimonial->ordering = $validated['ordering'];
$testimonial->statement = $validated['statement'];
$testimonial->statement = $validated['statement'];
$testimonial->link = $validated['link'];
$testimonial->save();
if (isset($validated['image']) && $validated['image']->isValid()) {
FileManagementService::storeFile(
file: $validated['image'],
uploadedFolderName: 'testimonials',
model: $testimonial
);
}
DB::commit();
return $testimonial;
} catch (\Throwable $th) {
report($th);
DB::rollback();
return null;
}
}
public function updateTestimonial($validated, $uuid)
{
DB::beginTransaction();
try {
$testimonial = $this->findTestimonialByUuid($uuid);
if (! $testimonial) {
return null;
}
$testimonial->name = $validated['name'];
$testimonial->designation = $validated['designation'];
$testimonial->ordering = $validated['ordering'];
$testimonial->statement = $validated['statement'];
$testimonial->link = $validated['link'];
$testimonial->status = $validated['status'];
$testimonial->save();
if (isset($validated['image']) && $validated['image']->isValid()) {
FileManagementService::uploadFile(
file: $validated['image'],
uploadedFolderName: 'testimonials',
filePath: $testimonial->image_path,
model: $testimonial
);
}
return $testimonial;
DB::commit();
} catch (\Throwable $th) {
report($th);
DB::rollBack();
return null;
}
}
//-- Delete Testimonial
public function deleteTestimonial(string $uuid)
{
DB::beginTransaction();
try {
$testimonial = $this->findTestimonialByUuid($uuid);
if (! $testimonial) {
return null;
}
// Delete the image file associated with the activity
if ($testimonial->image_path !== null) {
FileManagementService::deleteFile($testimonial->image_path);
}
$testimonial->delete();
DB::commit();
return $testimonial;
} catch (\Throwable $th) {
DB::rollBack();
report($th);
return null;
}
}
}