Browse Source

Added User Profile page and profileLink method to User model

pull/3/head
Nafies Luthfi 9 years ago
parent
commit
58b17d246f
  1. 85
      app/Http/Controllers/UsersController.php
  2. 6
      app/User.php
  3. 14
      resources/views/home.blade.php
  4. 6
      routes/web.php
  5. 20
      tests/Feature/UsersProfileTest.php
  6. 19
      tests/Unit/UserTest.php

85
app/Http/Controllers/UsersController.php

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function show(User $user)
{
return view('home', ['currentUser' => $user]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, User $user)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function destroy(User $user)
{
//
}
}

6
app/User.php

@ -89,4 +89,10 @@ class User extends Authenticatable
return $this->hasMany(User::class, 'father_id'); return $this->hasMany(User::class, 'father_id');
} }
public function profileLink()
{
$linkText = $this->name ?: $this->nickname;
return link_to_route('users.show', $linkText, [$this->id]);
}
} }

14
resources/views/home.blade.php

@ -5,18 +5,18 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">Profile : {{ $currentUser->name }}</div>
<div class="panel-heading">Profile : {{ $currentUser->name ?: $currentUser->nickname }}</div>
<div class="panel-body"> <div class="panel-body">
<table class="table table-condensed"> <table class="table table-condensed">
<tbody> <tbody>
<tr> <tr>
<th>Nama Panggilan</th>
<td>{{ $currentUser->nickname }}</td>
<th class="col-sm-4">Nama Panggilan</th>
<td class="col-sm-6">{{ $currentUser->nickname }}</td>
</tr> </tr>
<tr> <tr>
<th>Nama</th> <th>Nama</th>
<td>{{ $currentUser->name }}</td>
<td>{{ $currentUser->profileLink() }}</td>
</tr> </tr>
<tr> <tr>
<th>Jenis Kelamin</th> <th>Jenis Kelamin</th>
@ -26,7 +26,7 @@
<th>Ayah</th> <th>Ayah</th>
<td> <td>
@if ($currentUser->father_id) @if ($currentUser->father_id)
{{ $currentUser->father->nickname }}
{{ $currentUser->father->profileLink() }}
@else @else
{{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }} {{ Form::open(['route' => ['family-actions.set-father', $currentUser->id]]) }}
<div class="input-group"> <div class="input-group">
@ -43,7 +43,7 @@
<th>Ibu</th> <th>Ibu</th>
<td> <td>
@if ($currentUser->mother_id) @if ($currentUser->mother_id)
{{ $currentUser->mother->nickname }}
{{ $currentUser->mother->profileLink() }}
@else @else
{{ Form::open(['route' => ['family-actions.set-mother', $currentUser->id]]) }} {{ Form::open(['route' => ['family-actions.set-mother', $currentUser->id]]) }}
<div class="input-group"> <div class="input-group">
@ -64,7 +64,7 @@
<ul class="list-group"> <ul class="list-group">
@foreach($currentUser->childs as $child) @foreach($currentUser->childs as $child)
<li class="list-group-item"> <li class="list-group-item">
{{ $child->nickname }} ({{ $child->gender }})
{{ $child->profileLink() }} ({{ $child->gender }})
</li> </li>
@endforeach @endforeach
<li class="list-group-item"> <li class="list-group-item">

6
routes/web.php

@ -17,8 +17,10 @@ Route::get('/', function () {
Auth::routes(); Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/profile', 'HomeController@index')->name('profile');
Route::get('home', 'HomeController@index')->name('home');
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-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}/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-child', 'FamilyActionsController@addChild')->name('family-actions.add-child');
Route::get('users/{user}', 'UsersController@show')->name('users.show');

20
tests/Feature/UsersProfileTest.php

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

19
tests/Unit/UserTest.php

@ -0,0 +1,19 @@
<?php
namespace Tests\Unit;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
class UserTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_have_profile_link()
{
$user = factory(User::class)->create();
$this->assertEquals(link_to_route('users.show', $user->nickname, [$user->id]), $user->profileLink());
}
}
Loading…
Cancel
Save