145 lines
3.9 KiB
PHP
145 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Payment;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\PaymentRequest;
|
|
use App\Models\Payment;
|
|
use App\Repositories\Payment\PaymentReppositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class PaymentController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly PaymentReppositoryInterface $paymentRepository
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$payments = $this->paymentRepository->filter($request->all());
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $payments
|
|
]);
|
|
}
|
|
|
|
public function forSelect(): JsonResponse
|
|
{
|
|
$payments = $this->paymentRepository->filter(['status' => 1]);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $payments->map(fn($p) => [
|
|
'id' => $p->id,
|
|
'name' => $p->name,
|
|
'type' => (int) $p->type,
|
|
'status' => (int) $p->status,
|
|
])->values(),
|
|
]);
|
|
}
|
|
|
|
public function store(PaymentRequest $request): JsonResponse
|
|
{
|
|
$request->validated();
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$payment = $this->paymentRepository->create($request->all());
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Payment method created successfully',
|
|
'data' => $payment
|
|
], 201);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$payment = $this->paymentRepository->find($id);
|
|
|
|
if (!$payment) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find payment method by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => $payment
|
|
]);
|
|
}
|
|
|
|
public function update(int $id, PaymentRequest $request): JsonResponse
|
|
{
|
|
$payment = $this->paymentRepository->find($id);
|
|
|
|
if (!$payment) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find payment method by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
$request->validated();
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->paymentRepository->update($id, $request->all());
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Payment method updated successfully',
|
|
'data' => $this->paymentRepository->find($id)
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$payment = $this->paymentRepository->find($id);
|
|
|
|
if (!$payment) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find payment method by ID #" . $id
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->paymentRepository->delete($id);
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Payment method deleted successfully'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
}
|