Skip to content

Commit

Permalink
feat: add filament resource, http api
Browse files Browse the repository at this point in the history
  • Loading branch information
daurensky committed Mar 12, 2024
1 parent 051987c commit 7888182
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
5 changes: 3 additions & 2 deletions resources/lang/en/entry-screens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 4 additions & 3 deletions resources/lang/ru/entry-screens.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

return [
'resource' => [
'label' => 'EntryScreen',
'plural_label' => 'EntryScreens',
'label' => 'Вступительный экран',
'plural_label' => 'Вступительные экраны',

'id' => 'ID',
'title' => 'Title',
'title' => 'Заголовок',
'subtitle' => 'Подзаголовок',

'created_at' => 'Создан',
'updated_at' => 'Обновлен',
Expand Down
28 changes: 21 additions & 7 deletions src/Models/EntryScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions src/UI/API/Controllers/EntryScreenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 26 additions & 0 deletions src/UI/API/DTO/EntryScreenDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace AdminKit\EntryScreens\UI\API\DTO;

use Spatie\LaravelData\Data;
use AdminKit\EntryScreens\Models\EntryScreen;

class EntryScreenDTO extends Data
{
public function __construct(
public string $title,
public string $background,
public ?string $subtitle,
)
{
}

public static function fromModel(EntryScreen $entryScreen): EntryScreenDTO
{
return new self(
title: $entryScreen->title,
background: $entryScreen->background,
subtitle: $entryScreen->subtitle,
);
}
}
3 changes: 1 addition & 2 deletions src/UI/API/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
20 changes: 11 additions & 9 deletions src/UI/Filament/Resources/EntryScreenResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 7888182

Please sign in to comment.