Working on CRUD

This commit is contained in:
Hillel Coren 2016-12-08 17:09:45 +02:00
parent 2762debd59
commit bb55beaa64
10 changed files with 189 additions and 3 deletions

View File

@ -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()

View File

@ -0,0 +1,70 @@
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Nwidart\Modules\Commands\GeneratorCommand;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
class MakeRepository extends GeneratorCommand
{
use ModuleCommandTrait;
protected $argumentName = 'name';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'ninja:make-repository';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create repository stub';
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the datatable.'],
['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
];
}
public function getTemplateContents()
{
$module = $this->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';
}
}

View File

@ -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

View File

@ -12,5 +12,6 @@
"files": [
"start.php"
],
"icon": "th-large"
"icon": "th-large",
"plural": "$LOWER_NAME$"
}

View File

@ -0,0 +1,78 @@
<?php
namespace App\Ninja\Repositories;
use DB;
use App\Models\$STUDLY_NAME$;
//use App\Events\$STUDLY_NAME$WasCreated;
//use App\Events\$STUDLY_NAME$WasUpdated;
class $STUDLY_NAME$Repository extends BaseRepository
{
public function getClassName()
{
return '$NAMESPACE$\$STUDLY_NAME$';
}
public function find($filter = null, $userId = false)
{
$query = DB::table('$LOWER_NAME$')
->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;
}
}

View File

@ -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');
});

View File

@ -25,6 +25,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\TestOFX',
'App\Console\Commands\MakeModule',
'App\Console\Commands\MakeDatatable',
'App\Console\Commands\MakeRepository',
];
/**

View File

@ -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 {

View File

@ -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',