From bb55beaa647fae0c0743b1abf9329237edb6a412 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 8 Dec 2016 17:09:45 +0200 Subject: [PATCH] Working on CRUD --- app/Console/Commands/MakeModule.php | 1 + app/Console/Commands/MakeRepository.php | 70 +++++++++++++++++++ app/Console/Commands/stubs/controller.stub | 24 +++++++ app/Console/Commands/stubs/json.stub | 3 +- app/Console/Commands/stubs/repository.stub | 78 ++++++++++++++++++++++ app/Console/Commands/stubs/routes.stub | 5 ++ app/Console/Kernel.php | 1 + app/Http/Controllers/ClientController.php | 2 +- app/Libraries/Utils.php | 6 ++ config/modules.php | 2 +- 10 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/MakeRepository.php create mode 100644 app/Console/Commands/stubs/repository.stub diff --git a/app/Console/Commands/MakeModule.php b/app/Console/Commands/MakeModule.php index a2a513fa19a3..6bd91d6b492c 100644 --- a/app/Console/Commands/MakeModule.php +++ b/app/Console/Commands/MakeModule.php @@ -49,6 +49,7 @@ class MakeModule extends Command Artisan::call('module:make-model', ['model' => $name, 'module' => $name]); Artisan::call('ninja:make-datatable', ['name' => $name, 'module' => $name]); + Artisan::call('ninja:make-repository', ['name' => $name, 'module' => $name]); } protected function getArguments() diff --git a/app/Console/Commands/MakeRepository.php b/app/Console/Commands/MakeRepository.php new file mode 100644 index 000000000000..d701a5cc00b6 --- /dev/null +++ b/app/Console/Commands/MakeRepository.php @@ -0,0 +1,70 @@ +laravel['modules']->findOrFail($this->getModuleName()); + + return (new Stub('/repository.stub', [ + 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.repository'), + 'LOWER_NAME' => $module->getLowerName(), + 'CLASS' => $this->getClass(), + 'STUDLY_NAME' => Str::studly($module->getLowerName()), + ]))->render(); + } + + public function getDestinationFilePath() + { + $path = $this->laravel['modules']->getModulePath($this->getModuleName()); + $seederPath = $this->laravel['modules']->config('paths.generator.repository'); + + return $path . $seederPath . '/' . $this->getFileName() . '.php'; + } + + /** + * @return string + */ + protected function getFileName() + { + return studly_case($this->argument('name')) . 'Repository'; + } + +} diff --git a/app/Console/Commands/stubs/controller.stub b/app/Console/Commands/stubs/controller.stub index 1a1af33f144a..02f63e54a238 100755 --- a/app/Console/Commands/stubs/controller.stub +++ b/app/Console/Commands/stubs/controller.stub @@ -2,14 +2,27 @@ namespace $CLASS_NAMESPACE$; +use Auth; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Routing\Controller; use App\Http\Controllers\BaseController; +use App\Services\DatatableService; +use Modules\$STUDLY_NAME$\Repositories\$STUDLY_NAME$Repository; use Modules\$STUDLY_NAME$\Datatables\$STUDLY_NAME$Datatable; class $CLASS$ extends BaseController { + protected $$STUDLY_NAME$Repo; + //protected $entityType = '$LOWER_NAME$'; + + public function __construct($STUDLY_NAME$Repository $$LOWER_NAME$Repo) + { + //parent::__construct(); + + $this->$LOWER_NAME$Repo = $$LOWER_NAME$Repo; + } + /** * Display a listing of the resource. * @return Response @@ -23,6 +36,17 @@ class $CLASS$ extends BaseController ]); } + public function getDatatable(DatatableService $datatableService) + { + $search = request()->input('test'); + $userId = Auth::user()->filterId(); + + $datatable = new $STUDLY_NAME$Datatable(); + $query = $this->$LOWER_NAME$Repo->find($search, $userId); + + return $datatableService->createDatatable($datatable, $query); + } + /** * Show the form for creating a new resource. * @return Response diff --git a/app/Console/Commands/stubs/json.stub b/app/Console/Commands/stubs/json.stub index 6335f304eb32..4ac9fc409bf4 100755 --- a/app/Console/Commands/stubs/json.stub +++ b/app/Console/Commands/stubs/json.stub @@ -12,5 +12,6 @@ "files": [ "start.php" ], - "icon": "th-large" + "icon": "th-large", + "plural": "$LOWER_NAME$" } diff --git a/app/Console/Commands/stubs/repository.stub b/app/Console/Commands/stubs/repository.stub new file mode 100644 index 000000000000..6da3ebe01678 --- /dev/null +++ b/app/Console/Commands/stubs/repository.stub @@ -0,0 +1,78 @@ +where('$LOWER_NAME$.account_id', '=', \Auth::user()->account_id) + ->select( + '$LOWER_NAME$.public_id', + '$LOWER_NAME$.deleted_at', + '$LOWER_NAME$.is_deleted', + '$LOWER_NAME$.user_id' + ); + + $this->applyFilters($query, '$LOWER_NAME$'); + + /* + if ($filter) { + $query->where(function ($query) use ($filter) { + $query->where('clients.name', 'like', '%'.$filter.'%') + ->orWhere('contacts.first_name', 'like', '%'.$filter.'%') + ->orWhere('contacts.last_name', 'like', '%'.$filter.'%') + ->orWhere('contacts.email', 'like', '%'.$filter.'%'); + }); + } + + if ($userId) { + $query->where('clients.user_id', '=', $userId); + } + */ + + return $query; + } + + public function save($data, $client = null) + { + $publicId = isset($data['public_id']) ? $data['public_id'] : false; + + if ($client) { + // do nothing + } elseif (!$publicId || $publicId == '-1') { + $client = Client::createNew(); + } else { + $client = Client::scope($publicId)->with('contacts')->firstOrFail(); + } + + if ($client->is_deleted) { + return $client; + } + + $client->fill($data); + $client->save(); + + /* + if (!$publicId || $publicId == '-1') { + event(new ClientWasCreated($client)); + } else { + event(new ClientWasUpdated($client)); + } + */ + + return $client; + } + +} diff --git a/app/Console/Commands/stubs/routes.stub b/app/Console/Commands/stubs/routes.stub index 81e825184365..846061bc7294 100755 --- a/app/Console/Commands/stubs/routes.stub +++ b/app/Console/Commands/stubs/routes.stub @@ -4,3 +4,8 @@ Route::group(['middleware' => 'auth', 'prefix' => '$LOWER_NAME$', 'namespace' => { Route::get('/', '$STUDLY_NAME$Controller@index'); }); + +Route::group(['middleware' => 'auth', 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers'], function() +{ + Route::get('api/$LOWER_NAME$', '$STUDLY_NAME$Controller@getDatatable'); +}); diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 66f682de6cef..fd7e44246839 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -25,6 +25,7 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\TestOFX', 'App\Console\Commands\MakeModule', 'App\Console\Commands\MakeDatatable', + 'App\Console\Commands\MakeRepository', ]; /** diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index c77087184499..321692f7e3d4 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -121,7 +121,7 @@ class ClientController extends BaseController 'gatewayLink' => $token ? $token->gatewayLink() : false, 'gatewayName' => $token ? $token->gatewayName() : false, ]; - + return View::make('clients.show', $data); } diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index d86ea881e602..f76895163ad1 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -431,6 +431,12 @@ class Utils public static function pluralizeEntityType($type) { + if ( ! Utils::isNinjaProd()) { + if ($module = \Module::find($type)) { + return $module->get('plural', $type); + } + } + if ($type === ENTITY_EXPENSE_CATEGORY) { return 'expense_categories'; } else { diff --git a/config/modules.php b/config/modules.php index 516dcd047725..c442a7d45cf9 100644 --- a/config/modules.php +++ b/config/modules.php @@ -24,7 +24,7 @@ return [ 'stubs' => [ 'enabled' => true, - 'path' => base_path() . '/storage/stubs', + 'path' => base_path() . '/app/Console/Commands/stubs', 'files' => [ 'start' => 'start.php', 'routes' => 'Http/routes.php',