diff --git a/app/Console/Commands/MakeClass.php b/app/Console/Commands/MakeClass.php index f895728acb..a4ea33a9a4 100644 --- a/app/Console/Commands/MakeClass.php +++ b/app/Console/Commands/MakeClass.php @@ -34,18 +34,20 @@ class MakeClass extends GeneratorCommand protected function getArguments() { return [ - ['name', InputArgument::REQUIRED, 'The name of the datatable.'], + ['name', InputArgument::REQUIRED, 'The name of the module.'], ['module', InputArgument::REQUIRED, 'The name of module will be used.'], ['class', InputArgument::REQUIRED, 'The name of the class.'], + ['prefix', InputArgument::OPTIONAL, 'The prefix of the class.'], ]; } public function getTemplateContents() { $module = $this->laravel['modules']->findOrFail($this->getModuleName()); + $path = str_replace('/', '\\', config('modules.paths.generator.' . $this->argument('class'))); - return (new Stub('/' . $this->argument('class') . '.stub', [ - 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.' . $this->argument('class')), + return (new Stub('/' . $this->argument('prefix') . $this->argument('class') . '.stub', [ + 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . $path, 'LOWER_NAME' => $module->getLowerName(), 'CLASS' => $this->getClass(), 'STUDLY_NAME' => Str::studly($module->getLowerName()), @@ -65,7 +67,7 @@ class MakeClass extends GeneratorCommand */ protected function getFileName() { - return studly_case($this->argument('name')) . Str::studly($this->argument('class')); + return studly_case($this->argument('prefix')) . studly_case($this->argument('name')) . Str::studly($this->argument('class')); } } diff --git a/app/Console/Commands/MakeModule.php b/app/Console/Commands/MakeModule.php index 30ed6cb1e3..e9de9e2035 100644 --- a/app/Console/Commands/MakeModule.php +++ b/app/Console/Commands/MakeModule.php @@ -54,6 +54,11 @@ class MakeModule extends Command Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'repository']); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'policy']); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'auth-provider']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'presenter']); + + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'request']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'request', 'prefix' => 'create']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'request', 'prefix' => 'edit']); Artisan::call('module:dump'); } diff --git a/app/Console/Commands/stubs/controller.stub b/app/Console/Commands/stubs/controller.stub index 53cc484e0e..5a7c2ef31c 100755 --- a/app/Console/Commands/stubs/controller.stub +++ b/app/Console/Commands/stubs/controller.stub @@ -36,7 +36,7 @@ class $CLASS$ extends BaseController ]); } - public function getDatatable(DatatableService $datatableService) + public function datatable(DatatableService $datatableService) { $search = request()->input('test'); $userId = Auth::user()->filterId(); @@ -70,20 +70,37 @@ class $CLASS$ extends BaseController */ public function store(Request $request) { - $client = $this->$LOWER_NAME$Repo->save($request->input()); + $$LOWER_NAME$ = $this->$LOWER_NAME$Repo->save($request->input()); - Session::flash('message', trans('texts.created_$LOWER_NAME$')); - - return redirect()->to($$LOWER_NAME$->getRoute()); + return redirect()->to($$LOWER_NAME$->present()->editUrl) + ->with('message', trans('texts.created_$LOWER_NAME$')); } /** * Show the form for editing the specified resource. * @return Response */ - public function edit() + public function edit($STUDLY_NAME$Request $request) { - return view('$LOWER_NAME$::edit'); + $$LOWER_NAME$ = $request->entity(); + + $data = [ + '$LOWER_NAME$' => $$LOWER_NAME$, + 'method' => 'PUT', + 'url' => '$LOWER_NAME$/' . $$LOWER_NAME$->public_id, + 'title' => trans('texts.edit_$LOWER_NAME$'), + ]; + + return view('$LOWER_NAME$::edit', $data); + } + + /** + * Show the form for editing a resource. + * @return Response + */ + public function show(Request $request) + { + return redirect()->to("$LOWER_NAME$/{$request->$LOWER_NAME$}/edit"); } /** diff --git a/app/Console/Commands/stubs/createrequest.stub b/app/Console/Commands/stubs/createrequest.stub new file mode 100644 index 0000000000..d8ad07cfb0 --- /dev/null +++ b/app/Console/Commands/stubs/createrequest.stub @@ -0,0 +1,28 @@ +user()->can('create', '$LOWER_NAME$'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + + ]; + } +} diff --git a/app/Console/Commands/stubs/model.stub b/app/Console/Commands/stubs/model.stub index c270ef1650..459c3249de 100755 --- a/app/Console/Commands/stubs/model.stub +++ b/app/Console/Commands/stubs/model.stub @@ -14,9 +14,21 @@ class $CLASS$ extends EntityModel /** * @var string */ - protected $presenter = 'App\Ninja\Presenters\$CLASS$Presenter'; + protected $presenter = 'Modules\$CLASS$\Presenters\$CLASS$Presenter'; + /** + * @var string + */ protected $fillable = $FILLABLE$; + /** + * @var string + */ protected $table = '$LOWER_NAME$'; + + public function getEntityType() + { + return '$LOWER_NAME$'; + } + } diff --git a/app/Console/Commands/stubs/presenter.stub b/app/Console/Commands/stubs/presenter.stub new file mode 100644 index 0000000000..e7c83d571d --- /dev/null +++ b/app/Console/Commands/stubs/presenter.stub @@ -0,0 +1,10 @@ +select( '$LOWER_NAME$.public_id', '$LOWER_NAME$.deleted_at', + '$LOWER_NAME$.created_at', '$LOWER_NAME$.is_deleted', '$LOWER_NAME$.user_id' ); @@ -56,7 +57,7 @@ class $STUDLY_NAME$Repository extends BaseRepository } */ - $entity->fill($data); + //$entity->fill($data); $entity->save(); /* diff --git a/app/Console/Commands/stubs/request.stub b/app/Console/Commands/stubs/request.stub index 96e06163cd..4bf64c64eb 100755 --- a/app/Console/Commands/stubs/request.stub +++ b/app/Console/Commands/stubs/request.stub @@ -2,29 +2,9 @@ namespace $NAMESPACE$; -use Illuminate\Foundation\Http\FormRequest; +use App\Http\Requests\EntityRequest; -class $CLASS$ extends FormRequest +class $CLASS$Request extends EntityRequest { - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } - - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } + protected $entityType = '$LOWER_NAME$'; } diff --git a/app/Console/Commands/stubs/routes.stub b/app/Console/Commands/stubs/routes.stub index eefbac26fc..85fb4a8e92 100755 --- a/app/Console/Commands/stubs/routes.stub +++ b/app/Console/Commands/stubs/routes.stub @@ -3,5 +3,5 @@ Route::group(['middleware' => 'auth', 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers'], function() { Route::resource('$LOWER_NAME$', '$STUDLY_NAME$Controller'); - Route::get('api/$LOWER_NAME$', '$STUDLY_NAME$Controller@getDatatable'); + Route::get('api/$LOWER_NAME$', '$STUDLY_NAME$Controller@datatable'); }); diff --git a/app/Console/Commands/stubs/updaterequest.stub b/app/Console/Commands/stubs/updaterequest.stub new file mode 100644 index 0000000000..7577668027 --- /dev/null +++ b/app/Console/Commands/stubs/updaterequest.stub @@ -0,0 +1,28 @@ +user()->can('edit', '$LOWER_NAME$'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + + ]; + } +} diff --git a/app/Ninja/Presenters/EntityPresenter.php b/app/Ninja/Presenters/EntityPresenter.php index 66e6518c87..5eec3d513c 100644 --- a/app/Ninja/Presenters/EntityPresenter.php +++ b/app/Ninja/Presenters/EntityPresenter.php @@ -1,5 +1,6 @@ entity->getEntityType(); + $type = Utils::pluralizeEntityType($this->entity->getEntityType()); $id = $this->entity->public_id; - $link = sprintf('/%ss/%s', $type, $id); + $link = sprintf('/%s/%s', $type, $id); return URL::to($link); } + public function editUrl() + { + return $this->url() . '/edit'; + } + public function statusLabel() { $class = $text = ''; diff --git a/config/modules.php b/config/modules.php index 14cea0b879..ffec59c66a 100644 --- a/config/modules.php +++ b/config/modules.php @@ -116,6 +116,7 @@ return [ 'datatable' => 'Datatables', 'policy' => 'Policies', 'auth-provider' => 'AuthProviders', + 'presenter' => 'Presenters', ], ], /*