Skip to content

Commit

Permalink
feat: add admin resource and api
Browse files Browse the repository at this point in the history
  • Loading branch information
daurensky committed Feb 21, 2024
1 parent a109b6c commit b61796b
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use AdminKit\Vacancy\Models\Vacancy;

return new class extends Migration
{
public function up()
{
Schema::create('admin_kit_vacancy_gallery', function (Blueprint $table) {
$table->id();

// add fields
$table->foreignId('vacancy_id')
->constrained('admin_kit_vacancy')
->cascadeOnDelete();
$table->jsonb('title');

$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('admin_kit_vacancy_gallerys');
}
};
5 changes: 4 additions & 1 deletion database/migrations/create_admin_kit_vacancy_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ return new class extends Migration
$table->id();

// add fields
$table->jsonb('title')->default('{}');
$table->jsonb('title');
$table->jsonb('subtitle')->nullable();
$table->jsonb('action_title')->nullable();
$table->jsonb('action_link')->nullable();

$table->timestamps();
});
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/en/vacancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

'id' => 'ID',
'title' => 'Title',
'subtitle' => 'Subtitle',
'image' => 'Image',
'action_button' => 'Action button',
'link' => 'Link',

'created_at' => 'Created At',
'updated_at' => 'Updated At',
Expand Down
10 changes: 7 additions & 3 deletions resources/lang/ru/vacancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

return [
'resource' => [
'label' => 'Vacancy',
'plural_label' => 'Vacancy',
'label' => 'Вакансия',
'plural_label' => 'Вакансии',

'id' => 'ID',
'title' => 'Title',
'title' => 'Заголовок',
'subtitle' => 'Подзаголовок',
'image' => 'Изображение',
'action_button' => 'Кнопка действия',
'link' => 'Ссылка',

'created_at' => 'Создан',
'updated_at' => 'Обновлен',
Expand Down
17 changes: 16 additions & 1 deletion src/Models/Vacancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

namespace AdminKit\Vacancy\Models;

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use AdminKit\Core\Abstracts\Models\AbstractModel;
use Illuminate\Database\Eloquent\Relations\HasMany;
use AdminKit\Vacancy\Database\Factories\VacancyFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Translatable\HasTranslations;

class Vacancy extends AbstractModel
class Vacancy extends AbstractModel implements HasMedia
{
use HasFactory;
use HasTranslations;
use InteractsWithMedia;

protected $table = 'admin_kit_vacancy';

protected $fillable = [
'title',
'subtitle',
'action_title',
'action_link',
];

protected $casts = [
Expand All @@ -24,10 +31,18 @@ class Vacancy extends AbstractModel

protected $translatable = [
'title',
'subtitle',
'action_title',
'action_link',
];

protected static function newFactory(): VacancyFactory
{
return new VacancyFactory();
}

public function gallery(): HasMany
{
return $this->hasMany(VacancyGallery::class);
}
}
36 changes: 36 additions & 0 deletions src/Models/VacancyGallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace AdminKit\Vacancy\Models;

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use AdminKit\Core\Abstracts\Models\AbstractModel;
use AdminKit\Vacancy\Database\Factories\VacancyFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Translatable\HasTranslations;

class VacancyGallery extends AbstractModel implements HasMedia
{
use HasFactory;
use HasTranslations;
use InteractsWithMedia;

protected $table = 'admin_kit_vacancy_gallery';

protected $fillable = [
'title',
];

protected $casts = [
//
];

protected $translatable = [
'title',
];

protected static function newFactory(): VacancyFactory
{
return new VacancyFactory();
}
}
11 changes: 5 additions & 6 deletions src/UI/API/Controllers/VacancyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace AdminKit\Vacancy\UI\API\Controllers;

use AdminKit\Vacancy\Models\Vacancy;
use AdminKit\Vacancy\UI\API\Resources\VacancyResource;

class VacancyController extends Controller
{
public function index()
public function showFirst(): VacancyResource
{
return Vacancy::all();
}
$vacancies = Vacancy::query()
->first();

public function show(int $id)
{
return Vacancy::findOrFail($id);
return new VacancyResource($vacancies);
}
}
22 changes: 22 additions & 0 deletions src/UI/API/Resources/VacancyGalleryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace AdminKit\Vacancy\UI\API\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class VacancyGalleryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'title' => $this->title,

Check failure on line 18 in src/UI/API/Resources/VacancyGalleryResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyGalleryResource::$title.
'background' => $this->getFirstMediaUrl('gallery'),

Check failure on line 19 in src/UI/API/Resources/VacancyGalleryResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AdminKit\Vacancy\UI\API\Resources\VacancyGalleryResource::getFirstMediaUrl().
];
}
}
28 changes: 28 additions & 0 deletions src/UI/API/Resources/VacancyResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AdminKit\Vacancy\UI\API\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class VacancyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'title' => $this->title,

Check failure on line 18 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyResource::$title.
'subtitle' => $this->subtitle,

Check failure on line 19 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyResource::$subtitle.
'background' => $this->getFirstMediaUrl('main'),

Check failure on line 20 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AdminKit\Vacancy\UI\API\Resources\VacancyResource::getFirstMediaUrl().
'action' => [
'title' => $this->action_title,

Check failure on line 22 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyResource::$action_title.
'link' => $this->action_link,

Check failure on line 23 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyResource::$action_link.
],
'gallery' => VacancyGalleryResource::collection($this->gallery),

Check failure on line 25 in src/UI/API/Resources/VacancyResource.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Vacancy\UI\API\Resources\VacancyResource::$gallery.
];
}
}
5 changes: 2 additions & 3 deletions src/UI/API/Routes/api.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use AdminKit\Vacancy\UI\API\Controllers\VacancyController;
use Illuminate\Support\Facades\Route;
use AdminKit\Vacancy\UI\API\Controllers\VacancyController;

Route::get('/vacancy', [VacancyController::class, 'index']);
Route::get('/vacancy/{id}', [VacancyController::class, 'show']);
Route::get('/vacancy', [VacancyController::class, 'showFirst']);
31 changes: 26 additions & 5 deletions src/UI/Filament/Resources/VacancyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use AdminKit\Vacancy\UI\Filament\Resources\VacancyResource\RelationManagers\VacancyGalleryRelationManager;

class VacancyResource extends Resource
{
Expand All @@ -19,10 +20,25 @@ public static function form(Forms\Form $form): Forms\Form
{
return $form
->schema([
Forms\Components\SpatieMediaLibraryFileUpload::make('background')
->label(__('admin-kit-vacancy::vacancy.resource.image'))
->collection('main')
->image()
->columnSpan(2)
->required(),
TranslatableTabs::make(fn ($locale) => Forms\Components\Tabs\Tab::make($locale)->schema([
Forms\Components\TextInput::make('title')
Forms\Components\TextInput::make('title.'.$locale)
->label(__('admin-kit-vacancy::vacancy.resource.title'))
->required($locale === app()->getLocale()),
->required(),
Forms\Components\TextInput::make('subtitle.'.$locale)
->label(__('admin-kit-vacancy::vacancy.resource.subtitle'))
->required(),
Forms\Components\Section::make(__('admin-kit-vacancy::vacancy.resource.action_button'))->schema([
Forms\Components\TextInput::make('action_title.'.$locale)
->label(__('admin-kit-vacancy::vacancy.resource.title')),
Forms\Components\TextInput::make('action_link.'.$locale)
->label(__('admin-kit-vacancy::vacancy.resource.link')),
])->columns(2),
])),
])
->columns(1);
Expand Down Expand Up @@ -57,7 +73,7 @@ public static function table(Tables\Table $table): Tables\Table
public static function getRelations(): array
{
return [
//
VacancyGalleryRelationManager::class,
];
}

Expand All @@ -80,8 +96,13 @@ public static function getPluralLabel(): ?string
return __('admin-kit-vacancy::vacancy.resource.plural_label');
}

public static function getNavigationGroup(): ?string
public static function getNavigationUrl(): string
{
return __('admin-kit-vacancy::vacancy.resource.plural_label');
$vacancy = Vacancy::query()
->first();

return $vacancy
? route('filament.admin-kit.resources.vacancies.edit', $vacancy)
: route('filament.admin-kit.resources.vacancies.create');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace AdminKit\Vacancy\UI\Filament\Resources\VacancyResource\RelationManagers;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Forms\Components\Tabs\Tab;
use AdminKit\Core\Forms\Components\TranslatableTabs;
use Filament\Resources\RelationManagers\RelationManager;

class VacancyGalleryRelationManager extends RelationManager
{
protected static string $relationship = 'gallery';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\SpatieMediaLibraryFileUpload::make('background')
->label(__('admin-kit-vacancy::vacancy.resource.image'))
->collection('gallery')
->image()
->columnSpan(2)
->required(),
TranslatableTabs::make(fn ($locale) => Tab::make($locale)->schema([
Forms\Components\TextInput::make('title.'.$locale)
->label(__('admin-kit-vacancy::vacancy.resource.title'))
->required(),
]))->columnSpan(2),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('title')
->columns([
Tables\Columns\SpatieMediaLibraryImageColumn::make('background')
->collection('gallery')
->label(__('admin-kit-vacancy::vacancy.resource.image')),
Tables\Columns\TextColumn::make('title')
->label(__('admin-kit-vacancy::vacancy.resource.title')),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
5 changes: 4 additions & 1 deletion src/VacancyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasViews()
->hasTranslations()
->hasMigration('create_admin_kit_vacancy_table')
->hasMigrations([
'create_admin_kit_vacancy_table',
'create_admin_kit_vacancy_gallery_table',
])
->hasCommand(VacancyCommand::class);
}

Expand Down

0 comments on commit b61796b

Please sign in to comment.