feat(01-02): create MemberNoteController and routes
- MemberNoteController with index() and store() methods - StoreNoteRequest with Traditional Chinese validation messages - Routes registered at admin.members.notes.index/store - JSON responses for AJAX consumption in Phase 2 - DB::transaction wrapper with AuditLogger::log
This commit is contained in:
48
app/Http/Controllers/Admin/MemberNoteController.php
Normal file
48
app/Http/Controllers/Admin/MemberNoteController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StoreNoteRequest;
|
||||
use App\Models\Member;
|
||||
use App\Support\AuditLogger;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MemberNoteController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get all notes for a member
|
||||
*/
|
||||
public function index(Member $member)
|
||||
{
|
||||
$notes = $member->notes()->with('author')->get();
|
||||
|
||||
return response()->json(['notes' => $notes]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new note for a member
|
||||
*/
|
||||
public function store(StoreNoteRequest $request, Member $member)
|
||||
{
|
||||
$note = DB::transaction(function () use ($request, $member) {
|
||||
$note = $member->notes()->create([
|
||||
'content' => $request->content,
|
||||
'author_user_id' => $request->user()->id,
|
||||
]);
|
||||
|
||||
AuditLogger::log('note.created', $note, [
|
||||
'member_id' => $member->id,
|
||||
'member_name' => $member->full_name,
|
||||
'author' => $request->user()->name,
|
||||
]);
|
||||
|
||||
return $note;
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'note' => $note->load('author'),
|
||||
'message' => '備忘錄已新增',
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
41
app/Http/Requests/StoreNoteRequest.php
Normal file
41
app/Http/Requests/StoreNoteRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreNoteRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
// Authorization is handled by the admin middleware on the route group
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'content' => ['required', 'string', 'min:1', 'max:65535'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'content.required' => '備忘錄內容為必填欄位',
|
||||
'content.min' => '備忘錄內容不可為空白',
|
||||
'content.max' => '備忘錄內容不可超過 65535 字元',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user