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 bca3ed1 commit 5ca11e3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 37 deletions.
4 changes: 3 additions & 1 deletion database/migrations/create_admin_kit_products_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ return new class extends Migration
$table->id();

// add fields
$table->jsonb('title')->default('{}');
$table->jsonb('title');
$table->jsonb('text');
$table->integer('sort')->nullable();

$table->timestamps();
});
Expand Down
5 changes: 4 additions & 1 deletion resources/lang/en/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
'plural_label' => 'Products',

'id' => 'ID',
'title' => 'Title',
'photo' => 'Photo',
'name' => 'Name',
'text' => 'Text',
'attachments' => 'Attachments',

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

return [
'resource' => [
'label' => 'Product',
'plural_label' => 'Products',
'label' => 'Продукт',
'plural_label' => 'Продукция',

'id' => 'ID',
'title' => 'Title',
'photo' => 'Фото',
'name' => 'Название',
'text' => 'Текст',
'attachments' => 'Вложения',

'created_at' => 'Создан',
'updated_at' => 'Обновлен',
Expand Down
43 changes: 35 additions & 8 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@

namespace AdminKit\Products\Models;

use Spatie\MediaLibrary\HasMedia;
use Illuminate\Support\Collection;
use Spatie\Translatable\HasTranslations;
use Spatie\MediaLibrary\InteractsWithMedia;
use AdminKit\Core\Abstracts\Models\AbstractModel;
use AdminKit\Products\Database\Factories\ProductFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Translatable\HasTranslations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use AdminKit\Products\Database\Factories\ProductFactory;

class Product extends AbstractModel
/**
* @property-read string $title
* @property-read string $text
* @property-read string $photo
* @property-read Collection $attachments
* @property-read ?int $sort
*/
class Product extends AbstractModel implements HasMedia
{
use HasFactory;
use HasTranslations;
use InteractsWithMedia;

protected $table = 'admin_kit_products';

protected $fillable = [
'title',
'text',
'sort',
];

protected $casts = [
//
];

protected $translatable = [
protected array $translatable = [
'title',
'text',
];

public function photo(): Attribute
{
return new Attribute(
get: fn () => $this->getFirstMediaUrl('photo')
);
}

public function attachments(): Attribute
{
return new Attribute(
get: fn () => $this->getMedia('attachments.'.app()->getLocale())
->map(fn (Media $media) => $media->getFullUrl())
);
}

protected static function newFactory(): ProductFactory
{
return new ProductFactory();
Expand Down
13 changes: 7 additions & 6 deletions src/UI/API/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
namespace AdminKit\Products\UI\API\Controllers;

use AdminKit\Products\Models\Product;
use AdminKit\Products\UI\API\DTO\ProductDTO;
use Spatie\LaravelData\PaginatedDataCollection;

class ProductController extends Controller
{
public function index()
public function index(): PaginatedDataCollection
{
return Product::all();
}
$products = Product::query()
->orderBy('sort')
->paginate();

public function show(int $id)
{
return Product::findOrFail($id);
return ProductDTO::collection($products);
}
}
29 changes: 29 additions & 0 deletions src/UI/API/DTO/ProductDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace AdminKit\Products\UI\API\DTO;

use Spatie\LaravelData\Data;
use Illuminate\Support\Collection;
use AdminKit\Products\Models\Product;

class ProductDTO extends Data
{
public function __construct(
public string $title,
public string $text,
public string $photo,
public Collection $attachments,
)
{
}

public static function fromModel(Product $product): ProductDTO
{
return new self(
title: $product->title,
text: $product->text,
photo: $product->photo,
attachments: $product->attachments,
);
}
}
48 changes: 30 additions & 18 deletions src/UI/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace AdminKit\Products\UI\Filament\Resources;

use AdminKit\Core\Forms\Components\TranslatableTabs;
use AdminKit\Products\Models\Product;
use AdminKit\Products\UI\Filament\Resources\ProductResource\Pages;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Resources\Resource;
use AdminKit\Products\Models\Product;
use Filament\Forms\Components\Tabs\Tab;
use AdminKit\Core\Forms\Components\TranslatableTabs;
use AdminKit\Products\UI\Filament\Resources\ProductResource\Pages;

class ProductResource extends Resource
{
Expand All @@ -19,10 +20,21 @@ 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')
->label(__('admin-kit-products::products.resource.title'))
->required($locale === app()->getLocale()),
Forms\Components\SpatieMediaLibraryFileUpload::make('photo')
->label(__('admin-kit-products::products.resource.photo'))
->collection('photo')
->required(),
TranslatableTabs::make(fn ($locale) => Tab::make($locale)->schema([
Forms\Components\TextInput::make('title.'.$locale)
->label(__('admin-kit-products::products.resource.name'))
->required(),
Forms\Components\RichEditor::make('text.'.$locale)
->label(__('admin-kit-products::products.resource.text'))
->required(),
Forms\Components\SpatieMediaLibraryFileUpload::make('attachments.'.$locale)
->label(__('admin-kit-products::products.resource.attachments'))
->collection('attachments.'.$locale)
->multiple(),
])),
])
->columns(1);
Expand All @@ -33,14 +45,18 @@ public static function table(Tables\Table $table): Tables\Table
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label(__('admin-kit-products::products.resource.id'))
->sortable(),
->label(__('admin-kit-products::products.resource.id')),
Tables\Columns\SpatieMediaLibraryImageColumn::make('photo')
->label(__('admin-kit-products::products.resource.photo'))
->width(50)
->height(50)
->circular(),
Tables\Columns\TextColumn::make('title')
->label(__('admin-kit-products::products.resource.title')),
->label(__('admin-kit-products::products.resource.name'))
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->label(__('admin-kit-products::products.resource.created_at')),
])
->defaultSort('id', 'desc')
->filters([
//
])
Expand All @@ -51,7 +67,8 @@ public static function table(Tables\Table $table): Tables\Table
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
])
->defaultSort('id', 'desc');
->reorderable('sort')
->defaultSort('sort');
}

public static function getRelations(): array
Expand Down Expand Up @@ -79,9 +96,4 @@ public static function getPluralLabel(): ?string
{
return __('admin-kit-products::products.resource.plural_label');
}

public static function getNavigationGroup(): ?string
{
return __('admin-kit-products::products.resource.plural_label');
}
}

0 comments on commit 5ca11e3

Please sign in to comment.