ClientController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\ClientRequest;
  4. use App\Http\Resources\ClientResource;
  5. use App\Services\ClientService;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Http\JsonResponse;
  8. class ClientController extends Controller
  9. {
  10. // protected ClientService $clientService;
  11. // public function __construct(ClientService $clientService)
  12. // {
  13. // $this->clientService = $clientService;
  14. // }
  15. // public function index(): AnonymousResourceCollection
  16. // {
  17. // $clients = $this->clientService->getAll();
  18. // return ClientResource::collection($clients);
  19. // }
  20. // public function store(ClientRequest $request): ClientResource
  21. // {
  22. // $client = $this->clientService->create($request->validated());
  23. // return new ClientResource($client->load('user'));
  24. // }
  25. // public function show(string $id): ClientResource
  26. // {
  27. // $client = $this->clientService->findById($id);
  28. // return new ClientResource($client);
  29. // }
  30. // public function update(ClientRequest $request, string $id): JsonResponse
  31. // {
  32. // $this->clientService->update($request->validated(), $id);
  33. // return response()->json(['message' => 'Client updated successfully']);
  34. // }
  35. // public function destroy(string $id): JsonResponse
  36. // {
  37. // $this->clientService->delete($id);
  38. // return response()->json(['message' => 'Client deleted successfully']);
  39. // }
  40. public function __construct(protected ClientService $service) {}
  41. public function index(): JsonResponse
  42. {
  43. $items = $this->service->getAll();
  44. return $this->successResponse(
  45. payload: ClientResource::collection($items),
  46. );
  47. }
  48. public function store(ClientRequest $request): JsonResponse
  49. {
  50. $item = $this->service->create($request->validated());
  51. return $this->successResponse(
  52. payload: new ClientResource($item),
  53. message: __("messages.created"),
  54. code: 201,
  55. );
  56. }
  57. public function show(int $id): JsonResponse
  58. {
  59. $item = $this->service->findById($id);
  60. return $this->successResponse(payload: new ClientResource($item));
  61. }
  62. public function update(ClientRequest $request, int $id): JsonResponse
  63. {
  64. $item = $this->service->update($request->validated(), $id);
  65. return $this->successResponse(
  66. payload: new ClientResource($item),
  67. message: __("messages.updated"),
  68. );
  69. }
  70. public function destroy(int $id): JsonResponse
  71. {
  72. $this->service->delete($id);
  73. return $this->successResponse(
  74. message: __("messages.deleted"),
  75. code: 204,
  76. );
  77. }
  78. }