From 8d65681e461db94e26e0464a4d9c2e79b2869321 Mon Sep 17 00:00:00 2001 From: Aditya Darma Date: Sat, 18 May 2024 23:54:08 +0800 Subject: [PATCH] imp: update service --- README.md | 11 +++- src/Console/ServiceCommand.php | 7 ++- .../ServiceRepositoryInstallCommand.php | 2 +- src/Services/AppService.php | 10 ++++ src/Services/BaseService.php | 58 +++++++++---------- src/Stubs/service-app.stub | 13 +++++ 6 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 src/Services/AppService.php create mode 100644 src/Stubs/service-app.stub diff --git a/README.md b/README.md index a02cf2a..4192dde 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,6 @@ public function json(Request $request) public function withResource(Request $request) { - $this->nameService->functionName()->resource(ClassResource::class)->toJson(); - // OR $this->nameService->functionName()->toJsonFromResource(ClassResource::class); } ``` @@ -71,9 +69,16 @@ public function nameMethod() throw new CustomException('Error exception'); } .......... + // Call toJsonFromResource at controller return $this->setData($data) ->setMessage('Message data') - ->setCode(200) + ->setCode(200); + // OR + // Call toJson at controller + return $this->setData($data) + ->setResource(ClassResource::class) + ->setMessage('Message data') + ->setCode(200); } catch (Exception $e) { return $this->exceptionResponse($e); } diff --git a/src/Console/ServiceCommand.php b/src/Console/ServiceCommand.php index 7a4a750..6667295 100644 --- a/src/Console/ServiceCommand.php +++ b/src/Console/ServiceCommand.php @@ -3,6 +3,7 @@ namespace AdityaDarma\LaravelServiceRepository\Console; use Illuminate\Console\GeneratorCommand; +use Illuminate\Support\Facades\File; class ServiceCommand extends GeneratorCommand { @@ -37,7 +38,11 @@ class ServiceCommand extends GeneratorCommand */ protected function getStub(): string { - return __DIR__.'/../Stubs/service.stub'; + if (File::exists(app_path("Services/BaseService.php"))) { + return __DIR__.'/../Stubs/service-app.stub'; + } else { + return __DIR__.'/../Stubs/service.stub'; + } } /** diff --git a/src/Console/ServiceRepositoryInstallCommand.php b/src/Console/ServiceRepositoryInstallCommand.php index 82d6682..70d97ce 100644 --- a/src/Console/ServiceRepositoryInstallCommand.php +++ b/src/Console/ServiceRepositoryInstallCommand.php @@ -53,6 +53,6 @@ private function publishService(): void File::makeDirectory(app_path("Services")); } - File::copy(__DIR__ . "/../Services/BaseService.php", app_path("Services/BaseService.php")); + File::copy(__DIR__ . "/../Services/AppService.php", app_path("Services/BaseService.php")); } } diff --git a/src/Services/AppService.php b/src/Services/AppService.php new file mode 100644 index 0000000..b0e1942 --- /dev/null +++ b/src/Services/AppService.php @@ -0,0 +1,10 @@ +error; } - /** - * Convert to json data - * - * @return JsonResponse - */ - public function toJson(): JsonResponse - { - return response()->json( - array_filter([ - 'message' => $this->message, - 'errors' => $this->error, - 'meta' => $this->meta, - 'data' => $this->data, - 'link' => $this->link, - ]), $this->code); - } - - /** - * Convert resource to json response - * - * @param string $resource - * @return JsonResponse - */ - public function toJsonFromResource(string $resource): JsonResponse - { - return $this->resource($resource)->toJson(); - } - /** * Convert to json data with resource * * @param string $resource * @return static */ - public function resource(string $resource): static + public function setResource(string $resource): static { if ($this->data) { // Simple Paginate @@ -194,6 +166,34 @@ public function resource(string $resource): static return $this; } + /** + * Convert to json data + * + * @return JsonResponse + */ + public function toJson(): JsonResponse + { + return response()->json( + array_filter([ + 'message' => $this->message, + 'errors' => $this->error, + 'meta' => $this->meta, + 'data' => $this->data, + 'link' => $this->link, + ]), $this->code); + } + + /** + * Convert resource to json response + * + * @param string $resource + * @return JsonResponse + */ + public function toJsonFromResource(string $resource): JsonResponse + { + return $this->setResource($resource)->toJson(); + } + /** * Hadle exception error * diff --git a/src/Stubs/service-app.stub b/src/Stubs/service-app.stub new file mode 100644 index 0000000..6b9cd6e --- /dev/null +++ b/src/Stubs/service-app.stub @@ -0,0 +1,13 @@ +