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

153 lines
4.7 KiB
PHP

<?php
namespace Modules\Gallery\app\Repositories;
use Modules\Gallery\app\Models\Gallery;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Banner\app\Services\FileManagementService;
use Modules\Gallery\app\Models\GalleryCategory;
class GalleryRepository
{
//-- Retrieve all Galleries
public function allGalleries($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
{
return Gallery::with('galleryCategory')->when(array_keys($filter, true), function ($query) use ($filter) {
if (!empty($filter['detail'])) {
$query->where('detail', 'like', '%' . $filter['detail'] . '%');
}
if (!empty($filter['type'])) {
$query->where('type', 'like', '%' . $filter['type'] . '%');
}
})
->orderBy($sort['by'], $sort['sort'])
->paginate($perPage ?: env('PAGE_LIMIT', 999));
}
public function getGalleryCategories()
{
return GalleryCategory::get();
}
public function storeGallery(array $validated)
{
DB::beginTransaction();
try {
// Create Gallery Category
if (GalleryCategory::where('category', $validated['category'])->doesntExist()) {
$galleryCategory = GalleryCategory::create([
'uuid' => Str::uuid(),
'category' => $validated['category'],
'type' => $validated['type'],
]);
} else {
$galleryCategory = GalleryCategory::where('category', $validated['category'])->first();
}
// Create Gallery
$gallery = $galleryCategory->gallery()->create([
'uuid' => Str::uuid(),
'detail' => $validated['detail'],
'video_link' => isset($validated['video_link']) ? $validated['video_link'] : null,
'status' => $validated['status'],
]);
//-- store image
if (isset($validated['image']) && $validated['image']->isValid()) {
FileManagementService::storeFile(
file: $validated['image'],
uploadedFolderName: 'galleries',
model: $gallery
);
}
DB::commit();
return $gallery;
} catch (\Throwable $th) {
DB::rollback();
report($th);
}
}
public function findGalleryById($uuid)
{
return Gallery::where('uuid', $uuid)->first();
}
public function updateGallery($validated, $uuid)
{
try {
$gallery = $this->findGalleryById($uuid);
if (!$gallery) {
return null;
}
if (GalleryCategory::where('category', $validated['category'])->doesntExist()) {
$galleryCategory = GalleryCategory::create([
'uuid' => Str::uuid(),
'category' => $validated['category'],
'type' => $validated['type'],
]);
} else {
$galleryCategory = GalleryCategory::where('category', $validated['category'])->first();
}
//-- update gallery
$gallery->detail = $validated['detail'];
if ($gallery->video_link) {
$gallery->video_link = $validated['video_link'];
}
$gallery->gallery_category_id = $galleryCategory->id;
$gallery->status = $validated['status'];
$gallery->save();
//-- Update image
if (isset($validated['image']) && $validated['image']->isValid()) {
FileManagementService::uploadFile(
file: $validated['image'],
uploadedFolderName: 'galleries',
filePath: $gallery->image_path,
model: $gallery
);
}
return $gallery;
} catch (\Throwable $th) {
report($th);
DB::transaction();
return null;
}
}
public function deleteGallery($uuid)
{
DB::beginTransaction();
try {
$gallery = $this->findGalleryById($uuid);
if (!$gallery) {
return null;
}
// Delete the image file associated with the banner
if ($gallery->image_path !== null) {
FileManagementService::deleteFile($gallery->image_path);
}
$gallery->delete();
DB::commit();
return true;
} catch (\Throwable $th) {
DB::rollback();
report($th);
return null;
}
}
}