Browse Source

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
pull/3/head
Nafies Luthfi 8 years ago
parent
commit
894ba607f1
  1. 1
      .env.example
  2. 13
      app/Couple.php
  3. 15
      app/Http/Controllers/UserMarriagesController.php
  4. 8
      app/User.php
  5. 2
      config/app.php
  6. 13
      database/factories/ModelFactory.php
  7. 14
      resources/lang/en/couple.php
  8. 1
      resources/lang/en/user.php
  9. 14
      resources/lang/id/couple.php
  10. 1
      resources/lang/id/user.php
  11. 29
      resources/views/users/marriages.blade.php
  12. 2
      routes/web.php
  13. 20
      tests/Feature/ManageUserMarriagesTest.php
  14. 37
      tests/Unit/CoupleTest.php
  15. 16
      tests/Unit/UserTest.php

1
.env.example

@ -1,4 +1,3 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true

13
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();
}
}

15
app/Http/Controllers/UserMarriagesController.php

@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserMarriagesController extends Controller
{
public function index(User $user)
{
$marriages = $user->marriages()->with('husband', 'wife')->withCount('childs')->get();
return view('users.marriages', compact('user', 'marriages'));
}
}

8
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))

2
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',
/*
|--------------------------------------------------------------------------

13
database/factories/ModelFactory.php

@ -1,5 +1,6 @@
<?php
use App\Couple;
use App\User;
/*
@ -30,4 +31,16 @@ $factory->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;
},
];
});

14
resources/lang/en/couple.php

@ -0,0 +1,14 @@
<?php
return [
// Labels
'show' => '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',
];

1
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',

14
resources/lang/id/couple.php

@ -0,0 +1,14 @@
<?php
return [
// Labels
'show' => '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',
];

1
resources/lang/id/user.php

@ -16,6 +16,7 @@ return [
'grand_mother' => 'Nenek',
'grand_father' => 'Kakek',
'nieces' => 'Keponakan',
'marriages' => 'Pernikahan',
// Actions
'edit' => 'Edit Profil',

29
resources/views/users/marriages.blade.php

@ -0,0 +1,29 @@
@extends('layouts.app')
@section('content')
<h1 class="page-header">
{{ $user->name }} <small>{{ trans('user.marriages') }}</small>
</h1>
<div class="row">
@foreach ($marriages as $marriage)
<div class="col-md-4">
<div class="panel panel-default">
<table class="table table-condensed">
<tr><th class="col-xs-5">{{ trans('couple.husband') }}</th><td>{{ $marriage->husband->profileLink() }}</th></tr>
<tr><th>{{ trans('couple.wive') }}</th><td>{{ $marriage->wife->profileLink() }}</th></tr>
<tr><th>{{ trans('couple.marriage_date') }}</th><td>{{ $marriage->marriage_date }}</th></tr>
@if ($marriage->divorce_date)
<tr><th>{{ trans('couple.divorce_date') }}</th><td>{{ $marriage->divorce_date }}</th></tr>
@endif
<tr><th>{{ trans('couple.childs_count') }}</th><td>{{ $marriage->childs_count }}</th></tr>
{{-- <tr><th>{{ trans('couple.grand_childs_count') }}</th><td>?</th></tr> --}}
</table>
{{-- <div class="panel-footer">
{{ link_to_route('couples.show', trans('couple.show'), [$user->id], ['class' => 'btn btn-default btn-xs']) }}
</div> --}}
</div>
</div>
@endforeach
</div>
@endsection

2
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
*/

20
tests/Feature/ManageUserMarriagesTest.php

@ -0,0 +1,20 @@
<?php
namespace Tests\Feature;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ManageUserMarriagesTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function user_can_visit_other_user_marriages_page()
{
$user = factory(User::class)->create();
$this->visit(route('users.marriages', $user->id));
$this->see($user->name);
}
}

37
tests/Unit/CoupleTest.php

@ -0,0 +1,37 @@
<?php
namespace Tests\Unit;
use App\Couple;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CoupleTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function a_couple_consists_of_a_husband_and_a_wife()
{
$couple = factory(Couple::class)->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);
}
}

16
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();

Loading…
Cancel
Save