Skip to content

Commit

Permalink
imp: update service
Browse files Browse the repository at this point in the history
  • Loading branch information
adityadarma committed May 18, 2024
1 parent bc20157 commit 8d65681
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 34 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```
Expand All @@ -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);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Console/ServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AdityaDarma\LaravelServiceRepository\Console;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Facades\File;

class ServiceCommand extends GeneratorCommand
{
Expand Down Expand Up @@ -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';
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Console/ServiceRepositoryInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
10 changes: 10 additions & 0 deletions src/Services/AppService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Services;

use AdityaDarma\LaravelServiceRepository\Services\BaseService as CoreService;

class BaseService extends CoreService
{
// Custom
}
58 changes: 29 additions & 29 deletions src/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,41 +114,13 @@ public function getError(): mixed
return $this->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
Expand Down Expand Up @@ -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
*
Expand Down
13 changes: 13 additions & 0 deletions src/Stubs/service-app.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace {{ namespace }};

use App\Services\BaseService;

class {{ class }} extends BaseService
{
public function __construct(

){}

}

0 comments on commit 8d65681

Please sign in to comment.