146 lines
4.8 KiB
PHP
146 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Service\app\Repositories;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
use Modules\Service\app\Models\Service;
|
|
use Modules\Service\app\Models\ServiceMeta;
|
|
|
|
class ServiceRepository
|
|
{
|
|
//-- Retrieve all Services
|
|
public function allServices($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Service::with('serviceMeta')->when(array_keys($filter, true), function ($query) use ($filter) {
|
|
if (!empty($filter['title'])) {
|
|
$query->where('title', $filter['title']);
|
|
}
|
|
if (!empty($filter['price'])) {
|
|
$query->where('price', 'like', '%' . $filter['price'] . '%');
|
|
}
|
|
})
|
|
->orderBy($sort['by'], $sort['sort'])
|
|
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
|
}
|
|
|
|
//-- Find Service by uuid
|
|
public function findServiceByUuid($uuid)
|
|
{
|
|
return Service::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
public function storeService(array $validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$service = new Service();
|
|
$service->uuid = Str::uuid();
|
|
$service->title = $validated['title'];
|
|
$service->summary = $validated['summary'];
|
|
$service->detail = $validated['detail'];
|
|
$service->status = $validated['status'];
|
|
$service->slug = $validated['slug'];
|
|
// $service->homepage_flag = isset($validated['homepage_flag']) && $validated['homepage_flag'] == 1;
|
|
$service->save();
|
|
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'services',
|
|
model: $service
|
|
);
|
|
}
|
|
$serviceMeta = new ServiceMeta();
|
|
$serviceMeta->uuid = Str::uuid();
|
|
$serviceMeta->meta_title = $validated['meta_title'];
|
|
$serviceMeta->meta_description = $validated['meta_description'];
|
|
$serviceMeta->meta_keywords = $validated['meta_keywords'];
|
|
$service->serviceMeta()->save($serviceMeta);
|
|
DB::commit();
|
|
|
|
return $service;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollback();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function updateService($validated, $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$service = $this->findServiceByUuid($uuid);
|
|
if (!$service) {
|
|
|
|
return null;
|
|
}
|
|
|
|
$service->title = $validated['title'];
|
|
$service->summary = $validated['summary'];
|
|
$service->detail = $validated['detail'];
|
|
$service->status = $validated['status'];
|
|
$service->slug = $validated['slug'];
|
|
// $service->homepage_flag = isset($validated['homepage_flag']) && $validated['homepage_flag'] == 1;
|
|
$service->save();
|
|
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'services',
|
|
filePath: $service->image_path,
|
|
model: $service
|
|
);
|
|
}
|
|
|
|
// Update or create service meta
|
|
$serviceMeta = $service->serviceMeta()->firstOrNew([]);
|
|
if (!$serviceMeta->exists) {
|
|
$serviceMeta->uuid = Str::uuid();
|
|
}
|
|
$serviceMeta->meta_title = $validated['meta_title'];
|
|
$serviceMeta->meta_description = $validated['meta_description'];
|
|
$serviceMeta->meta_keywords = $validated['meta_keywords'];
|
|
$serviceMeta->save();
|
|
|
|
DB::commit();
|
|
|
|
return $service;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollBack();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//-- Delete Service
|
|
public function deleteService(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$service = $this->findServiceByUuid($uuid);
|
|
if (!$service) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the activity
|
|
if ($service->image_path !== null) {
|
|
FileManagementService::deleteFile($service->image_path);
|
|
}
|
|
// Delete associated service meta and service itself
|
|
$service->serviceMeta()->delete();
|
|
$service->delete();
|
|
|
|
DB::commit();
|
|
|
|
return $service;
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
return null;
|
|
}
|
|
}
|
|
}
|