Skip to content

Commit

Permalink
add CreateHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rupadana committed Oct 3, 2023
1 parent d81eac8 commit 091096b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ and then you will got this routes:
- [GET] '/api/blogs' - Return LengthAwarePaginator
- [GET] '/api/blogs/1' - Return single resource
- [PUT] '/api/blogs/1' - Update resource
- [POST] '/api/blogs' - Create resource
- [DELETE] '/api/blogs/1' - Delete resource


On CreateHandler, you need to be create your custom request validation.

Im using `"spatie/laravel-query-builder": "^5.3"` to handle query and filtering. u can see `"spatie/laravel-query-builder": "^5.3"` [https://spatie.be/docs/laravel-query-builder/v5/introduction](documentation)


Expand All @@ -53,6 +56,19 @@ class User extends Model {
}
```

## Create a Handler

To create a handler you can use this command. By default, i'm using CreateHandler

```bash
php artisan make:filament-api-handler BlogResource
```

or

```bash
php artisan make:filament-api-handler Blog
```

## Transform API Response

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeApiHandlerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handle(): int
$handler = (string) str(
$this->argument('handler') ?? text(
label: 'What is the Handler name?',
placeholder: 'UpdateHandler',
placeholder: 'CreateHandler',
required: true
)
)
Expand Down
12 changes: 10 additions & 2 deletions src/Commands/MakeApiServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ public function handle(): int
$resourceNamespace = $modelNamespace;
$namespace .= $resourceNamespace !== '' ? "\\{$resourceNamespace}" : '';

$createHandlerClass = 'CreateHandler';
$updateHandlerClass = 'UpdateHandler';
$detailHandlerClass = 'DetailHandler';
$paginationHandlerClass = 'PaginationClass';
$deleteHandlerClass = 'DeleteHandlerClass';
$paginationHandlerClass = 'PaginationHandler';
$deleteHandlerClass = 'DeleteHandler';

$baseResourcePath =
(string) str($resource)
Expand All @@ -97,6 +98,7 @@ public function handle(): int
$handlersNamespace = "{$namespace}\\{$resourceClass}\\Api\\Handlers";

$resourceApiDirectory = "{$baseResourcePath}/Api/$apiServiceClass.php";
$createHandlerDirectory = "{$baseResourcePath}/Api/Handlers/$createHandlerClass.php";
$updateHandlerDirectory = "{$baseResourcePath}/Api/Handlers/$updateHandlerClass.php";
$detailHandlerDirectory = "{$baseResourcePath}/Api/Handlers/$detailHandlerClass.php";
$paginationHandlerDirectory = "{$baseResourcePath}/Api/Handlers/$paginationHandlerClass.php";
Expand All @@ -122,6 +124,12 @@ public function handle(): int
'handlersNamespace' => $handlersNamespace,
]);

$this->copyStubToApp('CreateHandler', $createHandlerDirectory, [
'resource' => "{$namespace}\\{$resourceClass}",
'resourceClass' => $resourceClass,
'handlersNamespace' => $handlersNamespace,
]);

$this->copyStubToApp('UpdateHandler', $updateHandlerDirectory, [
'resource' => "{$namespace}\\{$resourceClass}",
'resourceClass' => $resourceClass,
Expand Down
31 changes: 31 additions & 0 deletions stubs/CreateHandler.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace {{ handlersNamespace }};

use Illuminate\Http\Request;
use Rupadana\ApiService\Http\Handlers;
use {{ resource }};

class CreateHandler extends Handlers {
public static string | null $uri = '/';
public static string | null $resource = {{ resourceClass }}::class;

public static function getMethod()
{
return Handlers::POST;
}

public static function getModel() {
return static::$resource::getModel();
}

public function handler(Request $request)
{
$model = new (static::getModel());

$model->fill($request->all());

$model->save();

return static::sendSuccessResponse($model, "Successfully Create Resource");
}
}
10 changes: 4 additions & 6 deletions stubs/CustomHandler.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Rupadana\ApiService\Http\Handlers;
use {{ resource }};

class {{ handlerClass }} extends Handlers {
public static string | null $uri = '/{id}';
public static string | null $uri = '/';
public static string | null $resource = {{ resourceClass }}::class;

public static function getMethod()
Expand All @@ -18,16 +18,14 @@ class {{ handlerClass }} extends Handlers {
return static::$resource::getModel();
}

public function handler(Request $request, $id)
public function handler(Request $request)
{
$model = static::getModel()::find($id);

if (!$model) return static::sendNotFoundResponse();
$model = new (static::getModel());

$model->fill($request->all());

$model->save();

return static::sendSuccessResponse($model, "Successfully Update Resource");
return static::sendSuccessResponse($model, "Successfully Create Resource");
}
}
1 change: 1 addition & 0 deletions stubs/ResourceApiService.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class {{ apiServiceClass }} extends ApiService

public static function allRoutes(Router $router)
{
Handlers\CreateHandler::route($router);
Handlers\UpdateHandler::route($router);
Handlers\DeleteHandler::route($router);
Handlers\PaginationHandler::route($router);
Expand Down

0 comments on commit 091096b

Please sign in to comment.