|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | import { sep } from 'node:path'
|
11 |
| -import { Json, Path, String } from '@athenna/common' |
| 11 | +import { Json, Path, File, String } from '@athenna/common' |
12 | 12 | import { BaseCommand, Option, Argument, Generator } from '@athenna/artisan'
|
13 | 13 |
|
14 | 14 | export class MakeCrudCommand extends BaseCommand {
|
@@ -154,6 +154,7 @@ export class MakeCrudCommand extends BaseCommand {
|
154 | 154 |
|
155 | 155 | if (Config.get('rc.commands.make:crud.controller.enabled', true)) {
|
156 | 156 | task.addPromise('Creating controller', () => this.makeController())
|
| 157 | + task.addPromise('Adding CRUD routes', () => this.addRoutes()) |
157 | 158 | }
|
158 | 159 |
|
159 | 160 | if (Config.get('rc.commands.make:crud.service.enabled', true)) {
|
@@ -255,7 +256,7 @@ export class MakeCrudCommand extends BaseCommand {
|
255 | 256 |
|
256 | 257 | if (this.properties.length - 1 !== i) {
|
257 | 258 | properties += '\n\n'
|
258 |
| - if (definitions.length) definitions += ',\n' |
| 259 | + if (definitions.length && property.custom) definitions += ',\n' |
259 | 260 | }
|
260 | 261 | })
|
261 | 262 |
|
@@ -392,6 +393,46 @@ export class MakeCrudCommand extends BaseCommand {
|
392 | 393 | await this.rc.pushTo('controllers', importPath).save()
|
393 | 394 | }
|
394 | 395 |
|
| 396 | + public async addRoutes() { |
| 397 | + const routeFilePath = Config.get( |
| 398 | + 'rc.commands.make:crud.routeFilePath', |
| 399 | + Path.routes(`http.${Path.ext()}`) |
| 400 | + ) |
| 401 | + |
| 402 | + const path = `/${String.pluralize(this.nameLower)}` |
| 403 | + const controller = `${this.namePascal}Controller` |
| 404 | + |
| 405 | + let body = '' |
| 406 | + |
| 407 | + this.properties |
| 408 | + .filter(p => p.custom) |
| 409 | + .forEach(property => { |
| 410 | + const type = { |
| 411 | + string: `'string'`, |
| 412 | + number: 1, |
| 413 | + boolean: true, |
| 414 | + Date: 'new Date()' |
| 415 | + } |
| 416 | + |
| 417 | + body += `${property.name}: ${type[property.type]}, ` |
| 418 | + }) |
| 419 | + |
| 420 | + const routeContent = ` |
| 421 | +Route.get('${path}', '${controller}.index') |
| 422 | +Route.post('${path}', '${controller}.store').body({ |
| 423 | + ${body.slice(0, body.length - 2)} |
| 424 | +}) |
| 425 | +Route.show('${path}/:id', '${controller}.show') |
| 426 | +Route.put('${path}/:id', '${controller}.update') |
| 427 | +Route.delete('${path}/:id', '${controller}.delete') |
| 428 | +\n` |
| 429 | + |
| 430 | + await new File( |
| 431 | + routeFilePath, |
| 432 | + `import { Route } from '@athenna/http'\n\n` |
| 433 | + ).append(routeContent) |
| 434 | + } |
| 435 | + |
395 | 436 | public async makeService() {
|
396 | 437 | this.cleanGenerator()
|
397 | 438 |
|
@@ -475,6 +516,7 @@ export class MakeCrudCommand extends BaseCommand {
|
475 | 516 | .fileName(this.toCase(`${this.name}ControllerTest`))
|
476 | 517 | .destination(destination)
|
477 | 518 | .properties({
|
| 519 | + hasDeletedAt: this.properties.find(p => p.name === 'deletedAt'), |
478 | 520 | createBody: createBody.slice(0, createBody.length - 2),
|
479 | 521 | updateBody: updateBody.slice(0, updateBody.length - 2),
|
480 | 522 | showAssertBody: showAssertBody.slice(0, showAssertBody.length - 2),
|
|
0 commit comments