192 lines
5.7 KiB
PHP
192 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Destination\app\Repositories;
|
|
|
|
use Throwable;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Activity\app\Models\Activity;
|
|
use Modules\CountryList\app\Models\Country;
|
|
use Modules\Destination\app\Models\Destination;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
|
|
class DestinationRepository
|
|
{
|
|
//-- Get All Destination
|
|
public function allDestinations($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Destination::with('country')
|
|
->withCount('activities')
|
|
->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 findDestinationById($uuid)
|
|
{
|
|
return Destination::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
//-- Store Destination
|
|
public function storeDestination($validated)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$destination = new Destination();
|
|
$destination->uuid = Str::uuid();
|
|
$destination->title = $validated['title'];
|
|
$destination->country_id = $validated['country_id'];
|
|
$destination->rating = $validated['rating'];
|
|
$destination->ordering = $validated['ordering'];
|
|
$destination->save();
|
|
|
|
//-- get activities
|
|
if (isset($validated['activities'][0])) {
|
|
$activityNames = json_decode($validated['activities'][0], true);
|
|
$activityIds = Activity::whereIn('name', $activityNames)
|
|
->pluck('id')
|
|
->toArray();
|
|
|
|
$destination->activities()->attach($activityIds);
|
|
}
|
|
|
|
//-- store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'banners',
|
|
model: $destination
|
|
);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $destination;
|
|
} catch (Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
}
|
|
}
|
|
|
|
//-- Update Destination
|
|
public function updateDestination($validated, $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$destination = $this->findDestinationById($uuid);
|
|
if (!$destination) {
|
|
return null;
|
|
}
|
|
|
|
$destination->title = $validated['title'];
|
|
$destination->country_id = $validated['country_id'];
|
|
$destination->rating = $validated['rating'];
|
|
$destination->ordering = $validated['ordering'];
|
|
$destination->save();
|
|
|
|
//-- update image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'destinations',
|
|
filePath: $destination->image_path,
|
|
model: $destination
|
|
);
|
|
}
|
|
|
|
//-- get activities
|
|
if (isset($validated['activities'][0])) {
|
|
$activityNames = json_decode($validated['activities'][0], true);
|
|
$activityIds = Activity::whereIn('name', $activityNames)
|
|
->pluck('id')
|
|
->toArray();
|
|
|
|
$destination->activities()->sync($activityIds);
|
|
}
|
|
return $destination;
|
|
} catch (Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//-- Delete Destination
|
|
public function deleteDestination($uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$destination = $this->findDestinationById($uuid);
|
|
|
|
if (!$destination) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the destination
|
|
if ($destination->image_path !== null) {
|
|
FileManagementService::deleteFile($destination->image_path);
|
|
}
|
|
|
|
$destination->delete();
|
|
DB::commit();
|
|
|
|
return true;
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
report($th);
|
|
|
|
}
|
|
}
|
|
|
|
//-- get countries
|
|
public function getCountries()
|
|
{
|
|
return Country::all();
|
|
}
|
|
|
|
//-- get activities
|
|
public function getActivities()
|
|
{
|
|
return Activity::all();
|
|
}
|
|
|
|
//-- get destination With Countries And Activities By Uuid
|
|
public function destinationWithCountriesAndActivitiesByUuid($uuid)
|
|
{
|
|
return Destination::with('country', 'activities')
|
|
->where('uuid', $uuid)
|
|
->first();
|
|
}
|
|
|
|
//-- Delete destination
|
|
public function destinationRepository(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$destination = $this->findDestinationById($uuid);
|
|
if (! $destination) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the activity
|
|
if ($destination->image_path !== null) {
|
|
FileManagementService::deleteFile($destination->image_path);
|
|
}
|
|
$destination->delete();
|
|
|
|
return $destination;
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
}
|
|
}
|
|
}
|