92 lines
3.0 KiB
PHP
92 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Folder;
|
|
use App\Models\Todo;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class DatabaseSeeder extends Seeder
|
|
{
|
|
use WithoutModelEvents;
|
|
|
|
/**
|
|
* Seed the application's database.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$this->call(BaseDatabaseSeeder::class);
|
|
|
|
$user = User::factory()->create([
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'is_admin' => true,
|
|
]);
|
|
|
|
$work = $this->createTopLevelFolder($user, 'Work');
|
|
$business = $this->createTopLevelFolder($user, 'Business');
|
|
$personal = $this->createTopLevelFolder($user, 'Personal');
|
|
|
|
$workCompanies = [
|
|
$this->createChildFolder($work, 'Acme Corp'),
|
|
$this->createChildFolder($work, 'Globex'),
|
|
];
|
|
|
|
$businessTypes = [
|
|
$this->createChildFolder($business, 'Invoices'),
|
|
$this->createChildFolder($business, 'Prospects'),
|
|
];
|
|
|
|
$personalTypes = [
|
|
$this->createChildFolder($personal, 'Health'),
|
|
$this->createChildFolder($personal, 'Home'),
|
|
];
|
|
|
|
$folders = collect([$work, $business, $personal])
|
|
->merge($workCompanies)
|
|
->merge($businessTypes)
|
|
->merge($personalTypes);
|
|
|
|
$samples = [
|
|
['Work out the Q4 roadmap', 'Acme Corp', 0, 'open'],
|
|
['Review Globex contract', 'Globex', 2, 'overdue'],
|
|
['Send invoice for consulting work', 'Invoices', 1, 'due-today'],
|
|
['Follow up on new prospect', 'Prospects', 5, 'open'],
|
|
['Book dentist appointment', 'Health', -1, 'overdue'],
|
|
['Fix the garage door', 'Home', 7, 'open'],
|
|
['Plan summer vacation', 'Personal', null, 'open'],
|
|
['Consolidate expense reports', 'Business', 10, 'completed'],
|
|
];
|
|
|
|
$foldersByName = $folders->keyBy(fn (Folder $folder) => $folder->name);
|
|
|
|
foreach ($samples as [$description, $folderName, $daysFromToday, $status]) {
|
|
$todo = Todo::factory()->recycle($user)->create([
|
|
'folder_id' => $foldersByName[$folderName]->id,
|
|
'description' => $description,
|
|
'due_date' => $daysFromToday === null ? null : today()->addDays($daysFromToday),
|
|
]);
|
|
|
|
if ($status === 'overdue') {
|
|
$todo->update(['due_date' => today()->subDays(3)]);
|
|
}
|
|
|
|
if ($status === 'completed') {
|
|
$todo->update(['completed_at' => now()->subDay()]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function createTopLevelFolder(User $user, string $name): Folder
|
|
{
|
|
return Folder::factory()->recycle($user)->create(['name' => $name]);
|
|
}
|
|
|
|
private function createChildFolder(Folder $parent, string $name): Folder
|
|
{
|
|
return Folder::factory()->childOf($parent)->create(['name' => $name]);
|
|
}
|
|
}
|