Skip to content

Commit

Permalink
feat: show posts by user
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Sep 6, 2023
1 parent d992c38 commit 76b0918
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 17 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# CHANGELOG

## Next Release
## v1.0.0 (2023-09-05)

- Completely overhauls all routes to follow RESTful practices
- Paginates and styles the comments on each post page
- Added prompts before deletion on the admin page for categories and posts
- Swap `updated_at` to `created_at` timestamps for posts and comments
- Removes the unusable "Remember Me" checkbox on the login page
- Updated routes
- Removes the `/blog` route
- Changes the `/posts/{category}` route to `/posts/category/{category}` to help distinguish what the slug is
- Adds a new `/posts/user/{user}` route so you can filter posts by author
- Adds a new `/posts/{user}/{slug}` route that works the same as the now legacy `/{user}/{slug}`
- Fixes a 403 issue for the default image preview when editing a post
- Fixes a bug that didn't properly validate reCAPTCHA on user registration
- Fixes the styling of tables to properly show the theme colors (admin panel)
Expand Down
38 changes: 36 additions & 2 deletions src/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Category;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand All @@ -29,7 +30,11 @@ public function showPosts(Request $request): View
$categories = Category::orderBy('category', 'asc')
->get();

return view('posts', compact('posts', 'categories'));
$authors = User::orderBy('name', 'asc')
->where('role', '=', 1)
->get();

return view('posts', compact('posts', 'categories', 'authors'));
}

/**
Expand All @@ -50,7 +55,36 @@ public function showPostsByCategory(Request $request, string $category): View
$categories = Category::orderBy('category', 'asc')
->get();

return view('posts', compact('categoryRecord', 'posts', 'categories'));
$authors = User::orderBy('name', 'asc')
->where('role', '=', 1)
->get();

return view('posts', compact('categoryRecord', 'posts', 'categories', 'authors'));
}

/**
* Show the "posts" page and filter by author (user).
*
* @param Request $request
* @param string $user
* @return View
*/
public function showPostsByUser(Request $request, string $user): View
{
$userRecord = User::where('name', '=', $user)->firstOrFail();
$posts = Post::orderBy('created_at', 'desc')
->where('published', '=', 1)
->where('user_id', '=', $userRecord->id)
->paginate(10);

$categories = Category::orderBy('category', 'asc')
->get();

$authors = User::orderBy('name', 'asc')
->where('role', '=', 1)
->get();

return view('posts', compact('userRecord', 'posts', 'categories', 'authors'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/resources/views/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
<i class="fas fa-calendar"></i>
{{ date_format($post->created_at, 'Y/m/d') }}
<i class="fas fa-user"></i>
{{ $post->user->name }}
<a href="{{ '/posts/user/' . $post->user->name }}">{{ $post->user->name }}</a>
<i class="fas fa-clock"></i>
{{ \App\Http\Controllers\PostController::generateReadingTime($post) }} minutes
<i class="fas fa-tag"></i>
@if (isset($post->category->category))
<a href="{{ '/posts/' . $post->category->category }}">{{ $post->category->category }}</a>
<a href="{{ '/posts/category/' . $post->category->category }}">{{ $post->category->category }}</a>
@else
{{ 'Uncategorized' }}
@endif
Expand Down
12 changes: 10 additions & 2 deletions src/resources/views/posts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class="banner-image">
{{ \App\Http\Controllers\PostController::generateReadingTime($post) }} minutes
<i class="fas fa-tag"></i>
@if (isset($post->category->category))
<a href="{{ '/posts/' . $post->category->category }}">{{ $post->category->category }}</a>
{{ $post->category->category }}
@else
{{ 'Uncategorized' }}
@endif
Expand All @@ -54,10 +54,18 @@ class="banner-image">
{{ $posts->links() }}
</div>

<h3>Categories</h3>
<a href="/posts" class="btn btn-sm btn-primary category-button">All Posts</a>
@foreach ($categories as $category)
<a href="/posts/{{ $category->category }}"
<a href="/posts/category/{{ $category->category }}"
class="btn btn-sm btn-primary category-button">{{ $category->category }}</a>
@endforeach

<h3 class="mt-3">Users</h3>
<a href="/posts" class="btn btn-sm btn-primary category-button">All Users</a>
@foreach ($authors as $author)
<a href="/posts/user/{{ $author->name }}"
class="btn btn-sm btn-primary category-button">{{ $author->name }}</a>
@endforeach
</div>
@endsection
13 changes: 5 additions & 8 deletions src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
Route::get('/logout', [App\Http\Controllers\Auth\LoginController::class, 'logout']);
Auth::routes();

// Landing pages
// Post pages
Route::get('/', [App\Http\Controllers\PostController::class, 'showPosts']);
Route::get('/blog', [App\Http\Controllers\PostController::class, 'showPosts']);
Route::get('/posts', [App\Http\Controllers\PostController::class, 'showPosts']);

// Misc
Route::get('/posts/{category}', [App\Http\Controllers\PostController::class, 'showPostsByCategory']);
Route::get('/{user}/{slug}', [App\Http\Controllers\PostController::class, 'showPost']);
Route::get('/posts/category/{category}', [App\Http\Controllers\PostController::class, 'showPostsByCategory']);
Route::get('/posts/user/{user}', [App\Http\Controllers\PostController::class, 'showPostsByUser']);
Route::get('/posts/{user}/{slug}', [App\Http\Controllers\PostController::class, 'showPost']);
Route::get('/{user}/{slug}', [App\Http\Controllers\PostController::class, 'showPost']); // Kept for legacy links, should instead us the route prepended with `/posts`
Route::get('/feed', [App\Http\Controllers\RssFeedController::class, 'getFeed']);


// Must be an Admin
Route::middleware('Admin')->group(function () {
// General
Expand All @@ -55,7 +53,6 @@
Route::delete('/users/{id}', [App\Http\Controllers\UserController::class, 'delete']);
});


// Must be logged in
Route::middleware('auth')->group(function () {
// Comments
Expand Down
28 changes: 26 additions & 2 deletions src/tests/Feature/Controller/PostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public function testShowPosts()

$this->assertGreaterThanOrEqual(1, count($viewData['posts']));
$this->assertGreaterThanOrEqual(1, count($viewData['categories']));
$this->assertGreaterThanOrEqual(1, count($viewData['authors']));
}

/**
* Tests that we return the posts page and data correctly.
* Tests that we return the posts page and data correctly when filtering by category.
*
* @return void
*/
Expand All @@ -46,14 +47,37 @@ public function testShowPostsByCategory()
$category = Category::factory()->create();
Post::factory(['category_id' => $category->id])->create();

$request = Request::create("/posts/$category->category", 'GET');
$request = Request::create("/posts/category/$category->category", 'GET');
$response = $controller->showPostsByCategory($request, $category->category);

$viewData = $response->getData();

$this->assertEquals($category->category, $viewData['categoryRecord']['category']);
$this->assertGreaterThanOrEqual(1, count($viewData['posts']));
$this->assertGreaterThanOrEqual(1, count($viewData['categories']));
$this->assertGreaterThanOrEqual(1, count($viewData['authors']));
}

/**
* Tests that we return the posts page and data correctly when filtering by user.
*
* @return void
*/
public function testShowPostsByUser()
{
$controller = new PostController();
$user = User::find(1);
Post::factory(['user_id' => $user->id])->create();

$request = Request::create("/posts/user/$user->name", 'GET');
$response = $controller->showPostsByUser($request, $user->name);

$viewData = $response->getData();

$this->assertEquals($user->name, $viewData['userRecord']['name']);
$this->assertGreaterThanOrEqual(1, count($viewData['posts']));
$this->assertGreaterThanOrEqual(1, count($viewData['categories']));
$this->assertGreaterThanOrEqual(1, count($viewData['authors']));
}

/**
Expand Down

0 comments on commit 76b0918

Please sign in to comment.