Added basic models and factories

This commit is contained in:
Constantin Graf
2024-01-21 18:35:29 +01:00
parent 28f7b43577
commit 6377df1f44
29 changed files with 1476 additions and 116 deletions

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Client;
use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Client>
*/
class ClientFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->company(),
'organization_id' => Team::factory(),
];
}
public function forOrganization(Team $organization): self
{
return $this->state(function (array $attributes) use ($organization) {
return [
'organization_id' => $organization->getKey(),
];
});
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Client;
use App\Models\Project;
use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Project>
*/
class ProjectFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->company(),
'color' => $this->faker->hexColor(),
'organization_id' => Team::factory(),
'client_id' => null,
];
}
public function forOrganization(Team $organization): self
{
return $this->state(function (array $attributes) use ($organization) {
return [
'organization_id' => $organization->getKey(),
];
});
}
public function forClient(?Client $client): self
{
return $this->state(function (array $attributes) use ($client) {
return [
'client_id' => $client?->getKey(),
];
});
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Tag;
use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Tag>
*/
class TagFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->name,
'organization_id' => Team::factory(),
];
}
public function forOrganization(Team $organization): self
{
return $this->state(function (array $attributes) use ($organization) {
return [
'organization_id' => $organization->getKey(),
];
});
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Task>
*/
class TaskFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->word(),
'project_id' => Project::factory(),
'organization_id' => Team::factory(),
];
}
public function forProject(Project $project): self
{
return $this->state(function (array $attributes) use ($project) {
return [
'project_id' => $project->getKey(),
];
});
}
public function forOrganization(Team $organization): self
{
return $this->state(function (array $attributes) use ($organization) {
return [
'organization_id' => $organization->getKey(),
];
});
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<TimeEntry>
*/
class TimeEntryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$start = $this->faker->dateTimeBetween('-1 year', '-1 hour');
return [
'description' => $this->faker->sentence(),
'start' => $start,
'end' => $this->faker->dateTimeBetween($start, 'now'),
'billable' => $this->faker->boolean(),
'tags' => [],
'user_id' => User::factory(),
'organization_id' => Team::factory(),
];
}
public function forUser(User $user): self
{
return $this->state(function (array $attributes) use ($user) {
return [
'user_id' => $user->getKey(),
];
});
}
public function forOrganization(Team $organization): self
{
return $this->state(function (array $attributes) use ($organization) {
return [
'organization_id' => $organization->getKey(),
];
});
}
public function forProject(?Project $project): self
{
return $this->state(function (array $attributes) use ($project) {
return [
'project_id' => $project?->getKey(),
];
});
}
public function forTask(?Task $task): self
{
return $this->state(function (array $attributes) use ($task) {
return [
'task_id' => $task?->getKey(),
];
});
}
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
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('clients', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name', 255);
$table->uuid('organization_id');
$table->foreign('organization_id')
->references('id')
->on('teams')
->cascadeOnUpdate()
->restrictOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('clients');
}
};

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
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('projects', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name', 255);
$table->string('color', 16);
$table->uuid('client_id')->nullable();
$table->foreign('client_id')
->references('id')
->on('clients')
->cascadeOnUpdate()
->restrictOnDelete();
$table->uuid('organization_id');
$table->foreign('organization_id')
->references('id')
->on('teams')
->cascadeOnUpdate()
->restrictOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('projects');
}
};

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
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('tasks', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name', 255);
$table->uuid('project_id');
$table->foreign('project_id')
->references('id')
->on('projects')
->cascadeOnUpdate()
->restrictOnDelete();
$table->uuid('organization_id');
$table->foreign('organization_id')
->references('id')
->on('teams')
->cascadeOnUpdate()
->restrictOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tasks');
}
};

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
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('tags', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name', 255);
$table->uuid('organization_id');
$table->foreign('organization_id')
->references('id')
->on('teams')
->cascadeOnUpdate()
->restrictOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tags');
}
};

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
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('time_entries', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('description', 255);
$table->dateTime('start');
$table->dateTime('end')->nullable();
$table->boolean('billable')->default(false);
$table->uuid('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->cascadeOnUpdate()
->restrictOnDelete();
$table->uuid('organization_id');
$table->foreign('organization_id')
->references('id')
->on('teams')
->cascadeOnUpdate()
->restrictOnDelete();
$table->uuid('project_id')->nullable();
$table->foreign('project_id')
->references('id')
->on('projects')
->cascadeOnUpdate()
->restrictOnDelete();
$table->uuid('task_id')->nullable();
$table->foreign('task_id')
->references('id')
->on('tasks')
->cascadeOnUpdate()
->restrictOnDelete();
$table->jsonb('tags')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('time_entries');
}
};

View File

@@ -5,8 +5,14 @@ declare(strict_types=1);
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\Client;
use App\Models\Project;
use App\Models\Task;
use App\Models\Team;
use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
@@ -15,9 +21,35 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
User::factory()->withPersonalTeam()->create([
$this->deleteAll();
$organization = Team::factory()->create([
'name' => 'ACME Corp',
]);
$user1 = User::factory()->withPersonalTeam()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
$user1->teams()->attach($organization);
$client = Client::factory()->create([
'name' => 'Big Company',
]);
$bigCompanyProject = Project::factory()->forClient($client)->create([
'name' => 'Big Company Project',
]);
Task::factory()->forProject($bigCompanyProject)->create();
$internalProject = Project::factory()->create([
'name' => 'Internal Project',
]);
}
private function deleteAll(): void
{
DB::table((new TimeEntry())->getTable())->delete();
DB::table((new Task())->getTable())->delete();
DB::table((new Project())->getTable())->delete();
DB::table((new Client())->getTable())->delete();
DB::table((new User())->getTable())->delete();
DB::table((new Team())->getTable())->delete();
}
}