- Added `ConnectionProviderData`, `NavItemData`, and `NavSubItemData` DTOs for structured data handling. - Implemented CRUD operations for `ConnectionProviderService`, including transaction-safe create, update, and delete methods. - Introduced Livewire components for ConnectionProvider list, edit, and create pages with dynamic and reusable UI elements. - Refactored routes to consolidate paths under the `apps` prefix and updated navigation to dynamically include new sections for providers. - Enhanced sidebar with structured submenu for ConnectionProviders and Settings using dynamic collections. - Updated existing Create and Edit templates to use consistent route naming and validation handling. - Replaced hardcoded navigation items with dynamically generated structures leveraging the `NavItemData` collection.
95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Data\ConnectedApp\{ConnectionProviderData, StoreConnectionProviderRequestData};
|
|
use App\Models\ConnectionProvider;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use InvalidArgumentException;
|
|
use NoDiscard;
|
|
use Spatie\LaravelData\PaginatedDataCollection;
|
|
use Throwable;
|
|
|
|
class ConnectionProviderService
|
|
{
|
|
/**
|
|
* @return Collection<int, ConnectionProvider>
|
|
*/
|
|
#[NoDiscard]
|
|
public function search(string $value): Collection
|
|
{
|
|
return ConnectionProvider::query()
|
|
->whereLike('slug', "%{$value}%")
|
|
->orWhereLike('name', "%{$value}%")
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function create(StoreConnectionProviderRequestData $data): void
|
|
{
|
|
DB::transaction(
|
|
function () use ($data): void {
|
|
ConnectionProvider::query()->create($data->toArray());
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return PaginatedDataCollection<int, ConnectionProviderData>
|
|
*/
|
|
#[NoDiscard]
|
|
public function getAll(): PaginatedDataCollection
|
|
{
|
|
$data = ConnectionProvider::query()->select(['id', 'name', 'slug'])->paginate();
|
|
|
|
return ConnectionProviderData::collect($data, PaginatedDataCollection::class);
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidArgumentException when id is invalid
|
|
* @throws Throwable when an error occurs during the update.
|
|
*/
|
|
public function update(string $slug, StoreConnectionProviderRequestData $data): void
|
|
{
|
|
DB::transaction(
|
|
fn () => ConnectionProvider::query()
|
|
->where('slug', $slug)
|
|
->update($data->toArray())
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable when an error occurs during the deletion.
|
|
* @throws InvalidArgumentException when id is invalid
|
|
*/
|
|
public function delete(string $slug): void
|
|
{
|
|
DB::transaction(
|
|
fn () => ConnectionProvider::query()
|
|
->where('slug', $slug)
|
|
->delete()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws ModelNotFoundException<ConnectionProviderData> when the connected app is not found.
|
|
* @throws InvalidArgumentException when id is invalid.
|
|
*/
|
|
#[NoDiscard]
|
|
public function getProvider(string $slug): ConnectionProviderData
|
|
{
|
|
$data = ConnectionProvider::query()
|
|
->select(['id', 'name', 'slug'])
|
|
->where('slug', $slug)
|
|
->first();
|
|
|
|
return ConnectionProviderData::from($data);
|
|
}
|
|
}
|