Skip to content

Commit

Permalink
feat: add categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ast21 committed Nov 14, 2024
1 parent ce540f8 commit 34300e8
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('admin_kit_documents', function (Blueprint $table) {
$table->unsignedBigInteger('category_id')->nullable();
});
}

public function down()
{
Schema::table('admin_kit_documents', function (Blueprint $table) {
$table->dropColumn('category_id');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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

$table->json('title')->default('{}');
$table->integer('sort')->nullable();

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

public function down()
{
Schema::dropIfExists('admin_kit_document_categories');
}
};
2 changes: 2 additions & 0 deletions src/DocumentsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function configurePackage(Package $package): void
->hasMigrations([
'create_admin_kit_documents_table',
'add_link_and_published_at_columns_to_admin_kit_documents_table',
'create_admin_kit_document_categories_table',
'add_category_id_column_to_admin_kit_documents_table',
])
->hasCommand(DocumentsCommand::class);
}
Expand Down
2 changes: 2 additions & 0 deletions src/FilamentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AdminKit\Documents;

use AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource;
use AdminKit\Documents\UI\Filament\Resources\DocumentResource;
use Filament\Contracts\Plugin;
use Filament\Panel;
Expand All @@ -17,6 +18,7 @@ public function register(Panel $panel): void
{
$panel->resources([
DocumentResource::class,
DocumentCategoryResource::class,
]);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Translatable\HasTranslations;
Expand All @@ -29,6 +30,11 @@ class Document extends AbstractModel implements HasMedia
'title',
];

public function category(): BelongsTo
{
return $this->belongsTo(DocumentCategory::class);
}

public function scopeYear(Builder $query, $year): Builder
{
$date = Carbon::createFromDate($year);
Expand All @@ -38,6 +44,12 @@ public function scopeYear(Builder $query, $year): Builder
->where('published_at', '<=', $date->copy()->endOfYear());
}

public function scopeCategoryId(Builder $query, $categoryId): Builder
{
return $query
->whereHas('category', fn ($query) => $query->where('id', $categoryId));
}

protected static function newFactory(): DocumentFactory
{
return new DocumentFactory();
Expand Down
21 changes: 21 additions & 0 deletions src/Models/DocumentCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AdminKit\Documents\Models;

use AdminKit\Core\Abstracts\Models\AbstractModel;
use Spatie\Translatable\HasTranslations;

class DocumentCategory extends AbstractModel
{
use HasTranslations;

protected $table = 'admin_kit_document_categories';

protected $fillable = [
'title',
];

protected $translatable = [
'title',
];
}
15 changes: 15 additions & 0 deletions src/UI/API/Controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AdminKit\Documents\UI\API\Controllers;

use AdminKit\Documents\Models\Document;
use AdminKit\Documents\Models\DocumentCategory;
use AdminKit\Documents\UI\API\Data\DocumentData;
use Illuminate\Http\JsonResponse;
use Spatie\LaravelData\PaginatedDataCollection;
Expand All @@ -18,6 +19,7 @@ public function index(): PaginatedDataCollection
$documents = QueryBuilder::for(Document::class)
->allowedFilters([
AllowedFilter::scope('year'),
AllowedFilter::scope('category_id'),
])
->paginate();

Expand All @@ -35,4 +37,17 @@ public function years(): JsonResponse

return response()->json($years);
}

public function categories(): JsonResponse
{
$locale = app()->getLocale();

$categories = DocumentCategory::query()
->selectRaw("id, title->'$locale' as title")
->orderBy('sort')
->get()
->toArray();

return response()->json($categories);
}
}
2 changes: 2 additions & 0 deletions src/UI/API/Data/DocumentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
public ?string $link,
public ?array $file,
public ?string $publishedAt,
public ?array $category,
) {}

public static function fromModel(Document $document): DocumentData
Expand All @@ -33,6 +34,7 @@ public static function fromModel(Document $document): DocumentData
'size' => $media->size,
] : null,
publishedAt: $document->published_at,

Check failure on line 36 in src/UI/API/Data/DocumentData.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Documents\Models\Document::$published_at.
category: $document->category->only('id', 'title'),
);
}
}
1 change: 1 addition & 0 deletions src/UI/API/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

Route::get('/documents', [DocumentController::class, 'index']);
Route::get('/documents/years', [DocumentController::class, 'years']);
Route::get('/documents/categories', [DocumentController::class, 'categories']);
98 changes: 98 additions & 0 deletions src/UI/Filament/Resources/DocumentCategoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace AdminKit\Documents\UI\Filament\Resources;

use AdminKit\Core\Forms\Components\TranslatableTabs;
use AdminKit\Documents\Models\DocumentCategory;
use AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource\Pages;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class DocumentCategoryResource extends Resource
{
protected static ?string $model = DocumentCategory::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function getModelLabel(): string
{
return 'Категория документов';
}

public static function getPluralModelLabel(): string
{
return 'Категории документов';
}

public static function getNavigationGroup(): ?string
{
return 'Документы';
}

public static function getTitleCaseModelLabel(): string
{
return self::getModelLabel();
}

public static function getTitleCasePluralModelLabel(): string
{
return self::getPluralModelLabel();
}

public static function form(Form $form): Form
{
return $form
->schema([
TranslatableTabs::make(fn ($locale) => [
TextInput::make("title.$locale")
->label('Наименование')
->required($locale === app()->getLocale()),
])
->columnSpan(2),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title')
->label('Наименование')
->limit(30),
Tables\Columns\TextColumn::make('sort')
->label('Порядок'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->reorderable('sort')
->defaultSort('sort');
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListDocumentCategory::route('/'),
'create' => Pages\CreateDocumentCategory::route('/create'),
'edit' => Pages\EditDocumentCategory::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource\Pages;

use AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource;
use Filament\Resources\Pages\CreateRecord;

class CreateDocumentCategory extends CreateRecord
{
protected static string $resource = DocumentCategoryResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource\Pages;

use AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditDocumentCategory extends EditRecord
{
protected static string $resource = DocumentCategoryResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource\Pages;

use AdminKit\Documents\UI\Filament\Resources\DocumentCategoryResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListDocumentCategory extends ListRecords
{
protected static string $resource = DocumentCategoryResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
Loading

0 comments on commit 34300e8

Please sign in to comment.