Browse Source

User can add parent, fatherm and mother from existing user

Added user can add children with parent id if exist
Added user can set father from existing male user
Added user can set mother from existing female user
Added user have many marriages relation
pull/3/head
Nafies Luthfi 9 years ago
parent
commit
5ca8d0a857
  1. 58
      app/Http/Controllers/FamilyActionsController.php
  2. 18
      app/Http/Controllers/HomeController.php
  3. 15
      app/Http/Controllers/UsersController.php
  4. 12
      app/User.php
  5. 87
      resources/views/home.blade.php
  6. 74
      tests/Feature/ManageUserFamiliesTest.php
  7. 12
      tests/Unit/UserTest.php

58
app/Http/Controllers/FamilyActionsController.php

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Couple;
use App\User;
use Illuminate\Http\Request;
@ -10,15 +11,21 @@ class FamilyActionsController extends Controller
public function setFather(Request $request, User $user)
{
$this->validate($request, [
'set_father' => 'required|string|max:255',
'set_father_id' => 'nullable',
'set_father' => 'required_without:set_father_id|max:255',
]);
$father = new User;
$father->name = $request->get('set_father');
$father->nickname = $request->get('set_father');
$father->gender_id = 1;
if ($request->get('set_father_id')) {
$user->father_id = $request->get('set_father_id');
$user->save();
} else {
$father = new User;
$father->name = $request->get('set_father');
$father->nickname = $request->get('set_father');
$father->gender_id = 1;
$user->setFather($father);
$user->setFather($father);
}
return back();
}
@ -26,15 +33,21 @@ class FamilyActionsController extends Controller
public function setMother(Request $request, User $user)
{
$this->validate($request, [
'set_mother' => 'required|string|max:255',
'set_mother_id' => 'nullable',
'set_mother' => 'required_without:set_mother_id|max:255',
]);
$mother = new User;
$mother->name = $request->get('set_mother');
$mother->nickname = $request->get('set_mother');
$mother->gender_id = 2;
if ($request->get('set_mother_id')) {
$user->mother_id = $request->get('set_mother_id');
$user->save();
} else {
$mother = new User;
$mother->name = $request->get('set_mother');
$mother->nickname = $request->get('set_mother');
$mother->gender_id = 2;
$user->setMother($mother);
$user->setMother($mother);
}
return back();
}
@ -44,18 +57,31 @@ class FamilyActionsController extends Controller
$this->validate($request, [
'add_child_name' => 'required|string|max:255',
'add_child_gender_id' => 'required|in:1,2',
'add_child_parent_id' => 'nullable|exists:couples,id',
]);
$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->parent_id = $request->get('add_child_parent_id');
\DB::beginTransaction();
$child->save();
if ($user->gender_id == 1)
$child->setFather($user);
else
$child->setMother($user);
if ($request->get('add_child_parent_id')) {
$couple = Couple::find($request->get('add_child_parent_id'));
$child->father_id = $couple->husband_id;
$child->mother_id = $couple->wife_id;
$child->save();
} else {
if ($user->gender_id == 1)
$child->setFather($user);
else
$child->setMother($user);
}
\DB::commit();
return back();
}

18
app/Http/Controllers/HomeController.php

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class HomeController extends Controller
@ -23,6 +24,21 @@ class HomeController extends Controller
*/
public function index()
{
return view('home', ['currentUser' => auth()->user()]);
$user = auth()->user();
$usersMariageList = [];
foreach ($user->marriages as $spouse) {
$usersMariageList[$spouse->pivot->id] = $user->name.' & '.$spouse->name;
}
$malePersonList = User::where('gender_id', 1)->pluck('nickname', 'id');
$femalePersonList = User::where('gender_id', 2)->pluck('nickname', 'id');
return view('home', [
'currentUser' => $user,
'usersMariageList' => $usersMariageList,
'malePersonList' => $malePersonList,
'femalePersonList' => $femalePersonList
]);
}
}

15
app/Http/Controllers/UsersController.php

@ -46,7 +46,20 @@ class UsersController extends Controller
*/
public function show(User $user)
{
return view('home', ['currentUser' => $user]);
$usersMariageList = [];
foreach ($user->marriages as $spouse) {
$usersMariageList[$spouse->pivot->id] = $user->name.' & '.$spouse->name;
}
$malePersonList = User::where('gender_id', 1)->pluck('nickname', 'id');
$femalePersonList = User::where('gender_id', 2)->pluck('nickname', 'id');
return view('home', [
'currentUser' => $user,
'usersMariageList' => $usersMariageList,
'malePersonList' => $malePersonList,
'femalePersonList' => $femalePersonList
]);
}
/**

12
app/User.php

@ -98,7 +98,7 @@ class User extends Authenticatable
public function wifes()
{
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id');
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps();
}
public function addWife(User $wife)
@ -113,7 +113,7 @@ class User extends Authenticatable
public function husbands()
{
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id');
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps();
}
public function addHusband(User $husband)
@ -125,4 +125,12 @@ class User extends Authenticatable
return false;
}
public function marriages()
{
if ($this->gender_id == 1)
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps();
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps();
}
}

87
resources/views/home.blade.php

@ -3,7 +3,7 @@
@section('content')
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Profile : {{ $currentUser->name ?: $currentUser->nickname }}</div>
@ -29,6 +29,7 @@
{{ $currentUser->father->profileLink() }}
@else
{{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
{!! FormField::select('set_father_id', $malePersonList, ['label' => false]) !!}
<div class="input-group">
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn">
@ -46,6 +47,7 @@
{{ $currentUser->mother->profileLink() }}
@else
{{ Form::open(['route' => ['family-actions.set-mother', $currentUser->id]]) }}
{!! FormField::select('set_mother_id', $femalePersonList, ['label' => false]) !!}
<div class="input-group">
{{ Form::text('set_mother', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn">
@ -56,14 +58,26 @@
@endif
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Keluarga</div>
<div class="panel-body">
<table class="table table-condensed">
<tbody>
@if ($currentUser->gender_id == 1)
<tr>
<th>Isteri</th>
<td>
@if ($currentUser->wifes->isEmpty() == false)
<ul class="list-group">
<ul>
@foreach($currentUser->wifes as $wife)
<li class="list-group-item">{{ $wife->profileLink() }}</li>
<li>{{ $wife->profileLink() }}</li>
@endforeach
</ul>
@else
@ -83,9 +97,9 @@
<th>Suami</th>
<td>
@if ($currentUser->husbands->isEmpty() == false)
<ul class="list-group">
<ul>
@foreach($currentUser->husbands as $husband)
<li class="list-group-item">{{ $husband->profileLink() }}</li>
<li>{{ $husband->profileLink() }}</li>
@endforeach
</ul>
@else
@ -101,41 +115,44 @@
</td>
</tr>
@endif
<tr>
<th colspan="2">Anak-Anak</th>
</tr>
<tr>
<td colspan="2">
<ul class="list-group">
@foreach($currentUser->childs as $child)
<li class="list-group-item">
{{ $child->profileLink() }} ({{ $child->gender }})
</li>
@endforeach
<li class="list-group-item">
{{ Form::open(['route' => ['family-actions.add-child', $currentUser->id]]) }}
<div class="row">
<div class="col-md-4">
{!! FormField::text('add_child_name', ['label' => 'Nama Anak']) !!}
</div>
<div class="col-md-5">
{!! FormField::radios('add_child_gender_id', [1 => 'Laki-laki', 2 => 'Perempuan'], ['label' => 'Jenis Kelamin Anak']) !!}
</div>
<div class="col-md-3">
<br>
{{ Form::submit('Tambah Anak', ['class' => 'btn btn-success btn-sm']) }}
</div>
</div>
{{ Form::close() }}
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<legend>Anak-Anak</legend>
<ul class="list-group">
@foreach($currentUser->childs as $child)
<li class="list-group-item">
{{ $child->profileLink() }} ({{ $child->gender }})
</li>
@endforeach
<li class="list-group-item">
{{ Form::open(['route' => ['family-actions.add-child', $currentUser->id]]) }}
<div class="row">
<div class="col-md-4">
{!! FormField::text('add_child_name', ['label' => 'Nama Anak']) !!}
</div>
<div class="col-md-4">
{!! FormField::radios('add_child_gender_id', [1 => 'Laki-laki', 2 => 'Perempuan'], ['label' => 'Jenis Kelamin Anak']) !!}
</div>
<div class="col-md-4">
{!! FormField::select('add_child_parent_id', $usersMariageList, ['label' => 'Dari Pernikahan']) !!}
</div>
</div>
{{ Form::submit('Tambah Anak', ['class' => 'btn btn-success btn-sm']) }}
{{ Form::close() }}
</li>
</ul>
</div>
</div>
</div>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
@endsection

74
tests/Feature/ManageUserFamiliesTest.php

@ -16,6 +16,7 @@ class ManageUserFamiliesTest extends TestCase
$user = $this->loginAsUser();
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_father']);
$this->submitForm('set_father_button', [
'set_father' => 'Nama Ayah',
@ -34,6 +35,7 @@ class ManageUserFamiliesTest extends TestCase
$user = $this->loginAsUser();
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_mother']);
$this->submitForm('set_mother_button', [
'set_mother' => 'Nama Ibu',
@ -54,16 +56,50 @@ class ManageUserFamiliesTest extends TestCase
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'add_child_name']);
$this->seeElement('input', ['name' => 'add_child_gender_id']);
$this->seeElement('select', ['name' => 'add_child_parent_id']);
$this->submitForm('Tambah Anak', [
'add_child_name' => 'Nama Anak 1',
'add_child_gender_id' => 1,
'add_child_parent_id' => '',
]);
$this->seeInDatabase('users', [
'nickname' => 'Nama Anak 1',
'gender_id' => 1,
'father_id' => $user->id,
'mother_id' => null,
'parent_id' => null,
]);
}
/** @test */
public function user_can_add_childrens_with_parent_id_if_exist()
{
$husband = factory(User::class)->states('male')->create();
$wife = factory(User::class)->states('female')->create();
$husband->addWife($wife);
$marriageId = $husband->wifes->first()->pivot->id;
$this->actingAs($husband);
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'add_child_name']);
$this->seeElement('input', ['name' => 'add_child_gender_id']);
$this->seeElement('select', ['name' => 'add_child_parent_id']);
$this->submitForm('Tambah Anak', [
'add_child_name' => 'Nama Anak 1',
'add_child_gender_id' => 1,
'add_child_parent_id' => $marriageId,
]);
$this->seeInDatabase('users', [
'nickname' => 'Nama Anak 1',
'gender_id' => 1,
'father_id' => $husband->id,
'mother_id' => $wife->id,
]);
}
@ -118,4 +154,42 @@ class ManageUserFamiliesTest extends TestCase
'wife_id' => $user->id,
]);
}
/** @test */
public function user_can_pick_father_from_existing_user()
{
$user = $this->loginAsUser();
$father = factory(User::class)->states('male')->create();
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_father']);
$this->seeElement('select', ['name' => 'set_father_id']);
$this->submitForm('set_father_button', [
'set_father' => '',
'set_father_id' => $father->id,
]);
$this->assertEquals($father->nickname, $user->fresh()->father->nickname);
}
/** @test */
public function user_can_pick_mother_from_existing_user()
{
$user = $this->loginAsUser();
$mother = factory(User::class)->states('female')->create();
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_mother']);
$this->seeElement('select', ['name' => 'set_mother_id']);
$this->submitForm('set_mother_button', [
'set_mother' => '',
'set_mother_id' => $mother->id,
]);
$this->assertEquals($mother->nickname, $user->fresh()->mother->nickname);
}
}

12
tests/Unit/UserTest.php

@ -16,4 +16,16 @@ class UserTest extends TestCase
$user = factory(User::class)->create();
$this->assertEquals(link_to_route('users.show', $user->nickname, [$user->id]), $user->profileLink());
}
/** @test */
public function user_can_have_marriages()
{
$husband = factory(User::class)->states('male')->create();
$wife = factory(User::class)->states('female')->create();
$husband->addWife($wife);
$this->assertCount(1, $husband->wifes);
$this->assertCount(1, $wife->husbands);
$this->assertCount(1, $husband->marriages);
}
}
Loading…
Cancel
Save