fixing thsu frontend views

This commit is contained in:
Micha
2026-01-16 01:43:07 +01:00
parent fd29b928d8
commit f9de433545
27 changed files with 538 additions and 51 deletions

View File

@@ -12,9 +12,13 @@ class ThreadController extends Controller
{
public function index(Request $request): JsonResponse
{
$query = Thread::query()->withoutTrashed()->with([
'user' => fn ($query) => $query->withCount('posts')->with('rank'),
]);
$query = Thread::query()
->withoutTrashed()
->withCount('posts')
->with([
'user' => fn ($query) => $query->withCount('posts')->with('rank'),
'latestPost.user',
]);
$forumParam = $request->query('forum');
if (is_string($forumParam)) {
@@ -34,9 +38,12 @@ class ThreadController extends Controller
public function show(Thread $thread): JsonResponse
{
$thread->increment('views_count');
$thread->refresh();
$thread->loadMissing([
'user' => fn ($query) => $query->withCount('posts')->with('rank'),
]);
'latestPost.user',
])->loadCount('posts');
return response()->json($this->serializeThread($thread));
}
@@ -62,6 +69,11 @@ class ThreadController extends Controller
'body' => $data['body'],
]);
$thread->loadMissing([
'user' => fn ($query) => $query->withCount('posts')->with('rank'),
'latestPost.user',
])->loadCount('posts');
return response()->json($this->serializeThread($thread), 201);
}
@@ -99,18 +111,26 @@ class ThreadController extends Controller
'body' => $thread->body,
'forum' => "/api/forums/{$thread->forum_id}",
'user_id' => $thread->user_id,
'posts_count' => $thread->posts_count ?? 0,
'views_count' => $thread->views_count ?? 0,
'user_name' => $thread->user?->name,
'user_avatar_url' => $thread->user?->avatar_path
? Storage::url($thread->user->avatar_path)
: null,
'user_posts_count' => $thread->user?->posts_count,
'user_created_at' => $thread->user?->created_at?->toIso8601String(),
'user_location' => $thread->user?->location,
'user_rank_name' => $thread->user?->rank?->name,
'user_rank_badge_type' => $thread->user?->rank?->badge_type,
'user_rank_badge_text' => $thread->user?->rank?->badge_text,
'user_rank_badge_url' => $thread->user?->rank?->badge_image_path
? Storage::url($thread->user->rank->badge_image_path)
: null,
'last_post_at' => $thread->latestPost?->created_at?->toIso8601String()
?? $thread->created_at?->toIso8601String(),
'last_post_user_id' => $thread->latestPost?->user_id ?? $thread->user_id,
'last_post_user_name' => $thread->latestPost?->user?->name
?? $thread->user?->name,
'created_at' => $thread->created_at?->toIso8601String(),
'updated_at' => $thread->updated_at?->toIso8601String(),
];