Permalink
Browse files

Lesson 14

  • Loading branch information...
JeffreyWay committed Mar 27, 2017
1 parent 02878b5 commit bb04d5b2d89bb9658090459868bd28233ad6ddf6
@@ -26,11 +26,20 @@ public function __construct()
public function index(Channel $channel)
{
if ($channel->exists) {
$threads = $channel->threads()->latest()->get();
$threads = $channel->threads()->latest();
} else {
$threads = Thread::latest()->get();
$threads = Thread::latest();
}
// if request('by'), we should filter by the given username.
if ($username = request('by')) {
$user = \App\User::where('name', $username)->firstOrFail();
$threads->where('user_id', $user->id);
}
$threads = $threads->get();
return view('threads.index', compact('threads'));
}
@@ -14,7 +14,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
\View::share('channels', Channel::all());
\View::composer('*', function ($view) {
$view->with('channels', Channel::all());
});
}
/**
@@ -44,7 +44,18 @@
<div class="collapse navbar-collapse" id="app-navbar-collapse">
<!-- Left Side Of Navbar -->
<ul class="nav navbar-nav">
<li><a href="/threads">All Threads</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">Browse <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/threads">All Threads</a></li>
@if (auth()->check())
<li><a href="/threads?by={{ auth()->user()->name }}">My Threads</a></li>
@endif
</ul>
</li>
<li>
<a href="/threads/create">New Thread</a>
@@ -52,4 +52,17 @@ function a_user_can_filter_threads_according_to_a_channel()
->assertSee($threadInChannel->title)
->assertDontSee($threadNotInChannel->title);
}
/** @test */
function a_user_can_filter_threads_by_any_username()
{
$this->signIn(create('App\User', ['name' => 'JohnDoe']));
$threadByJohn = create('App\Thread', ['user_id' => auth()->id()]);
$threadNotByJohn = create('App\Thread');
$this->get('threads?by=JohnDoe')
->assertSee($threadByJohn->title)
->assertDontSee($threadNotByJohn->title);
}
}

0 comments on commit bb04d5b

Please sign in to comment.