Add Destination resource (T2.2)
Migration/model/factory for destinations (name, mm_name, region, description fields, geo coordinates, is_active, popular) plus its Filament resource.
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<Destination>
|
||||||
|
*/
|
||||||
|
class DestinationFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->city(),
|
||||||
|
'mm_name' => fake()->city(),
|
||||||
|
'region' => fake()->state(),
|
||||||
|
'description' => fake()->sentence(),
|
||||||
|
'mm_description' => null,
|
||||||
|
'meta_keyword' => null,
|
||||||
|
'meta_description' => null,
|
||||||
|
'latitude' => fake()->latitude(),
|
||||||
|
'longitude' => fake()->longitude(),
|
||||||
|
'is_active' => true,
|
||||||
|
'popular' => false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('destinations', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('mm_name');
|
||||||
|
$table->string('region')->nullable();
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->text('mm_description')->nullable();
|
||||||
|
$table->text('meta_keyword')->nullable();
|
||||||
|
$table->text('meta_description')->nullable();
|
||||||
|
$table->string('latitude')->nullable();
|
||||||
|
$table->string('longitude')->nullable();
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->boolean('popular')->default(false);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('destinations');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations;
|
||||||
|
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\CreateDestination;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\EditDestination;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\ListDestinations;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Schemas\DestinationForm;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Tables\DestinationsTable;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class DestinationResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = Destination::class;
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return DestinationForm::configure($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return DestinationsTable::configure($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRelations(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListDestinations::route('/'),
|
||||||
|
'create' => CreateDestination::route('/create'),
|
||||||
|
'edit' => EditDestination::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations\Pages;
|
||||||
|
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\DestinationResource;
|
||||||
|
|
||||||
|
class CreateDestination extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DestinationResource::class;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\DeleteAction;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\DestinationResource;
|
||||||
|
|
||||||
|
class EditDestination extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DestinationResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
DeleteAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\CreateAction;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\DestinationResource;
|
||||||
|
|
||||||
|
class ListDestinations extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = DestinationResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations\Schemas;
|
||||||
|
|
||||||
|
use Filament\Forms\Components\Textarea;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Components\Toggle;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
|
||||||
|
class DestinationForm
|
||||||
|
{
|
||||||
|
public static function configure(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('name')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('mm_name')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('region')
|
||||||
|
->maxLength(255),
|
||||||
|
Textarea::make('description')
|
||||||
|
->columnSpanFull(),
|
||||||
|
Textarea::make('mm_description')
|
||||||
|
->columnSpanFull(),
|
||||||
|
Textarea::make('meta_keyword')
|
||||||
|
->columnSpanFull(),
|
||||||
|
Textarea::make('meta_description')
|
||||||
|
->columnSpanFull(),
|
||||||
|
TextInput::make('latitude')
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('longitude')
|
||||||
|
->maxLength(255),
|
||||||
|
Toggle::make('is_active')
|
||||||
|
->required()
|
||||||
|
->default(true),
|
||||||
|
Toggle::make('popular')
|
||||||
|
->required()
|
||||||
|
->default(false),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\Destinations\Tables;
|
||||||
|
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Actions\DeleteBulkAction;
|
||||||
|
use Filament\Actions\EditAction;
|
||||||
|
use Filament\Tables\Columns\IconColumn;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\TernaryFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
|
||||||
|
class DestinationsTable
|
||||||
|
{
|
||||||
|
public static function configure(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('name')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('region')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
IconColumn::make('is_active')
|
||||||
|
->boolean(),
|
||||||
|
IconColumn::make('popular')
|
||||||
|
->boolean(),
|
||||||
|
TextColumn::make('created_at')
|
||||||
|
->dateTime()
|
||||||
|
->sortable()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
])
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->filters([
|
||||||
|
TernaryFilter::make('is_active'),
|
||||||
|
TernaryFilter::make('popular'),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
DeleteBulkAction::make(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Modules\Catalog\Database\Factories\DestinationFactory;
|
||||||
|
|
||||||
|
class Destination extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<DestinationFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'mm_name',
|
||||||
|
'region',
|
||||||
|
'description',
|
||||||
|
'mm_description',
|
||||||
|
'meta_keyword',
|
||||||
|
'meta_description',
|
||||||
|
'latitude',
|
||||||
|
'longitude',
|
||||||
|
'is_active',
|
||||||
|
'popular',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
'popular' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\CreateDestination;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\EditDestination;
|
||||||
|
use Modules\Catalog\Filament\Resources\Destinations\Pages\ListDestinations;
|
||||||
|
use Modules\Catalog\Models\Destination;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
|
use function Pest\Laravel\assertDatabaseHas;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Role::findOrCreate('admin', 'web');
|
||||||
|
|
||||||
|
$this->admin = User::factory()->create()->assignRole('admin');
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can list destinations', function () {
|
||||||
|
$destinations = Destination::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListDestinations::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($destinations);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can create a destination', function () {
|
||||||
|
$destination = Destination::factory()->make();
|
||||||
|
|
||||||
|
Livewire::test(CreateDestination::class)
|
||||||
|
->fillForm([
|
||||||
|
'name' => $destination->name,
|
||||||
|
'mm_name' => $destination->mm_name,
|
||||||
|
'region' => $destination->region,
|
||||||
|
'is_active' => true,
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
assertDatabaseHas(Destination::class, [
|
||||||
|
'name' => $destination->name,
|
||||||
|
'region' => $destination->region,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can edit a destination', function () {
|
||||||
|
$destination = Destination::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(EditDestination::class, ['record' => $destination->getRouteKey()])
|
||||||
|
->assertOk()
|
||||||
|
->fillForm(['name' => 'Updated Name'])
|
||||||
|
->call('save')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
assertDatabaseHas(Destination::class, [
|
||||||
|
'id' => $destination->id,
|
||||||
|
'name' => 'Updated Name',
|
||||||
|
]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user