Repository navigation
feat(notice): migrate notices table to app_settings key-value store #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,17 +2,18 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace App\Http\Controllers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\AppSetting; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\Blog; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\ForumPost; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\ForumVote; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\Notice; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\Resource; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\ResourceVote; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\Subject; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use App\Models\UserAppreciation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Illuminate\Http\Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Illuminate\Support\Facades\Auth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Illuminate\Support\Facades\Cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Illuminate\Support\Facades\Storage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Inertia\Inertia; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class HomeController extends Controller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -44,13 +45,31 @@ public function index(Request $request) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ->toArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $notice = Cache::rememberForever('active_notice_for_display', function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $raw = AppSetting::get('site_notice'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (! $raw) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $data = is_array($raw) ? $raw : json_decode($raw, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (empty($data['is_active'])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (! empty($data['image'])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $data['image'] = str($data['image'])->startsWith(['http://', 'https://']) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? $data['image'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : Storage::url($data['image']); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+48
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔎 Supported by static analysis
Use a new cache key for the migrated notice payload. The legacy cache stores an array, not a serialized 🐛 Suggested fix
Committable suggestion
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Inertia::render('Feed', [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'feed' => $feed, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'subjects' => $subjects, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'course' => $course, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'group' => $group, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'sort' => $sort, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'notice' => Notice::activeForDisplay(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'notice' => $notice, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| if (Schema::hasTable('notices')) { | ||
| $first = DB::table('notices')->first(); | ||
| if ($first) { | ||
| DB::table('app_settings')->updateOrInsert( | ||
| ['key' => 'site_notice'], | ||
| [ | ||
| 'value' => json_encode([ | ||
| 'title' => $first->title, | ||
| 'message' => $first->message, | ||
| 'image' => $first->image, | ||
| 'show_button' => (bool) $first->show_button, | ||
| 'button_title' => $first->button_title, | ||
| 'button_link' => $first->button_link, | ||
| 'is_active' => (bool) $first->is_active, | ||
| ]), | ||
| 'updated_at' => now(), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| Schema::dropIfExists('notices'); | ||
| } | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::create('notices', function (Blueprint $table) { | ||
| $table->id(); | ||
| $table->string('title')->nullable(); | ||
| $table->text('message')->nullable(); | ||
| $table->string('image')->nullable(); | ||
| $table->boolean('show_button')->default(false); | ||
| $table->string('button_title')->nullable(); | ||
| $table->string('button_link')->nullable(); | ||
| $table->boolean('is_active')->default(false); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Server accepts an active notice with no content.
titleandmessagewere required before. They are now nullable. The "add content before activating" rule exists only as a warning inNoticeEdit.vue. A direct POST, or a form submitted after clearing every field, can saveis_active = truewith no title, message, image, or video URL.HomeControllerthen returns that notice to the homepage as an active notice, so visitors may see an empty dialog.Add a server-side check. Run it before the
Storage::deleteandstorecalls, so a rejected request does not remove or upload files. Compute the image state that will result: keep the existing image unlessremove_imageis set or a new file is uploaded.