Skip to content

Commit

Permalink
crud done
Browse files Browse the repository at this point in the history
  • Loading branch information
dipeshsukhia committed Jun 14, 2022
1 parent 5d5782f commit 8d9d077
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/LaravelApiDevelopServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class LaravelApiDevelopServiceProvider extends ServiceProvider
*/
public function boot()
{

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/resources/stubs/ApiController.stub' => app_path('Http/Controllers/Api/ApiController.php'),
__DIR__ . '/resources/stubs/ApiHandlerTrait.stub' => app_path('Exceptions/Traits/ApiHandlerTrait.php'),
__DIR__ . '/resources/stubs/ApiJson.stub' => app_path('Http/Resources/Macros/ApiJson.php'),
__DIR__ . '/resources/stubs/ApiResourceTrait.stub' => app_path('Http/Resources/Traits/ApiResourceTrait.php'),
__DIR__ . '/resources/stubs/message.stub' => resource_path('lang/en/message.php'),
__DIR__.'/../config/config.php' => config_path('laravel-api-develop.php'),
], 'LaravelApiDevelop');
// Registering package commands.
Expand All @@ -38,6 +38,10 @@ public function register()
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-api-develop');

if(file_exists(app_path('Http/Resources/Macros/ApiJson.php'))){
require app_path('Http/Resources/Macros/ApiJson.php');
}

// Register the main class to use with the facade
$this->app->singleton('laravel-api-develop', function () {
return new LaravelApiDevelop;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/stubs/ApiController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ApiController extends BaseController
{
return response()->apiJson([
'success' => false,
'message' => 'Method '.$method.' does not exist',
'message' => __('message.method_404', compact('method')),
], 404);
}
}
10 changes: 5 additions & 5 deletions src/resources/stubs/ApiResourceTrait.stub
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait ApiResourceTrait
public function with($request): array
{
if (is_null($this->message) && !$this->count()) {
$this->setMessage('There are no data available.');
$this->setMessage( __('message.no_data') );
}
return [
'success' => true,
Expand All @@ -44,11 +44,11 @@ trait ApiResourceTrait
/**
* Retrieve a value when key Exists.
*
* @param $key
* @param string $key
* @param mixed $default
* @return MissingValue|mixed
*/
protected function whenExists($key, $default = null)
protected function whenExists(string $key, $default = null)
{
if (array_key_exists($key,$this->getAttributes())) {
return value($this->{$key});
Expand All @@ -59,11 +59,11 @@ trait ApiResourceTrait
/**
* Retrieve a manipulated value when key Exists.
*
* @param $key
* @param string $key
* @param $value
* @return MissingValue|mixed
*/
protected function whenExistsThan($key, $value)
protected function whenExistsThan(string $key, $value)
{
if (array_key_exists($key,$this->getAttributes())) {
return value($value);
Expand Down
6 changes: 3 additions & 3 deletions src/resources/stubs/ModelNameController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class {{modelName}}Controller extends ApiController
{
$requestData = $request->all();
${{modelNameCamel}} = {{modelName}}::create($requestData);
return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage('Created!');
return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage( __('message.create', ['model' => '{{modelName}}' ]) );
}

/**
Expand All @@ -60,7 +60,7 @@ class {{modelName}}Controller extends ApiController
{
$requestData = $request->all();
${{modelNameCamel}}->update($requestData);
return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage('Updated!');
return (new {{modelName}}Resource(${{modelNameCamel}}))->setMessage( __('message.update', ['model' => '{{modelName}}' ]) );
}

/**
Expand All @@ -73,7 +73,7 @@ class {{modelName}}Controller extends ApiController
{
${{modelNameCamel}}->delete();
return response()->apiJson([
'message' => 'Deleted!'
'message' => __('message.delete', ['model' => '{{modelName}}' ])
]);
}
}
18 changes: 18 additions & 0 deletions src/resources/stubs/message.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/*
|--------------------------------------------------------------------------
| Api Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during api for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/
return [
'create' => ':model successfully Created!!',
'update' => ':model successfully Updated!!',
'delete' => ':model successfully Deleted!!',
'no_data' => 'There are no data available!!',
'method_404' => 'Method :method does not exist!!',
];

0 comments on commit 8d9d077

Please sign in to comment.