diff --git a/app/Couple.php b/app/Couple.php index 2908730..b801c19 100644 --- a/app/Couple.php +++ b/app/Couple.php @@ -2,8 +2,8 @@ namespace App; -use Illuminate\Database\Eloquent\Model; use Ramsey\Uuid\Uuid; +use Illuminate\Database\Eloquent\Model; class Couple extends Model { @@ -26,7 +26,7 @@ class Couple extends Model public function childs() { - return $this->hasMany(User::class, 'parent_id'); + return $this->hasMany(User::class, 'parent_id')->orderBy('birth_order'); } public function addChild(User $user) diff --git a/app/Http/Controllers/FamilyActionsController.php b/app/Http/Controllers/FamilyActionsController.php index 037975e..883b385 100644 --- a/app/Http/Controllers/FamilyActionsController.php +++ b/app/Http/Controllers/FamilyActionsController.php @@ -2,10 +2,10 @@ namespace App\Http\Controllers; -use App\Couple; use App\User; -use Illuminate\Http\Request; +use App\Couple; use Ramsey\Uuid\Uuid; +use Illuminate\Http\Request; class FamilyActionsController extends Controller { @@ -60,9 +60,10 @@ class FamilyActionsController extends Controller public function addChild(Request $request, User $user) { $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', + 'add_child_name' => 'required|string|max:255', + 'add_child_gender_id' => 'required|in:1,2', + 'add_child_parent_id' => 'nullable|exists:couples,id', + 'add_child_birth_order' => 'nullable|numeric', ]); $child = new User; @@ -71,6 +72,7 @@ class FamilyActionsController extends Controller $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'); + $child->birth_order = $request->get('add_child_birth_order'); $child->manager_id = auth()->id(); \DB::beginTransaction(); diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 7ade80f..a2ebe8b 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -123,30 +123,7 @@ class UsersController extends Controller */ public function update(UpdateRequest $request, User $user) { - $request->validated(); - - $user->nickname = $request->nickname; - $user->name = $request->get('name'); - $user->gender_id = $request->get('gender_id'); - $user->dob = $request->get('dob'); - $user->dod = $request->get('dod'); - - if ($request->get('dod')) { - $user->yod = substr($request->get('dod'), 0, 4); - } else { - $user->yod = $request->get('yod'); - } - - $user->phone = $request->get('phone'); - $user->address = $request->get('address'); - $user->city = $request->get('city'); - $user->email = $request->get('email'); - - if ($request->get('password')) { - $user->password = bcrypt($request->get('password')); - } - - $user->save(); + $user->update($request->validated()); return redirect()->route('users.show', $user->id); } diff --git a/app/Http/Requests/Users/UpdateRequest.php b/app/Http/Requests/Users/UpdateRequest.php index e82c2b7..b77795c 100644 --- a/app/Http/Requests/Users/UpdateRequest.php +++ b/app/Http/Requests/Users/UpdateRequest.php @@ -26,17 +26,18 @@ class UpdateRequest extends FormRequest public function rules() { return [ - 'nickname' => 'required|string|max:255', - 'name' => 'required|string|max:255', - 'gender_id' => 'required|numeric', - 'dob' => 'nullable|date|date_format:Y-m-d', - 'dod' => 'nullable|date|date_format:Y-m-d', - 'yod' => 'nullable|date_format:Y', - 'phone' => 'nullable|string|max:255', - 'address' => 'nullable|string|max:255', - 'city' => 'nullable|string|max:255', - 'email' => 'nullable|string|max:255', - 'password' => 'nullable|min:6|max:15', + 'nickname' => 'required|string|max:255', + 'name' => 'required|string|max:255', + 'gender_id' => 'required|numeric', + 'dob' => 'nullable|date|date_format:Y-m-d', + 'dod' => 'nullable|date|date_format:Y-m-d', + 'yod' => 'nullable|date_format:Y', + 'phone' => 'nullable|string|max:255', + 'address' => 'nullable|string|max:255', + 'city' => 'nullable|string|max:255', + 'email' => 'nullable|string|max:255', + 'password' => 'nullable|min:6|max:15', + 'birth_order' => 'nullable|numeric|min:1', ]; } @@ -47,4 +48,21 @@ class UpdateRequest extends FormRequest 'new_password.same_password' => trans('passwords.same_password'), ]; } + + public function validated() + { + $formData = parent::validated(); + + if ($formData['dod']) { + $formData['yod'] = substr($formData['dod'], 0, 4); + } else { + $formData['yod'] = $formData['yod']; + } + + if ($formData['password']) { + $formData['password'] = bcrypt($formData['password']); + } + + return $formData; + } } diff --git a/app/User.php b/app/User.php index 9a10f7e..cfdf3e2 100644 --- a/app/User.php +++ b/app/User.php @@ -23,11 +23,11 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'id', + 'id', 'birth_order', 'nickname', 'gender_id', 'name', 'email', 'password', 'address', 'phone', - 'dof', 'dod', + 'dob', 'dod', 'yod', 'city', 'father_id', 'mother_id', 'parent_id', ]; @@ -104,10 +104,10 @@ class User extends Authenticatable public function childs() { if ($this->gender_id == 2) { - return $this->hasMany(User::class, 'mother_id'); + return $this->hasMany(User::class, 'mother_id')->orderBy('birth_order'); } - return $this->hasMany(User::class, 'father_id'); + return $this->hasMany(User::class, 'father_id')->orderBy('birth_order'); } public function profileLink($type = 'profile') @@ -206,7 +206,7 @@ class User extends Authenticatable } }) - ->get(); + ->orderBy('birth_order')->get(); } public function parent() diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index a0e407c..31bd368 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -1,8 +1,8 @@ uuid('mother_id')->nullable(); $table->uuid('parent_id')->nullable(); $table->date('dob')->nullable(); + $table->unsignedTinyInteger('birth_order')->nullable(); $table->date('dod')->nullable(); $table->date('yod')->nullable(); $table->string('email')->unique()->nullable(); diff --git a/resources/lang/en/user.php b/resources/lang/en/user.php index 5a2bf49..7b67031 100644 --- a/resources/lang/en/user.php +++ b/resources/lang/en/user.php @@ -17,6 +17,7 @@ return [ 'grand_father' => 'Grand Father', 'nieces' => 'Nieces', 'marriages' => 'Marriages', + 'birth_order' => 'Birth Order', // Actions 'edit' => 'Edit Profile', diff --git a/resources/lang/id/user.php b/resources/lang/id/user.php index efa3977..e2425e0 100644 --- a/resources/lang/id/user.php +++ b/resources/lang/id/user.php @@ -17,6 +17,7 @@ return [ 'grand_father' => 'Kakek', 'nieces' => 'Keponakan', 'marriages' => 'Pernikahan', + 'birth_order' => 'Anak ke', // Actions 'edit' => 'Edit Profil', diff --git a/resources/views/users/edit.blade.php b/resources/views/users/edit.blade.php index 479281a..6414677 100644 --- a/resources/views/users/edit.blade.php +++ b/resources/views/users/edit.blade.php @@ -70,6 +70,11 @@
{!! FormField::text('dob', ['label' => trans('user.dob'), 'placeholder' => trans('app.example').' 1959-07-20']) !!}
+
+ {!! FormField::text('birth_order', ['label' => trans('user.birth_order'), 'type' => 'number', 'min' => 1]) !!} +
+
+
{!! FormField::text('yod', ['label' => trans('user.yod'), 'placeholder' => trans('app.example').' 2003']) !!}
{!! FormField::text('dod', ['label' => trans('user.dod'), 'placeholder' => trans('app.example').' 2003-10-17']) !!}
@@ -78,7 +83,7 @@
-

{{ trans('app.address') }} & {{ trans('app.contact') }}

+

{{ trans('app.address') }} & {{ trans('app.contact') }}

{!! FormField::textarea('address', ['label' => trans('app.address')]) !!} {!! FormField::text('city', ['label' => trans('app.city'), 'placeholder' => trans('app.example').' Jakarta']) !!} diff --git a/resources/views/users/partials/childs.blade.php b/resources/views/users/partials/childs.blade.php index 6f6e29b..6db3f79 100644 --- a/resources/views/users/partials/childs.blade.php +++ b/resources/views/users/partials/childs.blade.php @@ -2,10 +2,10 @@
@can ('edit', $user)
- {{ link_to_route('users.show', trans('user.add_child'), [$user->id, 'action' => 'add_child'], ['class' => 'btn btn-success btn-xs']) }} + {{ link_to_route('users.show', __('user.add_child'), [$user->id, 'action' => 'add_child'], ['class' => 'btn btn-success btn-xs']) }}
@endcan -

{{ trans('user.childs') }} ({{ $user->childs->count() }})

+

{{ __('user.childs') }} ({{ $user->childs->count() }})

    @@ -14,26 +14,34 @@ {{ $child->profileLink() }} ({{ $child->gender }}) @empty -
  • {{ trans('app.childs_were_not_recorded') }}
  • +
  • {{ __('app.childs_were_not_recorded') }}
  • @endforelse @can('edit', $user) @if (request('action') == 'add_child')
  • {{ Form::open(['route' => ['family-actions.add-child', $user->id]]) }}
    -
    - {!! FormField::text('add_child_name', ['label' => trans('user.child_name')]) !!} +
    + {!! FormField::text('add_child_name', ['label' => __('user.child_name')]) !!}
    -
    - {!! FormField::radios('add_child_gender_id', [1 => trans('app.male'), 2 => trans('app.female')], ['label' => trans('user.child_gender')]) !!} +
    + {!! FormField::radios('add_child_gender_id', [1 => __('app.male'), 2 => __('app.female')], ['label' => __('user.child_gender')]) !!}
    - {!! FormField::select('add_child_parent_id', $usersMariageList, ['label' => trans('user.add_child_from_existing_couples', ['name' => $user->name]), 'placeholder' => trans('app.unknown')]) !!} - {{ Form::submit(trans('user.add_child'), ['class' => 'btn btn-success btn-sm']) }} - {{ link_to_route('users.show', trans('app.cancel'), [$user->id], ['class' => 'btn btn-default btn-sm']) }} +
    +
    + {!! FormField::select('add_child_parent_id', $usersMariageList, ['label' => __('user.add_child_from_existing_couples', ['name' => $user->name]), 'placeholder' => __('app.unknown')]) !!} +
    +
    + {!! FormField::text('add_child_birth_order', ['label' => __('user.birth_order'), 'type' => 'number', 'min' => 1]) !!} +
    +
    + + {{ Form::submit(__('user.add_child'), ['class' => 'btn btn-success btn-sm']) }} + {{ link_to_route('users.show', __('app.cancel'), [$user->id], ['class' => 'btn btn-default btn-sm']) }} {{ Form::close() }}
  • @endif @endcan
-
\ No newline at end of file +
diff --git a/resources/views/users/partials/profile.blade.php b/resources/views/users/partials/profile.blade.php index c39e9b8..bc58061 100644 --- a/resources/views/users/partials/profile.blade.php +++ b/resources/views/users/partials/profile.blade.php @@ -7,9 +7,7 @@ {{ trans('user.name') }} - - {{ $user->profileLink() }} - + {{ $user->profileLink() }} {{ trans('user.nickname') }} @@ -23,6 +21,10 @@ {{ trans('user.dob') }} {{ $user->dob }} + + {{ trans('user.birth_order') }} + {{ $user->birth_order }} + @if ($user->dod) {{ trans('user.dod') }} diff --git a/tests/Feature/ManageUserFamiliesTest.php b/tests/Feature/ManageUserFamiliesTest.php index 506a214..3041636 100644 --- a/tests/Feature/ManageUserFamiliesTest.php +++ b/tests/Feature/ManageUserFamiliesTest.php @@ -3,8 +3,8 @@ namespace Tests\Feature; use App\User; -use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; +use Illuminate\Foundation\Testing\RefreshDatabase; class ManageUserFamiliesTest extends TestCase { @@ -114,6 +114,33 @@ class ManageUserFamiliesTest extends TestCase } /** @test */ + public function user_can_add_children_with_birth_order() + { + $user = $this->loginAsUser(['gender_id' => 1]); + $this->visit(route('profile')); + $this->seePageIs(route('profile')); + $this->click(trans('user.add_child')); + $this->seeElement('input', ['name' => 'add_child_birth_order']); + + $this->submitForm(trans('user.add_child'), [ + 'add_child_name' => 'Nama Anak 1', + 'add_child_gender_id' => 1, + 'add_child_birth_order' => 2, + 'add_child_parent_id' => '', + ]); + + $this->seeInDatabase('users', [ + 'nickname' => 'Nama Anak 1', + 'gender_id' => 1, + 'father_id' => $user->id, + 'mother_id' => null, + 'parent_id' => null, + 'manager_id' => $user->id, + 'birth_order' => 2, + ]); + } + + /** @test */ public function user_can_set_wife() { $user = $this->loginAsUser(['gender_id' => 1]); diff --git a/tests/Feature/UsersProfileTest.php b/tests/Feature/UsersProfileTest.php index 6d8e4e1..d92cdb9 100644 --- a/tests/Feature/UsersProfileTest.php +++ b/tests/Feature/UsersProfileTest.php @@ -27,31 +27,33 @@ class UsersProfileTest extends TestCase $this->seePageIs(route('users.edit', $user->id)); $this->submitForm(trans('app.update'), [ - 'nickname' => 'Nama Panggilan', - 'name' => 'Nama User', - 'gender_id' => 1, - 'dob' => '1959-06-09', - 'dod' => '2003-10-17', - 'yod' => '', - 'address' => 'Jln. Angkasa, No. 70', - 'city' => 'Nama Kota', - 'phone' => '081234567890', - 'email' => '', - 'password' => '', + 'nickname' => 'Nama Panggilan', + 'name' => 'Nama User', + 'gender_id' => 1, + 'dob' => '1959-06-09', + 'dod' => '2003-10-17', + 'yod' => '', + 'address' => 'Jln. Angkasa, No. 70', + 'city' => 'Nama Kota', + 'phone' => '081234567890', + 'email' => '', + 'password' => '', + 'birth_order' => 3, ]); $this->seeInDatabase('users', [ - 'nickname' => 'Nama Panggilan', - 'name' => 'Nama User', - 'gender_id' => 1, - 'dob' => '1959-06-09', - 'dod' => '2003-10-17', - 'yod' => '2003', - 'address' => 'Jln. Angkasa, No. 70', - 'city' => 'Nama Kota', - 'phone' => '081234567890', - 'email' => null, - 'password' => null, + 'nickname' => 'Nama Panggilan', + 'name' => 'Nama User', + 'gender_id' => 1, + 'dob' => '1959-06-09', + 'dod' => '2003-10-17', + 'yod' => '2003', + 'address' => 'Jln. Angkasa, No. 70', + 'city' => 'Nama Kota', + 'phone' => '081234567890', + 'email' => null, + 'password' => null, + 'birth_order' => 3, ]); }