Browse Source

Added set father and mother on profile page

pull/3/head
Nafies Luthfi 9 years ago
parent
commit
add2bfecf1
  1. 31
      app/Http/Controllers/FamilyActionsController.php
  2. 8
      app/User.php
  3. 12
      resources/views/home.blade.php
  4. 2
      routes/web.php
  5. 38
      tests/Feature/ManageUserFamiliesTest.php

31
app/Http/Controllers/FamilyActionsController.php

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class FamilyActionsController extends Controller
{
public function setFather(Request $request, User $user)
{
$father = new User;
$father->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();
}
}

8
app/User.php

@ -43,6 +43,10 @@ class User extends Authenticatable
public function setFather(User $father) public function setFather(User $father)
{ {
if ($father->gender_id === 1) { if ($father->gender_id === 1) {
if ($father->exists == false)
$father->save();
$this->father_id = $father->id; $this->father_id = $father->id;
$this->save(); $this->save();
@ -55,6 +59,10 @@ class User extends Authenticatable
public function setMother(User $mother) public function setMother(User $mother)
{ {
if ($mother->gender_id === 2) { if ($mother->gender_id === 2) {
if ($mother->exists == false)
$mother->save();
$this->mother_id = $mother->id; $this->mother_id = $mother->id;
$this->save(); $this->save();

12
resources/views/home.blade.php

@ -26,13 +26,13 @@
<th>Ayah</th> <th>Ayah</th>
<td> <td>
@if ($currentUser->father_id) @if ($currentUser->father_id)
{{ $currentUser->father->name }}
{{ $currentUser->father->nickname }}
@else @else
{{ Form::open() }}
{{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
<div class="input-group"> <div class="input-group">
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }} {{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn"> <span class="input-group-btn">
{{ Form::submit('update', ['class' => 'btn btn-info btn-sm']) }}
{{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_father_button']) }}
</span> </span>
</div> </div>
{{ Form::close() }} {{ Form::close() }}
@ -43,13 +43,13 @@
<th>Ibu</th> <th>Ibu</th>
<td> <td>
@if ($currentUser->mother_id) @if ($currentUser->mother_id)
{{ $currentUser->mother->name }}
{{ $currentUser->mother->nickname }}
@else @else
{{ Form::open() }}
{{ Form::open(['route' => ['family-actions.set-mother', $currentUser->id]]) }}
<div class="input-group"> <div class="input-group">
{{ Form::text('set_mother', null, ['class' => 'form-control input-sm']) }} {{ Form::text('set_mother', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn"> <span class="input-group-btn">
{{ 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']) }}
</span> </span>
</div> </div>
{{ Form::close() }} {{ Form::close() }}

2
routes/web.php

@ -19,3 +19,5 @@ Auth::routes();
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home', 'HomeController@index')->name('home');
Route::get('/profile', 'HomeController@index')->name('profile'); 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');

38
tests/Feature/ManageUserFamiliesTest.php

@ -10,16 +10,44 @@ class ManageUserFamiliesTest extends TestCase
use DatabaseMigrations; use DatabaseMigrations;
/** @test */ /** @test */
public function user_can_update_their_family_members()
public function user_can_update_their_father()
{ {
$user = $this->loginAsUser(); $user = $this->loginAsUser();
$this->visit(route('profile')); $this->visit(route('profile'));
$this->seePageIs(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' => '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);
} }
} }
Loading…
Cancel
Save