136 lines
4.3 KiB
PHP
136 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Package\app\Repositories;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Package\app\Models\Package;
|
|
use Modules\CountryList\app\Models\Country;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
|
|
class PackageRepository
|
|
{
|
|
//-- Retrieve all Packages
|
|
public function allPackages($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Package::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 Package by uuid
|
|
public function findPackageByUuid($uuid)
|
|
{
|
|
return Package::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
//-- Find Package by uuid
|
|
public function findPackageWithCountriesByUuid($uuid)
|
|
{
|
|
return Package::with('country')->where('uuid', $uuid)->first();
|
|
}
|
|
|
|
//-- Retrieve all countries
|
|
public function getAllCountries()
|
|
{
|
|
return Country::all();
|
|
}
|
|
|
|
//-- Store Package
|
|
public function storePackage(array $validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$package = new Package();
|
|
$package->uuid = Str::uuid();
|
|
$package->title = $validated['title'];
|
|
$package->description = $validated['description'];
|
|
$package->country_id = $validated['country_id'];
|
|
$package->price = $validated['price'];
|
|
$package->duration = $validated['duration'];
|
|
$package->group_size = $validated['group_size'];
|
|
$package->ordering = $validated['ordering'];
|
|
$package->save();
|
|
|
|
// Check if image is uploaded and valid and store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'packages',
|
|
model: $package
|
|
);
|
|
}
|
|
DB::commit();
|
|
|
|
return $package;
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
}
|
|
}
|
|
|
|
public function updatePackage(array $validated, string $uuid)
|
|
{
|
|
try {
|
|
$package = $this->findPackageByUuid($uuid);
|
|
if (!$package) {
|
|
return null;
|
|
}
|
|
|
|
$package->title = $validated['title'];
|
|
$package->description = $validated['description'];
|
|
$package->country_id = $validated['country_id'];
|
|
$package->price = $validated['price'];
|
|
$package->duration = $validated['duration'];
|
|
$package->group_size = $validated['group_size'];
|
|
$package->ordering = $validated['ordering'];
|
|
$package->save();
|
|
|
|
//-- Update image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'packages',
|
|
filePath: $package->image_path,
|
|
model: $package
|
|
);
|
|
}
|
|
|
|
return $package;
|
|
} catch (\Throwable $th) {
|
|
DB::transaction();
|
|
report($th);
|
|
}
|
|
}
|
|
|
|
//-- Delete package
|
|
public function deletePackage(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$package = $this->findPackageByUuid($uuid);
|
|
if (! $package) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the activity
|
|
if ($package->image_path !== null) {
|
|
FileManagementService::deleteFile($package->image_path);
|
|
}
|
|
$package->delete();
|
|
|
|
return $package;
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
}
|
|
}
|
|
}
|