From 23c82bfbf5165b9dc358d9a51f407ecad8304569 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 28 Mar 2021 11:54:47 +0800 Subject: [PATCH] Make test passed for family_member_connections entries --- app/Http/Controllers/FamilyActionsController.php | 17 +++++++++-- app/User.php | 27 +++++++++++++++++ ...1809_create_family_member_connections_table.php | 35 ++++++++++++++++++++++ tests/Feature/ManageUserFamiliesTest.php | 4 +-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2021_03_28_091809_create_family_member_connections_table.php diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php index edee17f..47277ae 100644 --- a/app/Http/Controllers/FamilyActionsController.php +++ b/app/Http/Controllers/FamilyActionsController.php @@ -2,10 +2,11 @@ namespace App\Http\Controllers; -use App\User; use App\Couple; -use Ramsey\Uuid\Uuid; +use App\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Ramsey\Uuid\Uuid; class FamilyActionsController extends Controller { @@ -26,6 +27,12 @@ class FamilyActionsController extends Controller if ($request->get('set_father_id')) { $user->father_id = $request->get('set_father_id'); $user->save(); + + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $user->id, + 'requested_id' => $request->get('set_father_id'), + ]); } else { $father = new User; $father->id = Uuid::uuid4()->toString(); @@ -57,6 +64,12 @@ class FamilyActionsController extends Controller if ($request->get('set_mother_id')) { $user->mother_id = $request->get('set_mother_id'); $user->save(); + + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $user->id, + 'requested_id' => $request->get('set_mother_id'), + ]); } else { $mother = new User; $mother->id = Uuid::uuid4()->toString(); diff --git a/app/User.php b/app/User.php index 26bafb6..a069885 100644 --- a/app/User.php +++ b/app/User.php @@ -5,6 +5,7 @@ namespace App; use Carbon\Carbon; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\DB; use Ramsey\Uuid\Uuid; class User extends Authenticatable @@ -76,6 +77,12 @@ class User extends Authenticatable $this->father_id = $father->id; $this->save(); + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $this->id, + 'requested_id' => $father->id, + ]); + return $father; } @@ -93,6 +100,12 @@ class User extends Authenticatable $this->mother_id = $mother->id; $this->save(); + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $this->id, + 'requested_id' => $mother->id, + ]); + return $mother; } @@ -147,6 +160,13 @@ class User extends Authenticatable 'marriage_date' => $marriageDate, 'manager_id' => auth()->id(), ]); + + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $this->id, + 'requested_id' => $wife->id, + ]); + return $wife; } @@ -166,6 +186,13 @@ class User extends Authenticatable 'marriage_date' => $marriageDate, 'manager_id' => auth()->id(), ]); + + DB::table('family_member_connections')->insert([ + 'id' => Uuid::uuid4()->toString(), + 'requester_id' => $this->id, + 'requested_id' => $husband->id, + ]); + return $husband; } diff --git a/database/migrations/2021_03_28_091809_create_family_member_connections_table.php b/database/migrations/2021_03_28_091809_create_family_member_connections_table.php new file mode 100644 index 0000000..fc01627 --- /dev/null +++ b/database/migrations/2021_03_28_091809_create_family_member_connections_table.php @@ -0,0 +1,35 @@ +uuid('id')->primary(); + $table->uuid('requester_id'); + $table->uuid('requested_id'); + $table->timestamps(); + + $table->unique(['requester_id', 'requested_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('family_member_connections'); + } +} diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 9aa16cc..77e4cae 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -93,8 +93,8 @@ class ManageUserFamiliesTest extends TestCase $child = User::where('name', 'Nama Anak 1')->first(); $this->seeInDatabase('family_member_connections', [ - 'requester_id' => $user->id, - 'requested_id' => $child->id, + 'requester_id' => $child->id, + 'requested_id' => $user->id, ]); }