From 5ca8d0a857db7741b23006999aa7ba35cad16e04 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 27 Jun 2017 22:16:36 +0800 Subject: [PATCH] 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 --- app/Http/Controllers/FamilyActionsController.php | 58 +++++++++++----- app/Http/Controllers/HomeController.php | 18 ++++- app/Http/Controllers/UsersController.php | 15 +++- app/User.php | 12 +++- resources/views/home.blade.php | 87 ++++++++++++++---------- tests/Feature/ManageUserFamiliesTest.php | 74 ++++++++++++++++++++ tests/Unit/UserTest.php | 12 ++++ 7 files changed, 221 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php index b71e72d..c831583 100644 --- a/app/Http/Controllers/FamilyActionsController.php +++ b/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(); } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 10f3148..f880473 100644 --- a/app/Http/Controllers/HomeController.php +++ b/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 + ]); } } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index c78daa9..a8e6760 100644 --- a/app/Http/Controllers/UsersController.php +++ b/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 + ]); } /** diff --git a/app/User.php b/app/User.php index e2696dc..7f71c01 100644 --- a/app/User.php +++ b/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(); + } } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 130ce1d..ce9bb00 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -3,7 +3,7 @@ @section('content')
-
+
Profile : {{ $currentUser->name ?: $currentUser->nickname }}
@@ -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]) !!}
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }} @@ -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]) !!}
{{ Form::text('set_mother', null, ['class' => 'form-control input-sm']) }} @@ -56,14 +58,26 @@ @endif + + +
+
+
+
+
+
Keluarga
+ +
+ + @if ($currentUser->gender_id == 1) @endif - - - - - -
Isteri @if ($currentUser->wifes->isEmpty() == false) -
    +
      @foreach($currentUser->wifes as $wife) -
    • {{ $wife->profileLink() }}
    • +
    • {{ $wife->profileLink() }}
    • @endforeach
    @else @@ -83,9 +97,9 @@
Suami @if ($currentUser->husbands->isEmpty() == false) -
    +
      @foreach($currentUser->husbands as $husband) -
    • {{ $husband->profileLink() }}
    • +
    • {{ $husband->profileLink() }}
    • @endforeach
    @else @@ -101,41 +115,44 @@
Anak-Anak
-
    - @foreach($currentUser->childs as $child) -
  • - {{ $child->profileLink() }} ({{ $child->gender }}) -
  • - @endforeach -
  • - {{ Form::open(['route' => ['family-actions.add-child', $currentUser->id]]) }} -
    -
    - {!! FormField::text('add_child_name', ['label' => 'Nama Anak']) !!} -
    -
    - {!! FormField::radios('add_child_gender_id', [1 => 'Laki-laki', 2 => 'Perempuan'], ['label' => 'Jenis Kelamin Anak']) !!} -
    -
    -
    - {{ Form::submit('Tambah Anak', ['class' => 'btn btn-success btn-sm']) }} -
    -
    - {{ Form::close() }} -
  • -
-
+ Anak-Anak +
    + @foreach($currentUser->childs as $child) +
  • + {{ $child->profileLink() }} ({{ $child->gender }}) +
  • + @endforeach +
  • + {{ Form::open(['route' => ['family-actions.add-child', $currentUser->id]]) }} +
    +
    + {!! FormField::text('add_child_name', ['label' => 'Nama Anak']) !!} +
    +
    + {!! FormField::radios('add_child_gender_id', [1 => 'Laki-laki', 2 => 'Perempuan'], ['label' => 'Jenis Kelamin Anak']) !!} +
    +
    + {!! FormField::select('add_child_parent_id', $usersMariageList, ['label' => 'Dari Pernikahan']) !!} +
    +
    + {{ Form::submit('Tambah Anak', ['class' => 'btn btn-success btn-sm']) }} + {{ Form::close() }} +
  • +
+ @if (count($errors) > 0) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif
@endsection diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index b51a0fc..85c84cc 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/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); + } } diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index b1e887c..8e9c032 100644 --- a/tests/Unit/UserTest.php +++ b/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); + } }