Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Helpers/CacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public static function clearHomePage(?string $course = null): void

Cache::forget('home_page_subjects_hsc');
Cache::forget('home_page_subjects_ssc');
Cache::forget('home_page_featured_blogs');
Cache::forget('home_page_notice');
}
}
22 changes: 13 additions & 9 deletions app/Http/Controllers/SubjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ public function index($course)
->toArray();
});

$featuredBlogs = Blog::where('is_featured', true)
->where('is_published', true)
->with('user:id,name,username')
->withCount(['reactions', 'comments'])
->inRandomOrder()
->limit(3)
->get()
->toArray();
$featuredBlogs = Cache::remember('home_page_featured_blogs', now()->addDay(), function () {
return Blog::where('is_featured', true)
->where('is_published', true)
->with('user:id,name,username')
->withCount(['reactions', 'comments'])
->inRandomOrder()
->limit(3)
->get()
->toArray();
});

$notice = Notice::activeForDisplay()?->toArray();
$notice = Cache::rememberForever('home_page_notice', function () {
return Notice::activeForDisplay()?->toArray();
});

return Inertia::render('Home', [
'subjects' => $subjects,
Expand Down
5 changes: 5 additions & 0 deletions app/Observers/NoticeObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public function saved(Notice $notice): void
{
CacheHelper::clearHomePage();
}

public function deleted(Notice $notice): void
{
CacheHelper::clearHomePage();
}
}
39 changes: 39 additions & 0 deletions tests/Feature/PublicPagesTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
<?php

use App\Models\Blog;
use App\Models\Notice;
use App\Models\User;
use Illuminate\Support\Facades\Cache;

test('the homepage loads successfully', function () {
$response = $this->get('/');

$response->assertStatus(200);
});

test('featured blogs and notice are cached on homepage', function () {
$user = User::factory()->create();
$blog = Blog::factory()->create([
'user_id' => $user->id,
'is_featured' => true,
'is_published' => true,
]);
$notice = Notice::singleton();
$notice->update([
'title' => 'Important Notice',
'is_active' => true,
]);

expect(Cache::has('home_page_featured_blogs'))->toBeFalse()
->and(Cache::has('home_page_notice'))->toBeFalse();

$this->get('/')->assertStatus(200);

expect(Cache::has('home_page_featured_blogs'))->toBeTrue()
->and(Cache::has('home_page_notice'))->toBeTrue();

// Cache should be invalidated when notice is updated
$notice->update(['title' => 'Updated Notice']);
expect(Cache::has('home_page_notice'))->toBeFalse()
->and(Cache::has('home_page_featured_blogs'))->toBeFalse();

// Re-cache and verify blog change invalidates cache
$this->get('/')->assertStatus(200);
expect(Cache::has('home_page_featured_blogs'))->toBeTrue();

$blog->update(['title' => 'Updated Blog Title']);
expect(Cache::has('home_page_featured_blogs'))->toBeFalse();
});

test('the about us page loads successfully', function () {
$response = $this->get('/about-us');

Expand Down