119 lines
3.5 KiB
PHP
119 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Activity\app\Repositories;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Activity\app\Models\Activity;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
|
|
class ActivityRepository
|
|
{
|
|
//-- Retrieve all Activities
|
|
public function allActivities($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Activity::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 Activiy by uuid
|
|
public function findActivityByUuid($uuid)
|
|
{
|
|
return Activity::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
// Store Activity
|
|
public function storeActivity(array $validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$activity = new Activity();
|
|
$activity->uuid = Str::uuid();
|
|
$activity->name = $validated['name'];
|
|
$activity->status = $validated['status'];
|
|
$activity->save();
|
|
|
|
// Check if image is uploaded and valid and store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'activities',
|
|
model: $activity
|
|
);
|
|
}
|
|
DB::commit();
|
|
|
|
return $activity;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollback();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Update Activity
|
|
public function updateActivity(array $validated, string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$activity = $this->findActivityByUuid($uuid);
|
|
if (! $activity) {
|
|
return null;
|
|
}
|
|
$activity->name = $validated['name'];
|
|
$activity->status = $validated['status'];
|
|
$activity->save();
|
|
|
|
// Check if image is uploaded and valid and store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'activities',
|
|
filePath: $activity->image_path,
|
|
model: $activity
|
|
);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $activity;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollBack();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//-- Delete Activity
|
|
public function deleteActivity(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$activity = $this->findActivityByUuid($uuid);
|
|
if (! $activity) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the activity
|
|
if ($activity->image_path !== null) {
|
|
FileManagementService::deleteFile($activity->image_path);
|
|
}
|
|
$activity->delete();
|
|
|
|
return $activity;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollBack();
|
|
return null;
|
|
}
|
|
}
|
|
}
|