Files

86 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Bank;
use App\Http\Controllers\Controller;
use App\Models\Bank;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BankController extends Controller
{
public function index(): JsonResponse
{
$banks = Bank::withTrashed()->orderBy('sort_order')->orderBy('id')->get();
return response()->json(['error' => false, 'data' => $banks]);
}
public function store(Request $request): JsonResponse
{
$data = $request->validate([
'bank_code' => 'required|string|max:20',
'bank_name' => 'required|string|max:100',
'account_no' => 'required|string|max:50',
'account_name' => 'required|string|max:200',
'is_active' => 'boolean',
'sort_order' => 'integer|min:0',
]);
$bank = Bank::create($data);
return response()->json([
'error' => false,
'message' => 'Thêm ngân hàng thành công',
'data' => $bank,
], 201);
}
public function update(Request $request, int $id): JsonResponse
{
$bank = Bank::withTrashed()->findOrFail($id);
$data = $request->validate([
'bank_code' => 'sometimes|string|max:20',
'bank_name' => 'sometimes|string|max:100',
'account_no' => 'sometimes|string|max:50',
'account_name' => 'sometimes|string|max:200',
'is_active' => 'boolean',
'sort_order' => 'integer|min:0',
]);
$bank->update($data);
return response()->json([
'error' => false,
'message' => 'Cập nhật ngân hàng thành công',
'data' => $bank->fresh(),
]);
}
public function destroy(int $id): JsonResponse
{
$bank = Bank::findOrFail($id);
DB::transaction(fn () => $bank->delete());
return response()->json([
'error' => false,
'message' => 'Đã xoá ngân hàng',
]);
}
public function restore(int $id): JsonResponse
{
$bank = Bank::onlyTrashed()->findOrFail($id);
$bank->restore();
return response()->json([
'error' => false,
'message' => 'Đã khôi phục ngân hàng',
'data' => $bank,
]);
}
}