From e0b6410a79ee33d7e6d940b5e20562741c39859f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 27 Jun 2017 16:11:29 +0800 Subject: [PATCH] Added user can set wife and husband Added Couple model and couples table --- app/Couple.php | 10 ++++ app/Http/Controllers/FamilyActionsController.php | 35 ++++++++++++++ app/User.php | 34 ++++++++++++- database/factories/ModelFactory.php | 4 +- .../2017_06_27_151536_create_couples_table.php | 35 ++++++++++++++ resources/views/home.blade.php | 47 +++++++++++++++++- routes/web.php | 2 + tests/Feature/ManageUserFamiliesTest.php | 55 +++++++++++++++++++++- 8 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 app/Couple.php create mode 100644 database/migrations/2017_06_27_151536_create_couples_table.php diff --git a/app/Couple.php b/app/Couple.php new file mode 100644 index 0000000..a606be5 --- /dev/null +++ b/app/Couple.php @@ -0,0 +1,10 @@ +name = $request->get('set_father'); $father->nickname = $request->get('set_father'); $father->gender_id = 1; @@ -29,6 +30,7 @@ class FamilyActionsController extends Controller ]); $mother = new User; + $mother->name = $request->get('set_mother'); $mother->nickname = $request->get('set_mother'); $mother->gender_id = 2; @@ -45,6 +47,7 @@ class FamilyActionsController extends Controller ]); $child = new User; + $child->name = $request->get('add_child_name'); $child->nickname = $request->get('add_child_name'); $child->gender_id = $request->get('add_child_gender_id'); $child->save(); @@ -56,4 +59,36 @@ class FamilyActionsController extends Controller return back(); } + + public function addWife(Request $request, User $user) + { + $this->validate($request, [ + 'set_wife' => 'required|string|max:255', + ]); + + $wife = new User; + $wife->name = $request->get('set_wife'); + $wife->nickname = $request->get('set_wife'); + $wife->gender_id = 2; + + $user->addWife($wife); + + return back(); + } + + public function addHusband(Request $request, User $user) + { + $this->validate($request, [ + 'set_husband' => 'required|string|max:255', + ]); + + $husband = new User; + $husband->name = $request->get('set_husband'); + $husband->nickname = $request->get('set_husband'); + $husband->gender_id = 1; + + $user->addHusband($husband); + + return back(); + } } diff --git a/app/User.php b/app/User.php index 178a587..e2696dc 100644 --- a/app/User.php +++ b/app/User.php @@ -42,7 +42,7 @@ class User extends Authenticatable public function setFather(User $father) { - if ($father->gender_id === 1) { + if ($father->gender_id == 1) { if ($father->exists == false) $father->save(); @@ -58,7 +58,7 @@ class User extends Authenticatable public function setMother(User $mother) { - if ($mother->gender_id === 2) { + if ($mother->gender_id == 2) { if ($mother->exists == false) $mother->save(); @@ -95,4 +95,34 @@ class User extends Authenticatable $linkText = $this->name ?: $this->nickname; return link_to_route('users.show', $linkText, [$this->id]); } + + public function wifes() + { + return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id'); + } + + public function addWife(User $wife) + { + if ($this->gender_id == 1) { + $this->wifes()->save($wife); + return $wife; + } + + return false; + } + + public function husbands() + { + return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id'); + } + + public function addHusband(User $husband) + { + if ($this->gender_id == 2) { + $this->husbands()->save($husband); + return $husband; + } + + return false; + } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 8c3290a..67ce81a 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -15,8 +15,10 @@ use App\User; /** @var \Illuminate\Database\Eloquent\Factory $factory */ $factory->define(User::class, function (Faker\Generator $faker) { + $name = $faker->name; return [ - 'nickname' => $faker->name, + 'name' => $name, + 'nickname' => $name, 'gender_id' => rand(1, 2), ]; }); diff --git a/database/migrations/2017_06_27_151536_create_couples_table.php b/database/migrations/2017_06_27_151536_create_couples_table.php new file mode 100644 index 0000000..76e1341 --- /dev/null +++ b/database/migrations/2017_06_27_151536_create_couples_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->unsignedInteger('husband_id')->index(); + $table->unsignedInteger('wife_id')->index(); + $table->date('marriege_date')->nullable(); + $table->date('divorce_date')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('couples'); + } +} diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 8bab953..130ce1d 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -28,7 +28,7 @@ @if ($currentUser->father_id) {{ $currentUser->father->profileLink() }} @else - {{ Form::open(['route' => ['family-actions.set-father', $currentUser->id, $currentUser->id]]) }} + {{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }} @@ -56,6 +56,51 @@ @endif + @if ($currentUser->gender_id == 1) + + Isteri + + @if ($currentUser->wifes->isEmpty() == false) +
    + @foreach($currentUser->wifes as $wife) +
  • {{ $wife->profileLink() }}
  • + @endforeach +
+ @else + {{ Form::open(['route' => ['family-actions.add-wife', $currentUser->id]]) }} +
+ {{ Form::text('set_wife', null, ['class' => 'form-control input-sm']) }} + + {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_wife_button']) }} + +
+ {{ Form::close() }} + @endif + + + @else + + Suami + + @if ($currentUser->husbands->isEmpty() == false) +
    + @foreach($currentUser->husbands as $husband) +
  • {{ $husband->profileLink() }}
  • + @endforeach +
+ @else + {{ Form::open(['route' => ['family-actions.add-husband', $currentUser->id]]) }} +
+ {{ Form::text('set_husband', null, ['class' => 'form-control input-sm']) }} + + {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_husband_button']) }} + +
+ {{ Form::close() }} + @endif + + + @endif Anak-Anak diff --git a/routes/web.php b/routes/web.php index 7c9097b..20f62ac 100644 --- a/routes/web.php +++ b/routes/web.php @@ -22,5 +22,7 @@ Route::get('profile', 'HomeController@index')->name('profile'); Route::post('family-actions/{user}/set-father', 'FamilyActionsController@setFather')->name('family-actions.set-father'); Route::post('family-actions/{user}/set-mother', 'FamilyActionsController@setMother')->name('family-actions.set-mother'); Route::post('family-actions/{user}/add-child', 'FamilyActionsController@addChild')->name('family-actions.add-child'); +Route::post('family-actions/{user}/add-wife', 'FamilyActionsController@addWife')->name('family-actions.add-wife'); +Route::post('family-actions/{user}/add-husband', 'FamilyActionsController@addHusband')->name('family-actions.add-husband'); Route::get('users/{user}', 'UsersController@show')->name('users.show'); diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 41c3de9..b51a0fc 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -2,8 +2,9 @@ namespace Tests\Feature; -use Tests\TestCase; +use App\User; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Tests\TestCase; class ManageUserFamiliesTest extends TestCase { @@ -65,4 +66,56 @@ class ManageUserFamiliesTest extends TestCase 'father_id' => $user->id, ]); } + + /** @test */ + public function user_can_set_wife() + { + $user = $this->loginAsUser(['gender_id' => 1]); + $this->visit(route('profile')); + $this->seePageIs(route('profile')); + $this->seeElement('input', ['name' => 'set_wife']); + + + $this->submitForm('set_wife_button', [ + 'set_wife' => 'Nama Istri', + ]); + + $this->seeInDatabase('users', [ + 'nickname' => 'Nama Istri', + 'gender_id' => 2, + ]); + + $wife = User::orderBy('id', 'desc')->first(); + + $this->seeInDatabase('couples', [ + 'husband_id' => $user->id, + 'wife_id' => $wife->id, + ]); + } + + /** @test */ + public function user_can_set_husband() + { + $user = $this->loginAsUser(['gender_id' => 2]); + $this->visit(route('profile')); + $this->seePageIs(route('profile')); + $this->seeElement('input', ['name' => 'set_husband']); + + + $this->submitForm('set_husband_button', [ + 'set_husband' => 'Nama Suami', + ]); + + $this->seeInDatabase('users', [ + 'nickname' => 'Nama Suami', + 'gender_id' => 1, + ]); + + $husband = User::orderBy('id', 'desc')->first(); + + $this->seeInDatabase('couples', [ + 'husband_id' => $husband->id, + 'wife_id' => $user->id, + ]); + } }