123 lines
3.5 KiB
PHP
123 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Banner\app\Repositories;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Banner\app\Models\Banner;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
|
|
class BannerRepository
|
|
{
|
|
public function allBanners($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Banner::when(array_keys($filter, true), function ($query) use ($filter) {
|
|
if (!empty($filter['title'])) {
|
|
$query->where('title', $filter['title']);
|
|
}
|
|
if (!empty($filter['caption'])) {
|
|
$query->where('caption', 'like', '%' . $filter['caption'] . '%');
|
|
}
|
|
})
|
|
->orderBy($sort['by'], $sort['sort'])
|
|
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
|
}
|
|
|
|
public function findBannerById($uuid)
|
|
{
|
|
return Banner::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
public function storeBanner($validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$banner = new Banner();
|
|
$banner->uuid = Str::uuid();
|
|
$banner->title = $validated['title'];
|
|
$banner->ordering = $validated['ordering'];
|
|
$banner->link = $validated['link'];
|
|
$banner->caption = $validated['caption'];
|
|
$banner->status = $validated['status'];
|
|
$banner->save();
|
|
|
|
//-- store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'banners',
|
|
model: $banner
|
|
);
|
|
}
|
|
DB::commit();
|
|
|
|
return $banner;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollback();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function updateBanner($validated, $uuid)
|
|
{
|
|
try {
|
|
$banner = $this->findBannerById($uuid);
|
|
|
|
if (!$banner) {
|
|
return null;
|
|
}
|
|
|
|
$banner->title = $validated['title'];
|
|
$banner->link = $validated['link'];
|
|
$banner->ordering = $validated['ordering'];
|
|
$banner->caption = $validated['caption'];
|
|
$banner->status = $validated['status'];
|
|
$banner->save();
|
|
|
|
//-- Update image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'banners',
|
|
filePath: $banner->image_path,
|
|
model: $banner
|
|
);
|
|
}
|
|
|
|
return $banner;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::transaction();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
public function deleteBanner($uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$banner = $this->findBannerById($uuid);
|
|
|
|
if (!$banner) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the banner
|
|
if ($banner->image_path !== null) {
|
|
FileManagementService::deleteFile($banner->image_path);
|
|
}
|
|
|
|
$banner->delete();
|
|
DB::commit();
|
|
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
return null;
|
|
}
|
|
}
|
|
}
|