From 894ba607f108a62c7a69748a56815d4f614bbfe9 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 5 Oct 2017 12:51:18 +0800 Subject: [PATCH] Add user marriages page Add addChild method on Couple model Add user marriages relation (different from user couples relation) Set default app.name config to 'Silsilah' Add lang files for couple entity --- .env.example | 1 - app/Couple.php | 13 +++++++++ app/Http/Controllers/UserMarriagesController.php | 15 ++++++++++ app/User.php | 8 +++++ config/app.php | 2 +- database/factories/ModelFactory.php | 13 +++++++++ resources/lang/en/couple.php | 14 +++++++++ resources/lang/en/user.php | 1 + resources/lang/id/couple.php | 14 +++++++++ resources/lang/id/user.php | 1 + resources/views/users/marriages.blade.php | 29 +++++++++++++++++++ routes/web.php | 2 ++ tests/Feature/ManageUserMarriagesTest.php | 20 +++++++++++++ tests/Unit/CoupleTest.php | 37 ++++++++++++++++++++++++ tests/Unit/UserTest.php | 16 +++++++++- 15 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/UserMarriagesController.php create mode 100644 resources/lang/en/couple.php create mode 100644 resources/lang/id/couple.php create mode 100644 resources/views/users/marriages.blade.php create mode 100644 tests/Feature/ManageUserMarriagesTest.php create mode 100644 tests/Unit/CoupleTest.php diff --git a/.env.example b/.env.example index 668c06f..55b5223 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,3 @@ -APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true diff --git a/app/Couple.php b/app/Couple.php index f2db974..c512e02 100644 --- a/app/Couple.php +++ b/app/Couple.php @@ -15,4 +15,17 @@ class Couple extends Model { return $this->belongsTo(User::class); } + + public function childs() + { + return $this->hasMany(User::class, 'parent_id'); + } + + public function addChild(User $user) + { + $user->parent_id = $this->id; + $user->father_id = $this->husband_id; + $user->mother_id = $this->wife_id; + $user->save(); + } } diff --git a/app/Http/Controllers/UserMarriagesController.php b/app/Http/Controllers/UserMarriagesController.php new file mode 100644 index 0000000..adf6e0a --- /dev/null +++ b/app/Http/Controllers/UserMarriagesController.php @@ -0,0 +1,15 @@ +marriages()->with('husband', 'wife')->withCount('childs')->get(); + return view('users.marriages', compact('user', 'marriages')); + } +} diff --git a/app/User.php b/app/User.php index 57262a9..40c9a9f 100644 --- a/app/User.php +++ b/app/User.php @@ -149,6 +149,14 @@ class User extends Authenticatable return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->withPivot(['id'])->withTimestamps(); } + public function marriages() + { + if ($this->gender_id == 1) + return $this->hasMany(Couple::class, 'husband_id'); + + return $this->hasMany(Couple::class, 'wife_id'); + } + public function siblings() { if (is_null($this->father_id) && is_null($this->mother_id) && is_null($this->parent_id)) diff --git a/config/app.php b/config/app.php index 85dbeb4..0baa4fa 100644 --- a/config/app.php +++ b/config/app.php @@ -12,7 +12,7 @@ return [ | any other location as required by the application or its packages. */ - 'name' => env('APP_NAME', 'Laravel'), + 'name' => 'Silsilah', /* |-------------------------------------------------------------------------- diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 697591e..dc96fc0 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -1,5 +1,6 @@ state(User::class, 'male', function (Faker\Generator $faker) { $factory->state(User::class, 'female', function (Faker\Generator $faker) { return ['gender_id' => 2]; +}); + +$factory->define(Couple::class, function (Faker\Generator $faker) { + $name = $faker->name; + return [ + 'husband_id' => function () { + return factory(User::class)->states('male')->create()->id; + }, + 'wife_id' => function () { + return factory(User::class)->states('female')->create()->id; + }, + ]; }); \ No newline at end of file diff --git a/resources/lang/en/couple.php b/resources/lang/en/couple.php new file mode 100644 index 0000000..ddbcdc4 --- /dev/null +++ b/resources/lang/en/couple.php @@ -0,0 +1,14 @@ + 'Show Marriage Profile', + 'childs_count' => 'Childs Count', + 'grand_childs_count' => 'Grand Childs Count', + + // Attributes + 'husband' => 'Head of Family', + 'wive' => 'Wife', + 'marriage_date' => 'Marriage Date', + 'divorce_date' => 'Divorce Date', +]; \ No newline at end of file diff --git a/resources/lang/en/user.php b/resources/lang/en/user.php index 4e277ff..672ec1a 100644 --- a/resources/lang/en/user.php +++ b/resources/lang/en/user.php @@ -16,6 +16,7 @@ return [ 'grand_mother' => 'Grand Mother', 'grand_father' => 'Grand Father', 'nieces' => 'Nieces', + 'marriages' => 'Marriages', // Actions 'edit' => 'Edit Profile', diff --git a/resources/lang/id/couple.php b/resources/lang/id/couple.php new file mode 100644 index 0000000..c92334f --- /dev/null +++ b/resources/lang/id/couple.php @@ -0,0 +1,14 @@ + 'Lhat Profil Pernikahan', + 'childs_count' => 'Jumlah Anak', + 'grand_childs_count' => 'Jumlah Cucu', + + // Attributes + 'husband' => 'Kepala Keluarga', + 'wive' => 'Isteri', + 'marriage_date' => 'Tanggal Pernikahan', + 'divorce_date' => 'Tanggal Perceraian', +]; \ No newline at end of file diff --git a/resources/lang/id/user.php b/resources/lang/id/user.php index d752764..87d3ed0 100644 --- a/resources/lang/id/user.php +++ b/resources/lang/id/user.php @@ -16,6 +16,7 @@ return [ 'grand_mother' => 'Nenek', 'grand_father' => 'Kakek', 'nieces' => 'Keponakan', + 'marriages' => 'Pernikahan', // Actions 'edit' => 'Edit Profil', diff --git a/resources/views/users/marriages.blade.php b/resources/views/users/marriages.blade.php new file mode 100644 index 0000000..755ef60 --- /dev/null +++ b/resources/views/users/marriages.blade.php @@ -0,0 +1,29 @@ +@extends('layouts.app') + +@section('content') +

+ {{ $user->name }} {{ trans('user.marriages') }} +

+ +
+ @foreach ($marriages as $marriage) +
+
+ + + + + @if ($marriage->divorce_date) + + @endif + + {{-- --}} +
{{ trans('couple.husband') }}{{ $marriage->husband->profileLink() }}
{{ trans('couple.wive') }}{{ $marriage->wife->profileLink() }}
{{ trans('couple.marriage_date') }}{{ $marriage->marriage_date }}
{{ trans('couple.divorce_date') }}{{ $marriage->divorce_date }}
{{ trans('couple.childs_count') }}{{ $marriage->childs_count }}
{{ trans('couple.grand_childs_count') }}?
+ {{-- --}} +
+
+ @endforeach +
+@endsection diff --git a/routes/web.php b/routes/web.php index 74935bb..a633b3b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,6 +31,8 @@ Route::patch('users/{user}', 'UsersController@update')->name('users.update'); Route::get('users/{user}/chart', 'UsersController@chart')->name('users.chart'); Route::get('users/{user}/tree', 'UsersController@tree')->name('users.tree'); +Route::get('users/{user}/marriages', 'UserMarriagesController@index')->name('users.marriages'); + /** * Backup Restore Database Routes */ diff --git a/tests/Feature/ManageUserMarriagesTest.php b/tests/Feature/ManageUserMarriagesTest.php new file mode 100644 index 0000000..7b3ad21 --- /dev/null +++ b/tests/Feature/ManageUserMarriagesTest.php @@ -0,0 +1,20 @@ +create(); + $this->visit(route('users.marriages', $user->id)); + $this->see($user->name); + } +} diff --git a/tests/Unit/CoupleTest.php b/tests/Unit/CoupleTest.php new file mode 100644 index 0000000..5b841f1 --- /dev/null +++ b/tests/Unit/CoupleTest.php @@ -0,0 +1,37 @@ +create(); + $this->assertTrue($couple->husband instanceof User); + $this->assertTrue($couple->wife instanceof User); + } + + /** @test */ + public function a_couple_can_have_many_childs() + { + $couple = factory(Couple::class)->create(); + $this->assertCount(0, $couple->childs); + + $child = factory(User::class)->create(); + $couple->addChild($child); + + $child = $child->fresh(); + $this->assertCount(1, $couple->fresh()->childs); + $this->assertEquals($child->parent_id, $couple->id); + $this->assertEquals($child->father_id, $couple->husband_id); + $this->assertEquals($child->mother_id, $couple->wife_id); + } +} diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 5113714..57fb09e 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -18,7 +18,7 @@ class UserTest extends TestCase } /** @test */ - public function user_can_have_couples() + public function user_can_have_many_couples() { $husband = factory(User::class)->states('male')->create(); $wife = factory(User::class)->states('female')->create(); @@ -31,6 +31,20 @@ class UserTest extends TestCase } /** @test */ + public function user_can_have_many_marriages() + { + $husband = factory(User::class)->states('male')->create(); + $wife = factory(User::class)->states('female')->create(); + $husband->addWife($wife); + + $husband = $husband->fresh(); + $this->assertCount(1, $husband->marriages); + + $wife = $wife->fresh(); + $this->assertCount(1, $wife->marriages); + } + + /** @test */ public function user_can_ony_marry_same_person_once() { $husband = factory(User::class)->states('male')->create();