fix(backend): harden sanctum auth, remove dead models, add composite indexes and optimize homepage queries
Deploy Backend / deploy (push) Successful in 48s

This commit is contained in:
Parsa Aghayi committed 2026-11-02 13:00:24 +00:00
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);
];
}
}
-21
View File
@@ -1,21 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
protected $fillable = [
'title',
'category',
'images',
];
protected $casts = [
'images' => 'array', // تبدیل تصاویر به آرایه
];
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
use HasFactory;
}
-1
View File
@@ -23,7 +23,6 @@ class User extends Authenticatable
'name',
'email',
'password',
'is_admin',
];
/**
+1 -1
View File
@@ -46,7 +46,7 @@ return [
|
*/
'expiration' => null,
'expiration' => env('SANCTUM_EXPIRATION', 20160), // 14 days in minutes
/*
|--------------------------------------------------------------------------
-30
View File
@@ -1,30 +0,0 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Project;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
*/
class ProjectFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
protected $model = Project::class;
public function definition(): array
{
return [
'title' => $this->faker->sentence,
'category' => $this->faker->randomElement(['Web Design', 'App Design', 'Graphic Design', 'Front End', 'Back End']),
'images' => json_encode([
['src' => $this->faker->imageUrl(), 'position' => 'bottom-[-5px] left-[-5px]', 'zIndex' => 20],
['src' => $this->faker->imageUrl(), 'position' => 'top-[-5px] right-[-5px]', 'zIndex' => 10],
]),
];
}
}
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->index(['published_at', 'views'], 'posts_published_at_views_idx');
$table->index(['category_id', 'published_at'], 'posts_category_id_published_at_idx');
$table->index(['author_id', 'published_at'], 'posts_author_id_published_at_idx');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->dropIndex('posts_published_at_views_idx');
$table->dropIndex('posts_category_id_published_at_idx');
$table->dropIndex('posts_author_id_published_at_idx');
});
}
};