feat: menu & API Informasi Sekolah untuk aplikasi wali
- Tabel school_informations (judul, isi, kategori, tanggal kegiatan, publish) - Menu admin 'Informasi Sekolah' (CRUD + permission manage-school-info) - API wali: GET /parent/school-informations (hanya yang dipublikasikan) - 2 data contoh: Jadwal UTS & Class Meet - Test suite tetap hijau (39 test)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SchoolInformation;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class SchoolInformationController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$items = SchoolInformation::query()
|
||||
->with('creator:id,name')
|
||||
->orderByDesc('event_date')
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
return view('settings.school-info', compact('items'));
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $this->validateData($request);
|
||||
|
||||
SchoolInformation::create($validated + ['created_by' => $request->user()->id]);
|
||||
|
||||
return back()->with('success', 'Informasi "' . $validated['title'] . '" berhasil dipublikasikan.');
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): RedirectResponse
|
||||
{
|
||||
$info = SchoolInformation::findOrFail($id);
|
||||
$validated = $this->validateData($request);
|
||||
|
||||
$info->update($validated);
|
||||
|
||||
return back()->with('success', 'Informasi berhasil diperbarui.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, int $id): RedirectResponse
|
||||
{
|
||||
$info = SchoolInformation::findOrFail($id);
|
||||
$info->delete();
|
||||
|
||||
return back()->with('success', 'Informasi dihapus.');
|
||||
}
|
||||
|
||||
protected function validateData(Request $request): array
|
||||
{
|
||||
return $request->validate([
|
||||
'title' => ['required', 'string', 'max:200'],
|
||||
'content' => ['nullable', 'string'],
|
||||
'category' => ['required', 'string', 'max:50'],
|
||||
'event_date' => ['nullable', 'date'],
|
||||
'is_published' => ['nullable', 'boolean'],
|
||||
]) + ['is_published' => $request->boolean('is_published')];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SchoolInformation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ParentSchoolInformationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Daftar informasi sekolah yang dipublikasikan (untuk app wali murid).
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$items = SchoolInformation::query()
|
||||
->where('is_published', true)
|
||||
->orderByDesc('event_date')
|
||||
->orderByDesc('created_at')
|
||||
->limit(50)
|
||||
->get()
|
||||
->map(fn (SchoolInformation $i) => [
|
||||
'id' => $i->id,
|
||||
'title' => $i->title,
|
||||
'content' => $i->content,
|
||||
'category' => $i->category,
|
||||
'event_date' => $i->event_date?->toDateString(),
|
||||
'created_at' => $i->created_at?->toISOString(),
|
||||
]);
|
||||
|
||||
return response()->json(['informations' => $items]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SchoolInformation extends Model
|
||||
{
|
||||
protected $table = 'school_informations';
|
||||
|
||||
protected $fillable = [
|
||||
'title', 'content', 'category', 'event_date', 'is_published', 'published_at', 'created_by',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'event_date' => 'date',
|
||||
'is_published' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user