From 7888182b997f02e42a24284d670c4e2b5248cb0d Mon Sep 17 00:00:00 2001 From: daurensky Date: Tue, 12 Mar 2024 13:51:53 +0500 Subject: [PATCH] feat: add filament resource, http api --- ...ate_admin_kit_entry_screens_table.php.stub | 3 +- resources/lang/en/entry-screens.php | 5 ++-- resources/lang/ru/entry-screens.php | 7 +++-- src/Models/EntryScreen.php | 28 ++++++++++++++----- .../API/Controllers/EntryScreenController.php | 11 ++++---- src/UI/API/DTO/EntryScreenDTO.php | 26 +++++++++++++++++ src/UI/API/Routes/api.php | 3 +- .../Resources/EntryScreenResource.php | 20 +++++++------ 8 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 src/UI/API/DTO/EntryScreenDTO.php diff --git a/database/migrations/create_admin_kit_entry_screens_table.php.stub b/database/migrations/create_admin_kit_entry_screens_table.php.stub index f1a9195..888fe71 100644 --- a/database/migrations/create_admin_kit_entry_screens_table.php.stub +++ b/database/migrations/create_admin_kit_entry_screens_table.php.stub @@ -12,7 +12,8 @@ return new class extends Migration $table->id(); // add fields - $table->jsonb('title')->default('{}'); + $table->jsonb('title'); + $table->jsonb('subtitle')->nullable(); $table->timestamps(); }); diff --git a/resources/lang/en/entry-screens.php b/resources/lang/en/entry-screens.php index 05b53de..9ace213 100644 --- a/resources/lang/en/entry-screens.php +++ b/resources/lang/en/entry-screens.php @@ -2,11 +2,12 @@ return [ 'resource' => [ - 'label' => 'EntryScreen', - 'plural_label' => 'EntryScreens', + 'label' => 'Entry screen', + 'plural_label' => 'Entry screens', 'id' => 'ID', 'title' => 'Title', + 'subtitle' => 'Subtitle', 'created_at' => 'Created At', 'updated_at' => 'Updated At', diff --git a/resources/lang/ru/entry-screens.php b/resources/lang/ru/entry-screens.php index 538ad91..525833a 100644 --- a/resources/lang/ru/entry-screens.php +++ b/resources/lang/ru/entry-screens.php @@ -2,11 +2,12 @@ return [ 'resource' => [ - 'label' => 'EntryScreen', - 'plural_label' => 'EntryScreens', + 'label' => 'Вступительный экран', + 'plural_label' => 'Вступительные экраны', 'id' => 'ID', - 'title' => 'Title', + 'title' => 'Заголовок', + 'subtitle' => 'Подзаголовок', 'created_at' => 'Создан', 'updated_at' => 'Обновлен', diff --git a/src/Models/EntryScreen.php b/src/Models/EntryScreen.php index 286ba47..5740e9b 100644 --- a/src/Models/EntryScreen.php +++ b/src/Models/EntryScreen.php @@ -2,30 +2,44 @@ namespace AdminKit\EntryScreens\Models; +use Spatie\MediaLibrary\HasMedia; +use Spatie\Translatable\HasTranslations; +use Spatie\MediaLibrary\InteractsWithMedia; +use Illuminate\Database\Eloquent\Casts\Attribute; use AdminKit\Core\Abstracts\Models\AbstractModel; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Spatie\Translatable\HasTranslations; use AdminKit\EntryScreens\Database\Factories\EntryScreenFactory; -class EntryScreen extends AbstractModel +/** + * @property-read string $title + * @property-read string $background + * @property-read ?string $subtitle + */ +class EntryScreen extends AbstractModel implements HasMedia { use HasFactory; use HasTranslations; + use InteractsWithMedia; protected $table = 'admin_kit_entry_screens'; protected $fillable = [ 'title', + 'subtitle', ]; - protected $casts = [ - // - ]; - - protected $translatable = [ + protected array $translatable = [ 'title', + 'subtitle', ]; + public function background(): Attribute + { + return new Attribute( + get: fn () => $this->getFirstMediaUrl(), + ); + } + protected static function newFactory(): EntryScreenFactory { return new EntryScreenFactory(); diff --git a/src/UI/API/Controllers/EntryScreenController.php b/src/UI/API/Controllers/EntryScreenController.php index faf4935..c210c33 100644 --- a/src/UI/API/Controllers/EntryScreenController.php +++ b/src/UI/API/Controllers/EntryScreenController.php @@ -5,16 +5,15 @@ namespace AdminKit\EntryScreens\UI\API\Controllers; use AdminKit\EntryScreens\Models\EntryScreen; +use AdminKit\EntryScreens\UI\API\DTO\EntryScreenDTO; class EntryScreenController extends Controller { - public function index() + public function showFirst(): EntryScreenDTO { - return EntryScreen::all(); - } + $entryScreen = EntryScreen::query() + ->firstOrFail(); - public function show(int $id) - { - return EntryScreen::findOrFail($id); + return EntryScreenDTO::from($entryScreen); } } diff --git a/src/UI/API/DTO/EntryScreenDTO.php b/src/UI/API/DTO/EntryScreenDTO.php new file mode 100644 index 0000000..f540332 --- /dev/null +++ b/src/UI/API/DTO/EntryScreenDTO.php @@ -0,0 +1,26 @@ +title, + background: $entryScreen->background, + subtitle: $entryScreen->subtitle, + ); + } +} diff --git a/src/UI/API/Routes/api.php b/src/UI/API/Routes/api.php index 5d74c90..e4a21d8 100644 --- a/src/UI/API/Routes/api.php +++ b/src/UI/API/Routes/api.php @@ -3,5 +3,4 @@ use Illuminate\Support\Facades\Route; use AdminKit\EntryScreens\UI\API\Controllers\EntryScreenController; -Route::get('/entry-screens', [EntryScreenController::class, 'index']); -Route::get('/entry-screens/{id}', [EntryScreenController::class, 'show']); +Route::get('/entry-screen', [EntryScreenController::class, 'showFirst']); diff --git a/src/UI/Filament/Resources/EntryScreenResource.php b/src/UI/Filament/Resources/EntryScreenResource.php index 1043276..1474c2a 100644 --- a/src/UI/Filament/Resources/EntryScreenResource.php +++ b/src/UI/Filament/Resources/EntryScreenResource.php @@ -2,6 +2,7 @@ namespace AdminKit\EntryScreens\UI\Filament\Resources; +use Filament\Forms\Components\Tabs\Tab; use AdminKit\Core\Forms\Components\TranslatableTabs; use Filament\Forms; use Filament\Resources\Resource; @@ -19,11 +20,17 @@ public static function form(Forms\Form $form): Forms\Form { return $form ->schema([ - TranslatableTabs::make(fn ($locale) => Forms\Components\Tabs\Tab::make($locale)->schema([ - Forms\Components\TextInput::make('title') + Forms\Components\SpatieMediaLibraryFileUpload::make('background') + ->label('Фон') + ->required() + ->columnSpan(2), + TranslatableTabs::make(fn ($locale) => Tab::make($locale)->schema([ + Forms\Components\TextInput::make('title.'.$locale) ->label(__('admin-kit-entry-screens::entry-screens.resource.title')) - ->required($locale === app()->getLocale()), - ])), + ->required(), + Forms\Components\TextInput::make('subtitle.'.$locale) + ->label(__('admin-kit-entry-screens::entry-screens.resource.subtitle')), + ]))->columnSpan(2), ]) ->columns(1); } @@ -79,9 +86,4 @@ public static function getPluralLabel(): ?string { return __('admin-kit-entry-screens::entry-screens.resource.plural_label'); } - - public static function getNavigationGroup(): ?string - { - return __('admin-kit-entry-screens::entry-screens.resource.plural_label'); - } }