127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Client\app\Repositories;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
use Modules\Client\app\Models\Client;
|
|
|
|
class ClientRepository
|
|
{
|
|
//-- Retrieve all Services
|
|
public function allClients($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Client::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 Client by uuid
|
|
public function findClientByUuid($uuid)
|
|
{
|
|
return Client::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
public function storeClient(array $validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
|
|
$client = new Client();
|
|
$client->uuid = Str::uuid();
|
|
$client->name = $validated['name'];
|
|
$client->link = $validated['link'];
|
|
$client->status = $validated['status'];
|
|
$client->save();
|
|
|
|
//-- store image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'clients',
|
|
model: $client
|
|
);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $client;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollback();
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function updateClient($validated, $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$client = $this->findClientByUuid($uuid);
|
|
|
|
if (! $client) {
|
|
return null;
|
|
}
|
|
|
|
$client->name = $validated['name'];
|
|
$client->link = $validated['link'];
|
|
$client->status = $validated['status'];
|
|
$client->save();
|
|
|
|
//-- update image
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'clients',
|
|
filePath: $client->image_path,
|
|
model: $client
|
|
);
|
|
}
|
|
DB::commit();
|
|
|
|
return $client;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollBack();
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//-- Delete Client
|
|
public function deleteClient(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$client = $this->findClientByUuid($uuid);
|
|
if (! $client) {
|
|
return null;
|
|
}
|
|
// Delete the image file associated with the Client
|
|
if ($client->image_path !== null) {
|
|
FileManagementService::deleteFile($client->image_path);
|
|
}
|
|
|
|
$client->delete();
|
|
|
|
DB::commit();
|
|
|
|
return $client;
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|