Browse Source

Added user can set wife and husband

Added Couple model and couples table
pull/3/head
Nafies Luthfi 9 years ago
parent
commit
e0b6410a79
  1. 10
      app/Couple.php
  2. 35
      app/Http/Controllers/FamilyActionsController.php
  3. 34
      app/User.php
  4. 4
      database/factories/ModelFactory.php
  5. 35
      database/migrations/2017_06_27_151536_create_couples_table.php
  6. 47
      resources/views/home.blade.php
  7. 2
      routes/web.php
  8. 55
      tests/Feature/ManageUserFamiliesTest.php

10
app/Couple.php

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Couple extends Model
{
//
}

35
app/Http/Controllers/FamilyActionsController.php

@ -14,6 +14,7 @@ class FamilyActionsController extends Controller
]);
$father = new User;
$father->name = $request->get('set_father');
$father->nickname = $request->get('set_father');
$father->gender_id = 1;
@ -29,6 +30,7 @@ class FamilyActionsController extends Controller
]);
$mother = new User;
$mother->name = $request->get('set_mother');
$mother->nickname = $request->get('set_mother');
$mother->gender_id = 2;
@ -45,6 +47,7 @@ class FamilyActionsController extends Controller
]);
$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->save();
@ -56,4 +59,36 @@ class FamilyActionsController extends Controller
return back();
}
public function addWife(Request $request, User $user)
{
$this->validate($request, [
'set_wife' => 'required|string|max:255',
]);
$wife = new User;
$wife->name = $request->get('set_wife');
$wife->nickname = $request->get('set_wife');
$wife->gender_id = 2;
$user->addWife($wife);
return back();
}
public function addHusband(Request $request, User $user)
{
$this->validate($request, [
'set_husband' => 'required|string|max:255',
]);
$husband = new User;
$husband->name = $request->get('set_husband');
$husband->nickname = $request->get('set_husband');
$husband->gender_id = 1;
$user->addHusband($husband);
return back();
}
}

34
app/User.php

@ -42,7 +42,7 @@ class User extends Authenticatable
public function setFather(User $father)
{
if ($father->gender_id === 1) {
if ($father->gender_id == 1) {
if ($father->exists == false)
$father->save();
@ -58,7 +58,7 @@ class User extends Authenticatable
public function setMother(User $mother)
{
if ($mother->gender_id === 2) {
if ($mother->gender_id == 2) {
if ($mother->exists == false)
$mother->save();
@ -95,4 +95,34 @@ class User extends Authenticatable
$linkText = $this->name ?: $this->nickname;
return link_to_route('users.show', $linkText, [$this->id]);
}
public function wifes()
{
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id');
}
public function addWife(User $wife)
{
if ($this->gender_id == 1) {
$this->wifes()->save($wife);
return $wife;
}
return false;
}
public function husbands()
{
return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id');
}
public function addHusband(User $husband)
{
if ($this->gender_id == 2) {
$this->husbands()->save($husband);
return $husband;
}
return false;
}
}

4
database/factories/ModelFactory.php

@ -15,8 +15,10 @@ use App\User;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(User::class, function (Faker\Generator $faker) {
$name = $faker->name;
return [
'nickname' => $faker->name,
'name' => $name,
'nickname' => $name,
'gender_id' => rand(1, 2),
];
});

35
database/migrations/2017_06_27_151536_create_couples_table.php

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCouplesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('couples', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('husband_id')->index();
$table->unsignedInteger('wife_id')->index();
$table->date('marriege_date')->nullable();
$table->date('divorce_date')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('couples');
}
}

47
resources/views/home.blade.php

@ -28,7 +28,7 @@
@if ($currentUser->father_id)
{{ $currentUser->father->profileLink() }}
@else
{{ Form::open(['route' => ['family-actions.set-father', $currentUser->id, $currentUser->id]]) }}
{{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
<div class="input-group">
{{ Form::text('set_father', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn">
@ -56,6 +56,51 @@
@endif
</td>
</tr>
@if ($currentUser->gender_id == 1)
<tr>
<th>Isteri</th>
<td>
@if ($currentUser->wifes->isEmpty() == false)
<ul class="list-group">
@foreach($currentUser->wifes as $wife)
<li class="list-group-item">{{ $wife->profileLink() }}</li>
@endforeach
</ul>
@else
{{ Form::open(['route' => ['family-actions.add-wife', $currentUser->id]]) }}
<div class="input-group">
{{ Form::text('set_wife', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn">
{{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_wife_button']) }}
</span>
</div>
{{ Form::close() }}
@endif
</td>
</tr>
@else
<tr>
<th>Suami</th>
<td>
@if ($currentUser->husbands->isEmpty() == false)
<ul class="list-group">
@foreach($currentUser->husbands as $husband)
<li class="list-group-item">{{ $husband->profileLink() }}</li>
@endforeach
</ul>
@else
{{ Form::open(['route' => ['family-actions.add-husband', $currentUser->id]]) }}
<div class="input-group">
{{ Form::text('set_husband', null, ['class' => 'form-control input-sm']) }}
<span class="input-group-btn">
{{ Form::submit('update', ['class' => 'btn btn-info btn-sm', 'id' => 'set_husband_button']) }}
</span>
</div>
{{ Form::close() }}
@endif
</td>
</tr>
@endif
<tr>
<th colspan="2">Anak-Anak</th>
</tr>

2
routes/web.php

@ -22,5 +22,7 @@ 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');
Route::post('family-actions/{user}/add-child', 'FamilyActionsController@addChild')->name('family-actions.add-child');
Route::post('family-actions/{user}/add-wife', 'FamilyActionsController@addWife')->name('family-actions.add-wife');
Route::post('family-actions/{user}/add-husband', 'FamilyActionsController@addHusband')->name('family-actions.add-husband');
Route::get('users/{user}', 'UsersController@show')->name('users.show');

55
tests/Feature/ManageUserFamiliesTest.php

@ -2,8 +2,9 @@
namespace Tests\Feature;
use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class ManageUserFamiliesTest extends TestCase
{
@ -65,4 +66,56 @@ class ManageUserFamiliesTest extends TestCase
'father_id' => $user->id,
]);
}
/** @test */
public function user_can_set_wife()
{
$user = $this->loginAsUser(['gender_id' => 1]);
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_wife']);
$this->submitForm('set_wife_button', [
'set_wife' => 'Nama Istri',
]);
$this->seeInDatabase('users', [
'nickname' => 'Nama Istri',
'gender_id' => 2,
]);
$wife = User::orderBy('id', 'desc')->first();
$this->seeInDatabase('couples', [
'husband_id' => $user->id,
'wife_id' => $wife->id,
]);
}
/** @test */
public function user_can_set_husband()
{
$user = $this->loginAsUser(['gender_id' => 2]);
$this->visit(route('profile'));
$this->seePageIs(route('profile'));
$this->seeElement('input', ['name' => 'set_husband']);
$this->submitForm('set_husband_button', [
'set_husband' => 'Nama Suami',
]);
$this->seeInDatabase('users', [
'nickname' => 'Nama Suami',
'gender_id' => 1,
]);
$husband = User::orderBy('id', 'desc')->first();
$this->seeInDatabase('couples', [
'husband_id' => $husband->id,
'wife_id' => $user->id,
]);
}
}
Loading…
Cancel
Save