Skip to content

Commit 1150987

Browse files
authored
feat: add SSC support (#72)
* refactor: centralize homepage cache handling * feat: add migration and backend support for SSC * feat: add SSC course support to subject UI * fix: update slug generation to support duplicate subjects across courses * fix: sort subjects by course and order * fix: update UI text for SSC and HSC support * fix: update test for new database scheme
2 parents fa4efa1 + 369cb54 commit 1150987

21 files changed

Lines changed: 226 additions & 34 deletions

‎app/Helpers/CacheHelper.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
class CacheHelper
8+
{
9+
public static function clearHomePage(?string $course = null): void
10+
{
11+
if ($course) {
12+
Cache::forget("home_page_data_{$course}");
13+
return;
14+
}
15+
16+
Cache::forget('home_page_data_hsc');
17+
Cache::forget('home_page_data_ssc');
18+
}
19+
}

‎app/Http/Controllers/Admin/SubjectController.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ class SubjectController extends Controller
1313
{
1414
function index()
1515
{
16-
$subjects = Subject::orderBy('sort_order', 'asc')->withCount('nodes')->get();
16+
$subjects = Subject::orderBy('course', 'desc')
17+
->orderBy('sort_order', 'asc')
18+
->withCount('nodes')
19+
->get();
1720

21+
1822
return Inertia::render('admin/Index', [
1923
'subjects' => $subjects,
2024
]);

‎app/Http/Controllers/SubjectController.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
class SubjectController extends Controller
1515
{
16-
public function index()
16+
public function index($course)
1717
{
18-
$homeData = Cache::rememberForever("home_page_data", function () {
18+
$homeData = Cache::rememberForever("home_page_data_{$course}", function () use ($course) {
1919
$subjects = Subject::orderBy('sort_order', 'asc')
20+
->where('course', $course)
2021
->withCount([
2122
'nodes' => function ($query) {
2223
$query->whereNull('parent_id');

‎app/Http/Requests/Subject/StoreSubjectRequest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function authorize(): bool
1919
protected function prepareForValidation(): void
2020
{
2121
$this->merge([
22-
'slug' => Str::slug($this->name),
22+
'slug' => Str::slug($this->course . '-' . $this->name),
2323
]);
2424
}
2525

@@ -36,6 +36,7 @@ public function rules(): array
3636
'tailwind_format' => ['required', 'string', 'max:100'],
3737
'icon' => ['required', 'string', 'max:50'],
3838
'sort_order' => ['required', 'integer'],
39+
'course' => ['required', 'string', 'in:ssc,hsc'],
3940
];
4041
}
4142
}

‎app/Http/Requests/Subject/UpdateSubjectRequest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function prepareForValidation(): void
2020
{
2121
if ($this->exists('name')) {
2222
$this->merge([
23-
'slug' => Str::slug($this->input('name')),
23+
'slug' => Str::slug($this->course . '-' . $this->name),
2424
]);
2525
}
2626
}
@@ -39,6 +39,7 @@ public function rules(): array
3939
'icon' => ['sometimes', 'string', 'max:50'],
4040
'sort_order' => ['sometimes', 'integer'],
4141
'slug' => ['sometimes', 'string'],
42+
'course' => ['sometimes', 'string', 'in:ssc,hsc'],
4243
];
4344
}
4445
}

‎app/Observers/BlogObserver.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Observers;
44

5+
use App\Helpers\CacheHelper;
56
use App\Models\Blog;
67
use Illuminate\Support\Facades\Cache;
78

@@ -14,14 +15,14 @@ public function saved(Blog $blog): void
1415
$blog->getOriginal('is_featured') ||
1516
$blog->wasChanged('is_published')
1617
) {
17-
Cache::forget('home_page_data');
18+
CacheHelper::clearHomePage();
1819
}
1920
}
2021

2122
public function deleted(Blog $blog): void
2223
{
2324
if ($blog->is_featured) {
24-
Cache::forget('home_page_data');
25+
CacheHelper::clearHomePage();
2526
}
2627
}
2728
}

‎app/Observers/NodeObserver.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Observers;
44

5+
use App\Helpers\CacheHelper;
56
use App\Models\Node;
67
use Illuminate\Support\Facades\Cache;
78

@@ -10,14 +11,14 @@ class NodeObserver
1011
public function created(Node $node): void
1112
{
1213
if ($node->parent_id === null) {
13-
Cache::forget('home_page_data');
14+
CacheHelper::clearHomePage();
1415
}
1516
}
1617

1718
public function deleted(Node $node): void
1819
{
1920
if ($node->parent_id === null) {
20-
Cache::forget('home_page_data');
21+
CacheHelper::clearHomePage();
2122
}
2223
}
2324
}

‎app/Observers/NoticeObserver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace App\Observers;
44

5+
use App\Helpers\CacheHelper;
56
use App\Models\Notice;
67
use Illuminate\Support\Facades\Cache;
78

89
class NoticeObserver
910
{
1011
public function saved(Notice $notice): void
1112
{
12-
Cache::forget('home_page_data');
13+
CacheHelper::clearHomePage();
1314
}
1415
}

‎app/Observers/SubjectObserver.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace App\Observers;
44

5+
use App\Helpers\CacheHelper;
56
use App\Models\Subject;
67
use Illuminate\Support\Facades\Cache;
78

89
class SubjectObserver
910
{
1011
public function saved(Subject $subject): void
1112
{
12-
Cache::forget('home_page_data');
13+
CacheHelper::clearHomePage();
1314
}
1415

1516
public function deleted(Subject $subject): void
1617
{
17-
Cache::forget('home_page_data');
18+
CacheHelper::clearHomePage($subject->course);
1819
}
1920
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('subjects', function (Blueprint $table) {
15+
//
16+
$table->enum('course', ['ssc', 'hsc'])
17+
->default('hsc')
18+
->after('id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
Schema::table('subjects', function (Blueprint $table) {
28+
//
29+
$table->dropColumn('course');
30+
});
31+
}
32+
};

0 commit comments

Comments
 (0)