diff --git a/README.md b/README.md index 17df8fa..d97c776 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Test method `test_countries_with_team_size()`. --- -## Task 6. Polymorphic Attachments +## Task 7. Polymorphic Attachments In the route `/attachments`, the table should show the filenames and the class names of Task and Comment models. Fix the `app/Models/Attachment.php` relationship to make it work. @@ -83,3 +83,11 @@ Test method `test_attachments_polymorphic()`. --- +## Task 8. Add BelongsToMany Row + +In the POST route `/projects`, the project should be saved for a logged-in user, with start_date field from $request. Write that sentence in the Controller. + +Test method `test_belongstomany_add()`. + +--- + diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php new file mode 100644 index 0000000..e04fb1a --- /dev/null +++ b/app/Http/Controllers/ProjectController.php @@ -0,0 +1,16 @@ +project_id and with $request->start_date parameter + + return 'Success'; + } +} diff --git a/app/Models/Project.php b/app/Models/Project.php new file mode 100644 index 0000000..cf8fe1d --- /dev/null +++ b/app/Models/Project.php @@ -0,0 +1,13 @@ +belongsToMany(Project::class)->withPivot('start_date'); + } } diff --git a/database/migrations/2021_11_22_093919_create_projects_table.php b/database/migrations/2021_11_22_093919_create_projects_table.php new file mode 100644 index 0000000..0e6842f --- /dev/null +++ b/database/migrations/2021_11_22_093919_create_projects_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('projects'); + } +} diff --git a/database/migrations/2021_11_22_093936_create_project_user_table.php b/database/migrations/2021_11_22_093936_create_project_user_table.php new file mode 100644 index 0000000..3b5b111 --- /dev/null +++ b/database/migrations/2021_11_22_093936_create_project_user_table.php @@ -0,0 +1,32 @@ +foreignId('project_id')->constrained(); + $table->foreignId('user_id')->constrained(); + $table->date('start_date'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('project_user'); + } +} diff --git a/routes/web.php b/routes/web.php index 6bbd0ed..ead44b9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,3 +30,5 @@ Route::get('teams', [\App\Http\Controllers\TeamController::class, 'index']); Route::get('countries', [\App\Http\Controllers\CountryController::class, 'index']); Route::get('attachments', [\App\Http\Controllers\AttachmentController::class, 'index']); + +Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store'])->middleware('auth'); diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index aa175c1..c23ec1a 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature; use App\Models\Attachment; use App\Models\Comment; use App\Models\Country; +use App\Models\Project; use App\Models\Role; use App\Models\Task; use App\Models\Team; @@ -133,4 +134,23 @@ class RelationshipsTest extends TestCase $response->assertSee('Task'); $response->assertSee('Comment'); } + + // TASK: add a record to belongstomany relationship + public function test_belongstomany_add() + { + $user = User::factory()->create(); + $project = Project::create(['name' => 'Some project']); + + $response = $this->actingAs($user)->post('/projects', [ + 'project_id' => $project->id, + 'start_date' => now()->toDateString() + ]); + $response->assertStatus(200); + + $this->assertDatabaseHas('project_user', [ + 'project_id' => $project->id, + 'user_id' => $user->id, + 'start_date' => now()->toDateString() + ]); + } }