From ce724fe1c9fa19c4751930b1367de2f151a991cf Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 8 Dec 2016 21:37:07 +0200 Subject: [PATCH] Working on CRUD --- app/Console/Commands/stubs/controller.stub | 14 +++++- app/Console/Commands/stubs/model.stub | 2 + app/Console/Commands/stubs/repository.stub | 26 +++++------- app/Console/Commands/stubs/routes.stub | 6 +-- app/Console/Commands/stubs/views/edit.stub | 47 +++++++++++++++++++++ app/Console/Commands/stubs/views/index.stub | 9 ---- app/Models/EntityModel.php | 3 +- config/modules.php | 4 +- 8 files changed, 76 insertions(+), 35 deletions(-) create mode 100755 app/Console/Commands/stubs/views/edit.stub delete mode 100755 app/Console/Commands/stubs/views/index.stub diff --git a/app/Console/Commands/stubs/controller.stub b/app/Console/Commands/stubs/controller.stub index 02f63e54a238..53cc484e0e39 100755 --- a/app/Console/Commands/stubs/controller.stub +++ b/app/Console/Commands/stubs/controller.stub @@ -53,7 +53,14 @@ class $CLASS$ extends BaseController */ public function create() { - return view('$LOWER_NAME$::create'); + $data = [ + '$LOWER_NAME$' => null, + 'method' => 'POST', + 'url' => '$LOWER_NAME$', + 'title' => trans('texts.new_$LOWER_NAME$'), + ]; + + return view('$LOWER_NAME$::edit', $data); } /** @@ -63,6 +70,11 @@ class $CLASS$ extends BaseController */ public function store(Request $request) { + $client = $this->$LOWER_NAME$Repo->save($request->input()); + + Session::flash('message', trans('texts.created_$LOWER_NAME$')); + + return redirect()->to($$LOWER_NAME$->getRoute()); } /** diff --git a/app/Console/Commands/stubs/model.stub b/app/Console/Commands/stubs/model.stub index ff25fc5e5763..c270ef165088 100755 --- a/app/Console/Commands/stubs/model.stub +++ b/app/Console/Commands/stubs/model.stub @@ -17,4 +17,6 @@ class $CLASS$ extends EntityModel protected $presenter = 'App\Ninja\Presenters\$CLASS$Presenter'; protected $fillable = $FILLABLE$; + + protected $table = '$LOWER_NAME$'; } diff --git a/app/Console/Commands/stubs/repository.stub b/app/Console/Commands/stubs/repository.stub index 22059999f5b8..c23f3550a4d6 100644 --- a/app/Console/Commands/stubs/repository.stub +++ b/app/Console/Commands/stubs/repository.stub @@ -3,7 +3,7 @@ namespace $NAMESPACE$; use DB; -use App\Models\$STUDLY_NAME$; +use Modules\$STUDLY_NAME$\Models\$STUDLY_NAME$; use App\Ninja\Repositories\BaseRepository; //use App\Events\$STUDLY_NAME$WasCreated; //use App\Events\$STUDLY_NAME$WasUpdated; @@ -46,24 +46,18 @@ class $STUDLY_NAME$Repository extends BaseRepository return $query; } - public function save($data, $client = null) + public function save($data, $$LOWER_NAME$ = null) { - $publicId = isset($data['public_id']) ? $data['public_id'] : false; + $entity = $$LOWER_NAME$ ?: $STUDLY_NAME$::createNew(); - if ($client) { - // do nothing - } elseif (!$publicId || $publicId == '-1') { - $client = Client::createNew(); - } else { - $client = Client::scope($publicId)->with('contacts')->firstOrFail(); + /* + if ($entity->is_deleted) { + return $entity; } + */ - if ($client->is_deleted) { - return $client; - } - - $client->fill($data); - $client->save(); + $entity->fill($data); + $entity->save(); /* if (!$publicId || $publicId == '-1') { @@ -73,7 +67,7 @@ class $STUDLY_NAME$Repository extends BaseRepository } */ - return $client; + return $entity; } } diff --git a/app/Console/Commands/stubs/routes.stub b/app/Console/Commands/stubs/routes.stub index 846061bc7294..eefbac26fc00 100755 --- a/app/Console/Commands/stubs/routes.stub +++ b/app/Console/Commands/stubs/routes.stub @@ -1,11 +1,7 @@ 'auth', 'prefix' => '$LOWER_NAME$', 'namespace' => '$MODULE_NAMESPACE$\$STUDLY_NAME$\Http\Controllers'], function() -{ - Route::get('/', '$STUDLY_NAME$Controller@index'); -}); - 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'); }); diff --git a/app/Console/Commands/stubs/views/edit.stub b/app/Console/Commands/stubs/views/edit.stub new file mode 100755 index 000000000000..8b1e1123c22c --- /dev/null +++ b/app/Console/Commands/stubs/views/edit.stub @@ -0,0 +1,47 @@ +@extends('header') + +@section('content') + + {!! Former::open($url) + ->addClass('col-md-10 col-md-offset-1 warn-on-exit') + ->method($method) + ->rules([]) !!} + + @if ($$LOWER_NAME$) + {!! Former::populate($$LOWER_NAME$) !!} +
+ {!! Former::text('public_id') !!} +
+ @endif + +
+
+ +
+
+ + + +
+
+ +
+
+ +
+ + {!! Button::normal(trans('texts.cancel')) + ->large() + ->asLinkTo(URL::to('/$LOWER_NAME$')) + ->appendIcon(Icon::create('remove-circle')) !!} + + {!! Button::success(trans('texts.save')) + ->submit() + ->large() + ->appendIcon(Icon::create('floppy-disk')) !!} + +
+ + {!! Former::close() !!} + +@stop diff --git a/app/Console/Commands/stubs/views/index.stub b/app/Console/Commands/stubs/views/index.stub deleted file mode 100755 index 9d445e5bf68b..000000000000 --- a/app/Console/Commands/stubs/views/index.stub +++ /dev/null @@ -1,9 +0,0 @@ -@extends('header') - -@section('content') -

Hello World

- -

- This view is loaded from module: {!! config('$LOWER_NAME$.name') !!} -

-@stop diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index b50660016c6e..0fef21200f6d 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -65,14 +65,13 @@ class EntityModel extends Eloquent // store references to the original user/account to prevent needing to reload them $entity->setRelation('user', $user); $entity->setRelation('account', $account); - + if (method_exists($className, 'trashed')){ $lastEntity = $className::whereAccountId($entity->account_id)->withTrashed(); } else { $lastEntity = $className::whereAccountId($entity->account_id); } - if (static::$hasPublicId) { $lastEntity = $lastEntity->orderBy('public_id', 'DESC') ->first(); diff --git a/config/modules.php b/config/modules.php index 0f13a16e0b63..14cea0b87955 100644 --- a/config/modules.php +++ b/config/modules.php @@ -29,7 +29,7 @@ return [ 'start' => 'start.php', 'routes' => 'Http/routes.php', 'json' => 'module.json', - 'views/index' => 'Resources/views/index.blade.php', + 'views/edit' => 'Resources/views/edit.blade.php', 'views/master' => 'Resources/views/layouts/master.blade.php', 'scaffold/config' => 'Config/config.php', 'composer' => 'composer.json', @@ -38,7 +38,7 @@ return [ 'start' => ['LOWER_NAME'], 'routes' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], 'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'], - 'views/index' => ['LOWER_NAME'], + 'views/edit' => ['LOWER_NAME'], 'views/master' => ['STUDLY_NAME'], 'scaffold/config' => ['STUDLY_NAME'], 'composer' => [