refactor: cruddify user posts

This commit is contained in:
Roardom
2022-12-24 21:48:43 -06:00
parent 417a981143
commit e4ce253493
7 changed files with 47 additions and 29 deletions
@@ -0,0 +1,34 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\User;
class PostController extends Controller
{
/**
* Show user posts.
*/
public function index(string $username): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$user = User::where('username', '=', $username)->sole();
$posts = $user->posts()->with(['topic', 'user'])->latest()->paginate(25);
return \view('user.post.index', [
'posts' => $posts,
'user' => $user,
]);
}
}
@@ -23,7 +23,6 @@ use App\Models\Group;
use App\Models\History;
use App\Models\Invite;
use App\Models\Peer;
use App\Models\Post;
use App\Models\Topic;
use App\Models\Torrent;
use App\Models\TorrentRequest;
@@ -157,21 +156,6 @@ class UserController extends Controller
]);
}
/**
* User Posts.
*/
public function posts(string $username): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$user = User::where('username', '=', $username)->firstOrFail();
$results = Post::selectRaw('posts.id as id,posts.*')->with(['topic', 'user'])->leftJoin('topics', 'posts.topic_id', '=', 'topics.id')->where('posts.user_id', '=', $user->id)->latest('posts.created_at')->paginate(25);
return \view('user.post.index', [
'route' => 'forum',
'results' => $results,
'user' => $user,
]);
}
/**
* Edit Profile Form.
*/
@@ -203,7 +203,7 @@
</dl>
<dl class="post__author-posts">
<dt>
<a href="{{ route('user_posts', ['username' => $post->user->username]) }}">
<a href="{{ route('users.posts.index', ['username' => $post->user->username]) }}">
{{ __('forum.posts') }}
</a>
</dt>
+4 -4
View File
@@ -263,7 +263,7 @@
|| auth()->user()->isAllowed($user, 'follower', 'show_follower')
)
<li class="nav-tab-menu">
<span class="{{ Route::is('users.achievements.*', 'user_topics', 'user_posts', 'user_followers') ? 'nav-tab--active__link' : 'nav-tab__link' }}">
<span class="{{ Route::is('users.achievements.*', 'user_topics', 'users.posts.index', 'user_followers') ? 'nav-tab--active__link' : 'nav-tab__link' }}">
{{ __('forum.activity') }}
</span>
<ul class="nav-tab-menu__items">
@@ -288,10 +288,10 @@
</li>
@endif
@if (auth()->user()->isAllowed($user, 'forum', 'show_post'))
<li class="{{ Route::is('user_posts') ? 'nav-tab--active' : 'nav-tavV2' }}">
<li class="{{ Route::is('users.posts.index') ? 'nav-tab--active' : 'nav-tavV2' }}">
<a
class="{{ Route::is('user_posts') ? 'nav-tab--active__link' : 'nav-tab__link' }}"
href="{{ route('user_posts', ['username' => $user->username]) }}"
class="{{ Route::is('users.posts.index') ? 'nav-tab--active__link' : 'nav-tab__link' }}"
href="{{ route('users.posts.index', ['username' => $user->username]) }}"
>
{{ __('user.posts') }}
</a>
+2 -2
View File
@@ -47,7 +47,7 @@
</tr>
</thead>
<tbody>
@foreach ($results as $r)
@foreach ($posts as $r)
@if ($r->topic->viewable())
<tr>
<td class="f-display-topic-icon"><span
@@ -118,7 +118,7 @@
</table>
</div>
<div class="text-center col-md-12">
{{ $results->links() }}
{{ $posts->links() }}
</div>
</div>
+5 -5
View File
@@ -413,6 +413,11 @@ Route::group(['middleware' => 'language'], function () {
Route::group(['prefix' => 'achievements', 'as' => 'achievements.'], function () {
Route::get('/', [App\Http\Controllers\User\AchievementsController::class, 'index'])->name('index');
});
// Posts
Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', [App\Http\Controllers\User\PostController::class, 'index'])->name('index');
});
});
Route::group(['middleware' => ['auth', 'twostep', 'banned']], function () {
@@ -479,11 +484,6 @@ Route::group(['middleware' => 'language'], function () {
Route::post('/{username}/flushOwnGhostPeers', [App\Http\Controllers\User\UserController::class, 'flushOwnGhostPeers'])->name('flush_own_ghost_peers');
});
// Posts
Route::group(['prefix' => 'users'], function () {
Route::get('/{username}/posts', [App\Http\Controllers\User\UserController::class, 'posts'])->name('user_posts');
});
// Private Messages
Route::group(['prefix' => 'mail'], function () {
Route::post('/searchPMInbox', [App\Http\Controllers\User\PrivateMessageController::class, 'searchPMInbox'])->name('searchPMInbox');
@@ -380,7 +380,7 @@ class UserControllerTest extends TestCase
$user = User::factory()->create();
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('user_posts', ['username' => $user->username]));
$response = $this->actingAs($user)->get(route('users.posts.index', ['username' => $user->username]));
$response->assertOk();
$response->assertViewIs('user.post.index');