diff --git a/module/docs/module-docs.md b/module/docs/module-docs.md new file mode 100644 index 0000000..fe65ee2 --- /dev/null +++ b/module/docs/module-docs.md @@ -0,0 +1,67 @@ +# moduleName API DOCS + +# Base URL +BaseUrl + +# Headers + +Authorization: key your token + +Accept : application/json + +# API + +| Route | Request Method | Parameters | Response | +| ----------- | ----------- |----------- |---------- | +| /api/admin/routeName | POST | [Create Parmaters](#Create)|[Response](#Response)| +| /api/admin/routeName | GET |-| [Response](#Response) | +|/api/admin/routeName/{id} | GET | - | [Response](#Response) | +|/api/admin/routeName/{id} |PUT | [Update Parmaters](#Update)|[Response](#Response) | +|/api/admin/routeName/{id} |DELETE | -|[Response](#Response)| +|/api/routeName |GET |-| [Response](#Response)| +|/api/routeName/{id} |GET |-|[Response](#Response)| + + +# Create new moduleName + +```json +data +``` + +# Update moduleName + +```json +data +``` +# Responses + +## Unauthorized error + +__*Response code : 401*__ +```json +{ + "message" : "Unauthenticated" +} +``` + +## Validation error +__*Response code : 422*__ + +```json +{ + "errors" { + "Key" : "Error message" + } +} +``` +## Success +__*Response code : 200*__ +```json +{ + "records" [ + { + + }, + ] +} +``` \ No newline at end of file diff --git a/module/docs/module.postman.json b/module/docs/module.postman.json new file mode 100644 index 0000000..403bc67 --- /dev/null +++ b/module/docs/module.postman.json @@ -0,0 +1,227 @@ +{ + "info": { + "_postman_id": "0402fe17-e481-4c12-a34e-569e61795aba", + "name": "{moduleName} Postman", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Create {moduleName} ", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + }, + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": "{formData}" + }, + "url": { + "raw": "{baseUrl}/api/admin/{routeUri}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "admin" + ] + } + }, + "response": [] + }, + { + "name": "Update {moduleName}", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + }, + { + "key": "Authorization", + "type": "text", + "value": "Bearer Token" + } + ], + "body": { + "mode": "urlencoded", + "urlencoded": "{formData}" + }, + "url": { + "raw": "{baseUrl}/api/admin/{routeUri}/{id}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "admin", + "{id}" + ] + } + }, + "response": [] + }, + { + "name": "Listing of {moduleName}", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + }, + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [] + }, + "url": { + "raw": "{baseUrl}/api/admin/{moduleName}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "admin" + ] + } + }, + "response": [] + }, + { + "name": "Get single of {moduleName} ", + "request": { + "method": "GET", + "header": [ + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + }, + { + "key": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + } + ], + "url": { + "raw": "{baseUrl}/api/admin/{routeUri}/{id}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "admin", + "{id}" + ] + } + }, + "response": [] + }, + { + "name": "Delete {moduleName} record", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + }, + { + "key": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + } + ], + "url": { + "raw": "{baseUrl}/api/admin/{routeUri}/{id}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "admin", + "{id}" + ] + } + }, + "response": [] + }, + { + "name": "Show single record of {moduleName} in front", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + }, + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + } + ], + "url": { + "raw": "{baseUrl}/api/{routeUri}/{id}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api", + "{id}" + ] + } + }, + "response": [] + }, + { + "name": "Show list {moduleName} in front", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "name": "Content-Type", + "value": "application/x-www-form-urlencoded", + "type": "text" + }, + { + "key": "Authorization", + "value": "Bearer Token", + "type": "text" + } + ], + "url": { + "raw": "{baseUrl}/api/{routeUri}", + "protocol":"{protocol}", + "host":"{host}", + "path": [ + "api" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/src/HZ/Illuminate/Organizer/Console/Commands/EngezController.php b/src/HZ/Illuminate/Organizer/Console/Commands/EngezController.php index 4397254..603261f 100644 --- a/src/HZ/Illuminate/Organizer/Console/Commands/EngezController.php +++ b/src/HZ/Illuminate/Organizer/Console/Commands/EngezController.php @@ -92,8 +92,6 @@ public function validateArguments() */ public function init() { - // $this->root = Mongez::packagePath(); - $this->info['controllerName'] = Str::studly($this->argument('controller')); $this->info['moduleName'] = Str::studly($this->option('module')); $this->info['type'] = $this->argument('type'); diff --git a/src/HZ/Illuminate/Organizer/Console/Commands/EngezModel.php b/src/HZ/Illuminate/Organizer/Console/Commands/EngezModel.php index e452a01..9bb8c10 100644 --- a/src/HZ/Illuminate/Organizer/Console/Commands/EngezModel.php +++ b/src/HZ/Illuminate/Organizer/Console/Commands/EngezModel.php @@ -16,7 +16,7 @@ class EngezModel extends Command implements EngezInterface * * @var string */ - protected $signature = 'engez:model {model} {--module=}'; + protected $signature = 'engez:model {model} {--module=} {--data=}'; /** * The console command description. @@ -49,6 +49,11 @@ public function handle() $this->init(); $this->validateArguments(); $this->create(); + + if ($this->databaseName == 'mongodb' && $this->option('data')) { + $this->createSchema(); + } + $this->info('Model created successfully'); } @@ -126,6 +131,33 @@ public function create() $this->info['modelPath'] = $modelPath . '\\' . $modelName; // create the file $this->createFile("$modelDirectory/{$modelName}.php", $content, 'Model'); + } + + /** + * Create schema of table in mongo + * + * @param string $dataFileName + * @return void + */ + protected function createSchema() + { + $defaultContent = [ + '_id' => "objectId", + 'id'=>'int', + ]; + + $databaseFileName = strtolower(str::plural($this->info['moduleName'])); + + $path = $this->modulePath("database/migrations"); + + $customData = explode(',', $this->option('data')) ?? []; + + unset($customData['id'], $customData['_id']); + + $customData = array_fill_keys($customData, 'string'); + + $content = array_merge($defaultContent, $customData); - } + $this->createFile("$path/{$databaseFileName}.json", json_encode($content, JSON_PRETTY_PRINT), 'Schema'); + } } diff --git a/src/HZ/Illuminate/Organizer/Console/Commands/ModuleBuilder.php b/src/HZ/Illuminate/Organizer/Console/Commands/ModuleBuilder.php index fecdc02..c6342e0 100644 --- a/src/HZ/Illuminate/Organizer/Console/Commands/ModuleBuilder.php +++ b/src/HZ/Illuminate/Organizer/Console/Commands/ModuleBuilder.php @@ -4,11 +4,12 @@ use File; use Exception; use Illuminate\Support\Str; -use Illuminate\Support\Arr; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; use HZ\Illuminate\Organizer\Helpers\Mongez; use HZ\Illuminate\Organizer\Traits\Console\EngezTrait; +use HZ\Illuminate\Organizer\Helpers\docs\postman\Postman; +use HZ\Illuminate\Organizer\Helpers\docs\markdown\MarkDown; class ModuleBuilder extends Command { @@ -145,8 +146,12 @@ protected function create() $this->info('Generating routes files'); $this->createRoutes(); + + $this->info('Generating Module Postman File'); + $this->generatePostmanModule(); - $this->info('Module has been created successfully'); + $this->info('Generating Module Docs'); + $this->generateModuleDocs(); $this->info('Updating configurations.'); $this->updateConfig(); @@ -360,6 +365,7 @@ protected function createMigration($databaseFileName, $databaseDriver) { $path = 'app/modules/'.$this->moduleName.'/database/migrations'; $content = File::get($this->path("Migrations/".$databaseDriver."-migration.php")); + $content = str_ireplace("TableName", "{$databaseFileName}", $content); if (isset($this->info['data'])) { @@ -499,4 +505,46 @@ protected function setModuleToFile() Mongez::setStored('modules', $content); Mongez::updateStorageFile(); } + + /** + * Generate module postman + * + * @return void + */ + protected function generatePostmanModule() + { + $data = []; + if (isset($this->info['data'])) $data = $this->info['data']; + + $postman = new Postman([ + 'moduleName' => $this->info['modelName'], + 'data' => $data + ]); + + $path = $this->modulePath($this->info['moduleName']); + + $content = $postman->getContent(); + $this->createFile("$path.json", $content, 'PostmanFile'); + } + + /** + * Generate module documentation + * + * @return void + */ + protected function generateModuleDocs() + { + $data = []; + if (isset($this->info['data'])) $data = $this->info['data']; + + $postman = new MarkDown([ + 'moduleName' => $this->info['modelName'], + 'data' => $data + ]); + + $path = $this->modulePath($this->info['moduleName']); + + $content = $postman->getContent(); + $this->createFile("$path.md", $content, 'Docs'); + } } \ No newline at end of file diff --git a/src/HZ/Illuminate/Organizer/Helpers/docs/markdown/MarkDown.php b/src/HZ/Illuminate/Organizer/Helpers/docs/markdown/MarkDown.php new file mode 100644 index 0000000..07505fd --- /dev/null +++ b/src/HZ/Illuminate/Organizer/Helpers/docs/markdown/MarkDown.php @@ -0,0 +1,87 @@ +moduleName = $data['moduleName']; + $this->data = $data['data']; + $this->setMarkDown(); + } + + /** + * Set Postman details. + * + * @return void + */ + protected function setMarkDown() + { + $content = File::get($this->path("Docs/moduleDocs/README.md")); + + // replace postman name + $content = str_ireplace("moduleName", $this->moduleName, $content); + + // replace base url + $content = str_ireplace("baseUrl", url('/'), $content); + + $content = str_ireplace("routeName", strtolower(str::plural($this->moduleName)), $content); + + $data = '{ +'; + foreach ($this->data as $key) + { + $data .= '"'.$key.'"'.' : '.'"text", +'; + } + $data.='}'; + + $content = str_ireplace("data", $data, $content); + + return $this->content = $content; + } + + /** + * Get content of postman json file + * + * @return mixed + */ + public function getContent() + { + return $this->content; + } +} \ No newline at end of file diff --git a/src/HZ/Illuminate/Organizer/Helpers/docs/postman/Postman.php b/src/HZ/Illuminate/Organizer/Helpers/docs/postman/Postman.php new file mode 100644 index 0000000..ae677f4 --- /dev/null +++ b/src/HZ/Illuminate/Organizer/Helpers/docs/postman/Postman.php @@ -0,0 +1,120 @@ +moduleName = $data['moduleName']; + + $this->data = []; + foreach ($data['data'] as $input) { + $this->data [] = [ + 'key' => $input, + 'type' => 'text', + ]; + } + $this->setPostman(); + } + + /** + * Set Postman details. + * + * @return void + */ + protected function setPostman() + { + $content = File::get($this->path("Docs/postman/module.postman.json")); + + // replace postman name + $content = str_ireplace("{postmanName}", $this->moduleName.' Module', $content); + + // replace module name + $content = str_ireplace("{moduleName}", $this->moduleName, $content); + + // replace base url + $content = str_ireplace("{baseUrl}", url('/'), $content); + // replace routeUri + $content = str_ireplace("{routeUri}", strtolower(str::plural($this->moduleName)), $content); + + $content = json_decode($content); + + // Set request details + foreach ($content->item as $item) + { + // set parameters of Add and update request + if ($item->request->method == 'POST') { + $item->request->body->formdata = json_decode(json_encode($this->data), false); + } elseif ($item->request->method == 'PUT') { + $item->request->body->urlencoded = json_decode(json_encode($this->data), false); + } + + // Set host name to request + $item->request->url->host = request()->getHost(); + $defaultPath = $item->request->url->path; + + // Set path to request + if (in_array("{id}", $defaultPath)) { + $defaultPath [count($defaultPath)-1] = strtolower(str::plural($this->moduleName)); + $defaultPath [] = "{id}"; + } else { + $defaultPath [] = strtolower(str::plural($this->moduleName)); + } + $path = explode("/", request()->path()); + $path = array_merge($path, $defaultPath); + $item->request->url->path = array_values(array_filter(array_unique($path))); + + // Set protocol to request. + $protocol = preg_replace('/[0-9]+/', '', request()->getProtocolVersion()); + $protocol = str_ireplace("/.", '', $protocol); + $item->request->url->protocol = strtolower($protocol); + } + + $this->content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + } + + /** + * Get content of file. + * + * @return mixed + */ + public function getContent() + { + return $this->content; + } +} \ No newline at end of file