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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +94,7 @@ class PostController extends Controller
|
||||
: null,
|
||||
'user_posts_count' => $post->user?->posts_count,
|
||||
'user_created_at' => $post->user?->created_at?->toIso8601String(),
|
||||
'user_location' => $post->user?->location,
|
||||
'user_rank_name' => $post->user?->rank?->name,
|
||||
'user_rank_badge_type' => $post->user?->rank?->badge_type,
|
||||
'user_rank_badge_text' => $post->user?->rank?->badge_text,
|
||||
|
||||
20
app/Http/Controllers/StatsController.php
Normal file
20
app/Http/Controllers/StatsController.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Thread;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class StatsController extends Controller
|
||||
{
|
||||
public function __invoke(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'threads' => Thread::query()->withoutTrashed()->count(),
|
||||
'posts' => Post::query()->withoutTrashed()->count(),
|
||||
'users' => User::query()->count(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
|
||||
@@ -22,6 +22,7 @@ class UserController extends Controller
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar_url' => $this->resolveAvatarUrl($user),
|
||||
'location' => $user->location,
|
||||
'rank' => $user->rank ? [
|
||||
'id' => $user->rank->id,
|
||||
'name' => $user->rank->name,
|
||||
@@ -45,6 +46,7 @@ class UserController extends Controller
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar_url' => $this->resolveAvatarUrl($user),
|
||||
'location' => $user->location,
|
||||
'rank' => $user->rank ? [
|
||||
'id' => $user->rank->id,
|
||||
'name' => $user->rank->name,
|
||||
@@ -59,6 +61,7 @@ class UserController extends Controller
|
||||
'id' => $user->id,
|
||||
'name' => $user->name,
|
||||
'avatar_url' => $this->resolveAvatarUrl($user),
|
||||
'location' => $user->location,
|
||||
'rank' => $user->rank ? [
|
||||
'id' => $user->rank->id,
|
||||
'name' => $user->rank->name,
|
||||
@@ -67,6 +70,42 @@ class UserController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateMe(Request $request): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
if (!$user) {
|
||||
return response()->json(['message' => 'Unauthenticated.'], 401);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'location' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$location = isset($data['location']) ? trim($data['location']) : null;
|
||||
if ($location === '') {
|
||||
$location = null;
|
||||
}
|
||||
|
||||
$user->forceFill([
|
||||
'location' => $location,
|
||||
])->save();
|
||||
|
||||
$user->loadMissing('rank');
|
||||
|
||||
return response()->json([
|
||||
'id' => $user->id,
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar_url' => $this->resolveAvatarUrl($user),
|
||||
'location' => $user->location,
|
||||
'rank' => $user->rank ? [
|
||||
'id' => $user->rank->id,
|
||||
'name' => $user->rank->name,
|
||||
] : null,
|
||||
'roles' => $user->roles()->pluck('name')->values(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateRank(Request $request, User $user): JsonResponse
|
||||
{
|
||||
$actor = $request->user();
|
||||
|
||||
@@ -4,6 +4,9 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@@ -60,4 +63,20 @@ class Forum extends Model
|
||||
{
|
||||
return $this->hasMany(Thread::class);
|
||||
}
|
||||
|
||||
public function posts(): HasManyThrough
|
||||
{
|
||||
return $this->hasManyThrough(Post::class, Thread::class, 'forum_id', 'thread_id');
|
||||
}
|
||||
|
||||
public function latestThread(): HasOne
|
||||
{
|
||||
return $this->hasOne(Thread::class)->latestOfMany();
|
||||
}
|
||||
|
||||
public function latestPost(): HasOneThrough
|
||||
{
|
||||
return $this->hasOneThrough(Post::class, Thread::class, 'forum_id', 'thread_id')
|
||||
->latestOfMany();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@@ -56,4 +57,9 @@ class Thread extends Model
|
||||
{
|
||||
return $this->hasMany(Post::class);
|
||||
}
|
||||
|
||||
public function latestPost(): HasOne
|
||||
{
|
||||
return $this->hasOne(Post::class)->latestOfMany();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
'name',
|
||||
'name_canonical',
|
||||
'avatar_path',
|
||||
'location',
|
||||
'rank_id',
|
||||
'email',
|
||||
'password',
|
||||
|
||||
Reference in New Issue
Block a user