ProviderPaymentMethodController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\ProviderPaymentMethodRequest;
  4. use App\Http\Resources\ProviderPaymentMethodResource;
  5. use App\Services\ProviderPaymentMethodService;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  9. class ProviderPaymentMethodController extends Controller
  10. {
  11. public function __construct(
  12. private readonly ProviderPaymentMethodService $service
  13. ) {}
  14. public function index($id): JsonResponse
  15. {
  16. $paymentMethods = $this->service->getByProvider($id);
  17. return $this->successResponse(
  18. payload: ProviderPaymentMethodResource::collection($paymentMethods)
  19. );
  20. }
  21. public function show(int $id): ProviderPaymentMethodResource
  22. {
  23. $paymentMethod = $this->service->findById($id);
  24. abort_if(!$paymentMethod, 404);
  25. return new ProviderPaymentMethodResource($paymentMethod);
  26. }
  27. public function store(ProviderPaymentMethodRequest $request): JsonResponse
  28. {
  29. $paymentMethod = $this->service->create($request->validated());
  30. return $this->successResponse(
  31. payload: new ProviderPaymentMethodResource($paymentMethod)
  32. );
  33. }
  34. public function update(ProviderPaymentMethodRequest $request, int $id): JsonResponse
  35. {
  36. $paymentMethod = $this->service->findById($id);
  37. abort_if(!$paymentMethod, 404);
  38. $paymentMethod = $this->service->update($paymentMethod, $request->validated());
  39. return $this->successResponse(
  40. payload: new ProviderPaymentMethodResource($paymentMethod)
  41. );
  42. }
  43. public function destroy(int $id): JsonResponse
  44. {
  45. $paymentMethod = $this->service->findById($id);
  46. $this->service->delete($paymentMethod);
  47. return $this->successResponse(
  48. message: __("messages.deleted"),
  49. code: 204,
  50. );
  51. }
  52. }