mirror of
https://github.com/henry4682/finance_app.git
synced 2026-07-16 00:10:00 +00:00
feat: 前端分離+功能api化
1. 新增資產管理CRUD、googleOAuth登入 2. 更改原記帳controller
This commit is contained in:
50
app/Http/Controllers/Api/CategoryRuleController.php
Normal file
50
app/Http/Controllers/Api/CategoryRuleController.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\CategoryRule;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class CategoryRuleController extends Controller
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
/**
|
||||
* 新增自動分類規則
|
||||
* POST /api/category-rules
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'keyword' => 'required|string|max:255',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
$rule = CategoryRule::create([
|
||||
...$validated,
|
||||
'user_id' => $request->user()->id,
|
||||
]);
|
||||
|
||||
// 🟢 核心優化:必須預先載入 category 關聯,否則前端拿到這筆規則物件時,會無法渲染它的分類標籤
|
||||
$rule->load('category');
|
||||
|
||||
return response()->json($rule, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刪除自動分類規則
|
||||
* DELETE /api/category-rules/{categoryRule}
|
||||
*/
|
||||
public function destroy(CategoryRule $categoryRule)
|
||||
{
|
||||
if ($categoryRule->user_id !== $request->user()->id) {
|
||||
return response()->json(['error' => '未經授權'], 403);
|
||||
}
|
||||
|
||||
$categoryRule->delete();
|
||||
|
||||
return response()->json(['message' => '自動分類規則刪除成功']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user