From f85e07091355b6009791ad1ed2e2a9014a33120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B6=8C=ED=98=81=EC=84=B1?= Date: Thu, 22 Jan 2026 20:49:29 +0900 Subject: [PATCH] =?UTF-8?q?feat(API):=20=EC=B9=B4=EB=93=9C=20=EA=B1=B0?= =?UTF-8?q?=EB=9E=98=20=EB=93=B1=EB=A1=9D/=EC=88=98=EC=A0=95/=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CardTransactionService: store, update, destroy 메서드 추가 - CardTransactionController: store, update, destroy 메서드 추가 - routes/api.php: POST, PUT/{id}, DELETE/{id} 라우트 추가 Co-Authored-By: Claude Opus 4.5 --- .../Api/V1/CardTransactionController.php | 47 +++++++++++ app/Services/CardTransactionService.php | 83 +++++++++++++++++++ routes/api.php | 5 +- 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/V1/CardTransactionController.php b/app/Http/Controllers/Api/V1/CardTransactionController.php index 13c9b37..33f5a5f 100644 --- a/app/Http/Controllers/Api/V1/CardTransactionController.php +++ b/app/Http/Controllers/Api/V1/CardTransactionController.php @@ -89,4 +89,51 @@ public function show(int $id): JsonResponse return $transaction; }, __('message.fetched')); } + + /** + * 카드 거래 등록 + */ + public function store(Request $request): JsonResponse + { + return ApiResponse::handle(function () use ($request) { + $validated = $request->validate([ + 'card_id' => 'nullable|integer|exists:cards,id', + 'used_at' => 'required|date', + 'merchant_name' => 'required|string|max:100', + 'amount' => 'required|numeric|min:0', + 'description' => 'nullable|string|max:500', + 'account_code' => 'nullable|string|max:20', + ]); + + return $this->service->store($validated); + }, __('message.created')); + } + + /** + * 카드 거래 수정 + */ + public function update(Request $request, int $id): JsonResponse + { + return ApiResponse::handle(function () use ($request, $id) { + $validated = $request->validate([ + 'used_at' => 'nullable|date', + 'merchant_name' => 'nullable|string|max:100', + 'amount' => 'nullable|numeric|min:0', + 'description' => 'nullable|string|max:500', + 'account_code' => 'nullable|string|max:20', + ]); + + return $this->service->update($id, $validated); + }, __('message.updated')); + } + + /** + * 카드 거래 삭제 + */ + public function destroy(int $id): JsonResponse + { + return ApiResponse::handle(function () use ($id) { + return $this->service->destroy($id); + }, __('message.deleted')); + } } diff --git a/app/Services/CardTransactionService.php b/app/Services/CardTransactionService.php index f1c64bd..633681d 100644 --- a/app/Services/CardTransactionService.php +++ b/app/Services/CardTransactionService.php @@ -180,4 +180,87 @@ public function show(int $id): ?Withdrawal ->with(['card', 'card.assignedUser', 'creator']) ->find($id); } + + /** + * 카드 거래 등록 + */ + public function store(array $data): Withdrawal + { + $tenantId = $this->tenantId(); + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($data, $tenantId, $userId) { + $withdrawal = new Withdrawal; + $withdrawal->tenant_id = $tenantId; + $withdrawal->card_id = $data['card_id'] ?? null; + $withdrawal->used_at = $data['used_at'] ?? null; + $withdrawal->withdrawal_date = isset($data['used_at']) ? date('Y-m-d', strtotime($data['used_at'])) : now()->toDateString(); + $withdrawal->merchant_name = $data['merchant_name'] ?? null; + $withdrawal->amount = $data['amount']; + $withdrawal->payment_method = 'card'; + $withdrawal->account_code = $data['account_code'] ?? null; + $withdrawal->description = $data['description'] ?? null; + $withdrawal->created_by = $userId; + $withdrawal->updated_by = $userId; + $withdrawal->save(); + + return $withdrawal->load(['card', 'card.assignedUser', 'creator']); + }); + } + + /** + * 카드 거래 수정 + */ + public function update(int $id, array $data): Withdrawal + { + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($id, $data, $userId) { + $withdrawal = Withdrawal::query() + ->where('payment_method', 'card') + ->findOrFail($id); + + if (isset($data['used_at'])) { + $withdrawal->used_at = $data['used_at']; + $withdrawal->withdrawal_date = date('Y-m-d', strtotime($data['used_at'])); + } + if (array_key_exists('merchant_name', $data)) { + $withdrawal->merchant_name = $data['merchant_name']; + } + if (isset($data['amount'])) { + $withdrawal->amount = $data['amount']; + } + if (array_key_exists('account_code', $data)) { + $withdrawal->account_code = $data['account_code']; + } + if (array_key_exists('description', $data)) { + $withdrawal->description = $data['description']; + } + + $withdrawal->updated_by = $userId; + $withdrawal->save(); + + return $withdrawal->fresh(['card', 'card.assignedUser', 'creator']); + }); + } + + /** + * 카드 거래 삭제 + */ + public function destroy(int $id): bool + { + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($id, $userId) { + $withdrawal = Withdrawal::query() + ->where('payment_method', 'card') + ->findOrFail($id); + + $withdrawal->deleted_by = $userId; + $withdrawal->save(); + $withdrawal->delete(); + + return true; + }); + } } diff --git a/routes/api.php b/routes/api.php index f2c3eff..28a3ab5 100644 --- a/routes/api.php +++ b/routes/api.php @@ -590,12 +590,15 @@ Route::get('/{clientId}', [VendorLedgerController::class, 'show'])->whereNumber('clientId')->name('v1.vendor-ledger.show'); }); - // Card Transaction API (카드 거래 조회) + // Card Transaction API (카드 거래) Route::prefix('card-transactions')->group(function () { Route::get('', [CardTransactionController::class, 'index'])->name('v1.card-transactions.index'); Route::get('/summary', [CardTransactionController::class, 'summary'])->name('v1.card-transactions.summary'); + Route::post('', [CardTransactionController::class, 'store'])->name('v1.card-transactions.store'); Route::put('/bulk-update-account', [CardTransactionController::class, 'bulkUpdateAccountCode'])->name('v1.card-transactions.bulk-update-account'); Route::get('/{id}', [CardTransactionController::class, 'show'])->whereNumber('id')->name('v1.card-transactions.show'); + Route::put('/{id}', [CardTransactionController::class, 'update'])->whereNumber('id')->name('v1.card-transactions.update'); + Route::delete('/{id}', [CardTransactionController::class, 'destroy'])->whereNumber('id')->name('v1.card-transactions.destroy'); }); // Bank Transaction API (은행 거래 조회)