fixing thsu frontend views
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Forum;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -11,7 +12,10 @@ class ForumController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$query = Forum::query()->withoutTrashed();
|
||||
$query = Forum::query()
|
||||
->withoutTrashed()
|
||||
->withCount(['threads', 'posts'])
|
||||
->withSum('threads', 'views_count');
|
||||
|
||||
$parentParam = $request->query('parent');
|
||||
if (is_array($parentParam) && array_key_exists('exists', $parentParam)) {
|
||||
@@ -35,15 +39,24 @@ class ForumController extends Controller
|
||||
$forums = $query
|
||||
->orderBy('position')
|
||||
->orderBy('name')
|
||||
->get()
|
||||
->map(fn (Forum $forum) => $this->serializeForum($forum));
|
||||
->get();
|
||||
|
||||
return response()->json($forums);
|
||||
$forumIds = $forums->pluck('id')->all();
|
||||
$lastPostByForum = $this->loadLastPostsByForum($forumIds);
|
||||
|
||||
$payload = $forums->map(
|
||||
fn (Forum $forum) => $this->serializeForum($forum, $lastPostByForum[$forum->id] ?? null)
|
||||
);
|
||||
|
||||
return response()->json($payload);
|
||||
}
|
||||
|
||||
public function show(Forum $forum): JsonResponse
|
||||
{
|
||||
return response()->json($this->serializeForum($forum));
|
||||
$forum->loadCount(['threads', 'posts'])
|
||||
->loadSum('threads', 'views_count');
|
||||
$lastPost = $this->loadLastPostForForum($forum->id);
|
||||
return response()->json($this->serializeForum($forum, $lastPost));
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
@@ -83,7 +96,11 @@ class ForumController extends Controller
|
||||
'position' => ($position ?? 0) + 1,
|
||||
]);
|
||||
|
||||
return response()->json($this->serializeForum($forum), 201);
|
||||
$forum->loadCount(['threads', 'posts'])
|
||||
->loadSum('threads', 'views_count');
|
||||
$lastPost = $this->loadLastPostForForum($forum->id);
|
||||
|
||||
return response()->json($this->serializeForum($forum, $lastPost), 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Forum $forum): JsonResponse
|
||||
@@ -127,7 +144,11 @@ class ForumController extends Controller
|
||||
|
||||
$forum->save();
|
||||
|
||||
return response()->json($this->serializeForum($forum));
|
||||
$forum->loadCount(['threads', 'posts'])
|
||||
->loadSum('threads', 'views_count');
|
||||
$lastPost = $this->loadLastPostForForum($forum->id);
|
||||
|
||||
return response()->json($this->serializeForum($forum, $lastPost));
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Forum $forum): JsonResponse
|
||||
@@ -180,7 +201,7 @@ class ForumController extends Controller
|
||||
return null;
|
||||
}
|
||||
|
||||
private function serializeForum(Forum $forum): array
|
||||
private function serializeForum(Forum $forum, ?Post $lastPost): array
|
||||
{
|
||||
return [
|
||||
'id' => $forum->id,
|
||||
@@ -189,8 +210,54 @@ class ForumController extends Controller
|
||||
'type' => $forum->type,
|
||||
'parent' => $forum->parent_id ? "/api/forums/{$forum->parent_id}" : null,
|
||||
'position' => $forum->position,
|
||||
'threads_count' => $forum->threads_count ?? 0,
|
||||
'posts_count' => $forum->posts_count ?? 0,
|
||||
'views_count' => (int) ($forum->threads_sum_views_count ?? 0),
|
||||
'last_post_at' => $lastPost?->created_at?->toIso8601String(),
|
||||
'last_post_user_id' => $lastPost?->user_id,
|
||||
'last_post_user_name' => $lastPost?->user?->name,
|
||||
'created_at' => $forum->created_at?->toIso8601String(),
|
||||
'updated_at' => $forum->updated_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
private function loadLastPostsByForum(array $forumIds): array
|
||||
{
|
||||
if (empty($forumIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$posts = Post::query()
|
||||
->select('posts.*', 'threads.forum_id as forum_id')
|
||||
->join('threads', 'posts.thread_id', '=', 'threads.id')
|
||||
->whereIn('threads.forum_id', $forumIds)
|
||||
->whereNull('posts.deleted_at')
|
||||
->whereNull('threads.deleted_at')
|
||||
->orderByDesc('posts.created_at')
|
||||
->with('user')
|
||||
->get();
|
||||
|
||||
$byForum = [];
|
||||
foreach ($posts as $post) {
|
||||
$forumId = (int) ($post->forum_id ?? 0);
|
||||
if ($forumId && !array_key_exists($forumId, $byForum)) {
|
||||
$byForum[$forumId] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
return $byForum;
|
||||
}
|
||||
|
||||
private function loadLastPostForForum(int $forumId): ?Post
|
||||
{
|
||||
return Post::query()
|
||||
->select('posts.*')
|
||||
->join('threads', 'posts.thread_id', '=', 'threads.id')
|
||||
->where('threads.forum_id', $forumId)
|
||||
->whereNull('posts.deleted_at')
|
||||
->whereNull('threads.deleted_at')
|
||||
->orderByDesc('posts.created_at')
|
||||
->with('user')
|
||||
->first();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user