From add2bfecf1f8451308d5a1ffa03cd68ee78d65c5 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 26 Jun 2017 14:30:32 +0800 Subject: [PATCH] Added set father and mother on profile page --- app/Http/Controllers/FamilyActionsController.php | 31 +++++++++++++++++++ app/User.php | 8 +++++ resources/views/home.blade.php | 12 ++++---- routes/web.php | 2 ++ tests/Feature/ManageUserFamiliesTest.php | 38 ++++++++++++++++++++---- 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 app/Http/Controllers/FamilyActionsController.php diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php new file mode 100644 index 0000000..5ee77cf --- /dev/null +++ b/app/Http/Controllers/FamilyActionsController.php @@ -0,0 +1,31 @@ +nickname = $request->get('set_father'); + $father->gender_id = 1; + + $user->setFather($father); + + return back(); + } + + public function setMother(Request $request, User $user) + { + $mother = new User; + $mother->nickname = $request->get('set_mother'); + $mother->gender_id = 2; + + $user->setMother($mother); + + return back(); + } +} diff --git a/app/User.php b/app/User.php index f1f4adc..420f7c1 100644 --- a/app/User.php +++ b/app/User.php @@ -43,6 +43,10 @@ class User extends Authenticatable public function setFather(User $father) { if ($father->gender_id === 1) { + + if ($father->exists == false) + $father->save(); + $this->father_id = $father->id; $this->save(); @@ -55,6 +59,10 @@ class User extends Authenticatable public function setMother(User $mother) { if ($mother->gender_id === 2) { + + if ($mother->exists == false) + $mother->save(); + $this->mother_id = $mother->id; $this->save(); diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 602d36f..42721b3 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -26,13 +26,13 @@ Ayah @if ($currentUser->father_id) - {{ $currentUser->father->name }} + {{ $currentUser->father->nickname }} @else - {{ Form::open() }} + {{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }} - {{ Form::submit('update', ['class' => 'btn btn-info btn-sm']) }} + {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_father_button']) }}
{{ Form::close() }} @@ -43,13 +43,13 @@ Ibu @if ($currentUser->mother_id) - {{ $currentUser->mother->name }} + {{ $currentUser->mother->nickname }} @else - {{ Form::open() }} + {{ Form::open(['route' => ['family-actions.set-mother', $currentUser->id]]) }}
{{ Form::text('set_mother', null, ['class' => 'form-control input-sm']) }} - {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_father_button']) }} + {{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_mother_button']) }}
{{ Form::close() }} diff --git a/routes/web.php b/routes/web.php index f168fe2..33b513b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,3 +19,5 @@ Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); 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'); diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 402aeb1..49b619e 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -10,16 +10,44 @@ class ManageUserFamiliesTest extends TestCase use DatabaseMigrations; /** @test */ - public function user_can_update_their_family_members() + public function user_can_update_their_father() { $user = $this->loginAsUser(); $this->visit(route('profile')); $this->seePageIs(route('profile')); - $this->see($user->nickname); - $this->seeElement('input', ['name' => 'set_father']); + $this->submitForm('set_father_button', [ + 'set_father' => 'Nama Ayah', + ]); + + $this->seeInDatabase('users', [ + 'nickname' => 'Nama Ayah', + ]); + + $this->assertEquals('Nama Ayah', $user->fresh()->father->nickname); + } + + /** @test */ + public function user_can_update_their_mother() + { + $user = $this->loginAsUser(); + $this->visit(route('profile')); + $this->seePageIs(route('profile')); + + // $this->see($user->nickname); + // $this->seeElement('input', ['name' => 'set_mother']); $this->seeElement('input', ['name' => 'set_mother']); - $this->seeElement('input', ['name' => 'add_child_name']); - $this->seeElement('input', ['name' => 'add_child_gender_id']); + // $this->seeElement('input', ['name' => 'add_child_name']); + // $this->seeElement('input', ['name' => 'add_child_gender_id']); + + $this->submitForm('set_mother_button', [ + 'set_mother' => 'Nama Ibu', + ]); + + $this->seeInDatabase('users', [ + 'nickname' => 'Nama Ibu', + ]); + + $this->assertEquals('Nama Ibu', $user->fresh()->mother->nickname); } }