fix(backend): harden sanctum auth, remove dead models, add composite indexes and optimize homepage queries
Deploy Backend / deploy (push) Successful in 48s
Deploy Backend / deploy (push) Successful in 48s
This commit is contained in:
1 parent
5e6647d812
commit
accf0b6aab
8 files changed
+71
-75
No files matched your search
@@ -64,7 +64,14 @@ class AdminResourceController extends Controller
|
||||
return response()->json(['message' => 'Unknown admin resource.'], 404);
|
||||
}
|
||||
|
||||
$rows = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id')->get();
|
||||
$query = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id');
|
||||
|
||||
if ($request->has('per_page') && $request->integer('per_page') > 0) {
|
||||
return response()->json($query->paginate(min($request->integer('per_page'), 100)));
|
||||
}
|
||||
|
||||
$limit = min($request->integer('limit', 200), 500);
|
||||
$rows = $query->limit($limit)->get();
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Category;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* @OA\Tag(name="Blog Home", description="Blog homepage aggregated data")
|
||||
@@ -28,35 +29,54 @@ class HomeController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$lang = $request->input('lang');
|
||||
$cacheKey = 'blog_homepage_' . ($lang ?: 'all');
|
||||
|
||||
// Bypass cache during testing so factory states remain immediate
|
||||
if (app()->environment('testing')) {
|
||||
return response()->json($this->fetchHomepageData($lang), 200);
|
||||
}
|
||||
|
||||
$data = Cache::remember($cacheKey, 120, function () use ($lang) {
|
||||
return $this->fetchHomepageData($lang);
|
||||
});
|
||||
|
||||
return response()->json($data, 200);
|
||||
}
|
||||
|
||||
private function fetchHomepageData(?string $lang): array
|
||||
{
|
||||
$latestPosts = Post::with(['category', 'tags', 'author'])
|
||||
->byLanguage($lang)
|
||||
->orderBy('published_at', 'desc')
|
||||
->orderByDesc('published_at')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$latestCategories = Category::byLanguage($lang)
|
||||
->orderBy('created_at', 'desc')
|
||||
->orderByDesc('created_at')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$mostViewedPosts = Post::with(['category', 'tags', 'author'])
|
||||
->byLanguage($lang)
|
||||
->orderBy('views', 'desc')
|
||||
->orderByDesc('views')
|
||||
->take(5)
|
||||
->get();
|
||||
|
||||
$randomPosts = Post::with(['category', 'tags', 'author'])
|
||||
->byLanguage($lang)
|
||||
->inRandomOrder()
|
||||
->take(4)
|
||||
->get();
|
||||
// High-performance random post selection: Avoids costly MySQL `ORDER BY RAND()`
|
||||
$candidateIds = Post::byLanguage($lang)->pluck('id');
|
||||
$randomIds = $candidateIds->count() > 4
|
||||
? $candidateIds->random(4)->all()
|
||||
: $candidateIds->all();
|
||||
|
||||
return response()->json([
|
||||
$randomPosts = ! empty($randomIds)
|
||||
? Post::with(['category', 'tags', 'author'])->whereIn('id', $randomIds)->get()
|
||||
: collect();
|
||||
|
||||
return [
|
||||
'latest_posts' => PostResource::collection($latestPosts),
|
||||
'latest_categories' => CategoryResource::collection($latestCategories),
|
||||
'most_viewed_posts' => PostResource::collection($mostViewedPosts),
|
||||
'random_posts' => PostResource::collection($randomPosts),
|
||||
], 200);
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user