diff --git a/package-lock.json b/package-lock.json index 46fbddd..6a02b3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/database", - "version": "4.58.0", + "version": "4.59.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/database", - "version": "4.58.0", + "version": "4.59.0", "license": "MIT", "dependencies": { "@faker-js/faker": "^8.4.1", diff --git a/package.json b/package.json index 00d5be1..02a9551 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/database", - "version": "4.58.0", + "version": "4.59.0", "description": "The Athenna database handler for SQL/NoSQL.", "license": "MIT", "author": "João Lenon ", diff --git a/src/commands/MakeCrudCommand.ts b/src/commands/MakeCrudCommand.ts index 18d76fe..6ea371b 100644 --- a/src/commands/MakeCrudCommand.ts +++ b/src/commands/MakeCrudCommand.ts @@ -8,7 +8,7 @@ */ import { sep } from 'node:path' -import { Json, Path, String } from '@athenna/common' +import { Json, Path, File, String } from '@athenna/common' import { BaseCommand, Option, Argument, Generator } from '@athenna/artisan' export class MakeCrudCommand extends BaseCommand { @@ -154,6 +154,7 @@ export class MakeCrudCommand extends BaseCommand { if (Config.get('rc.commands.make:crud.controller.enabled', true)) { task.addPromise('Creating controller', () => this.makeController()) + task.addPromise('Adding CRUD routes', () => this.addRoutes()) } if (Config.get('rc.commands.make:crud.service.enabled', true)) { @@ -255,7 +256,7 @@ export class MakeCrudCommand extends BaseCommand { if (this.properties.length - 1 !== i) { properties += '\n\n' - if (definitions.length) definitions += ',\n' + if (definitions.length && property.custom) definitions += ',\n' } }) @@ -392,6 +393,46 @@ export class MakeCrudCommand extends BaseCommand { await this.rc.pushTo('controllers', importPath).save() } + public async addRoutes() { + const routeFilePath = Config.get( + 'rc.commands.make:crud.routeFilePath', + Path.routes(`http.${Path.ext()}`) + ) + + const path = `/${String.pluralize(this.nameLower)}` + const controller = `${this.namePascal}Controller` + + let body = '' + + this.properties + .filter(p => p.custom) + .forEach(property => { + const type = { + string: `'string'`, + number: 1, + boolean: true, + Date: 'new Date()' + } + + body += `${property.name}: ${type[property.type]}, ` + }) + + const routeContent = ` +Route.get('${path}', '${controller}.index') +Route.post('${path}', '${controller}.store').body({ + ${body.slice(0, body.length - 2)} +}) +Route.show('${path}/:id', '${controller}.show') +Route.put('${path}/:id', '${controller}.update') +Route.delete('${path}/:id', '${controller}.delete') +\n` + + await new File( + routeFilePath, + `import { Route } from '@athenna/http'\n\n` + ).append(routeContent) + } + public async makeService() { this.cleanGenerator() @@ -475,6 +516,7 @@ export class MakeCrudCommand extends BaseCommand { .fileName(this.toCase(`${this.name}ControllerTest`)) .destination(destination) .properties({ + hasDeletedAt: this.properties.find(p => p.name === 'deletedAt'), createBody: createBody.slice(0, createBody.length - 2), updateBody: updateBody.slice(0, updateBody.length - 2), showAssertBody: showAssertBody.slice(0, showAssertBody.length - 2), diff --git a/templates/crud-controller-test.edge b/templates/crud-controller-test.edge index b213ef5..4da05df 100644 --- a/templates/crud-controller-test.edge +++ b/templates/crud-controller-test.edge @@ -37,9 +37,7 @@ export default class {{ namePascal }} extends BaseHttpTest { const response = await request.get('/{{ crudNameLowerPlural }}/' + {{ crudNameLower }}.id) response.assertStatusCode(200) - response.assertBodyContains({ - {{{ showAssertBody }}} - }) + response.assertBodyContains({ id: {{ crudNameLower }}.id }) } @Test() diff --git a/tests/fixtures/consoles/crud/disabled.ts b/tests/fixtures/consoles/crud/disabled.ts index ee28c62..9a91c88 100644 --- a/tests/fixtures/consoles/crud/disabled.ts +++ b/tests/fixtures/consoles/crud/disabled.ts @@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config')) Rc.setFile(Path.pwd('package.json')) Path.mergeDirs({ + routes: 'tests/fixtures/storage/routes', models: 'tests/fixtures/storage/app/models', seeders: 'tests/fixtures/storage/database/seeders', migrations: 'tests/fixtures/storage/database/migrations', diff --git a/tests/fixtures/consoles/crud/file-case.ts b/tests/fixtures/consoles/crud/file-case.ts index 956711a..085a6d5 100644 --- a/tests/fixtures/consoles/crud/file-case.ts +++ b/tests/fixtures/consoles/crud/file-case.ts @@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config')) Rc.setFile(Path.pwd('package.json')) Path.mergeDirs({ + routes: 'tests/fixtures/storage/routes', models: 'tests/fixtures/storage/app/models', seeders: 'tests/fixtures/storage/database/seeders', migrations: 'tests/fixtures/storage/database/migrations', diff --git a/tests/fixtures/consoles/crud/with-id-and-timestamps.ts b/tests/fixtures/consoles/crud/with-id-and-timestamps.ts index 32380dc..900988a 100644 --- a/tests/fixtures/consoles/crud/with-id-and-timestamps.ts +++ b/tests/fixtures/consoles/crud/with-id-and-timestamps.ts @@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config')) Rc.setFile(Path.pwd('package.json')) Path.mergeDirs({ + routes: 'tests/fixtures/storage/routes', models: 'tests/fixtures/storage/app/models', seeders: 'tests/fixtures/storage/database/seeders', migrations: 'tests/fixtures/storage/database/migrations', diff --git a/tests/fixtures/consoles/crud/with-properties.ts b/tests/fixtures/consoles/crud/with-properties.ts index cd43bca..03213a1 100644 --- a/tests/fixtures/consoles/crud/with-properties.ts +++ b/tests/fixtures/consoles/crud/with-properties.ts @@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config')) Rc.setFile(Path.pwd('package.json')) Path.mergeDirs({ + routes: 'tests/fixtures/storage/routes', models: 'tests/fixtures/storage/app/models', seeders: 'tests/fixtures/storage/database/seeders', migrations: 'tests/fixtures/storage/database/migrations', diff --git a/tests/fixtures/consoles/crud/without-id-and-timestamps.ts b/tests/fixtures/consoles/crud/without-id-and-timestamps.ts index d5b65f1..b0fdb7b 100644 --- a/tests/fixtures/consoles/crud/without-id-and-timestamps.ts +++ b/tests/fixtures/consoles/crud/without-id-and-timestamps.ts @@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config')) Rc.setFile(Path.pwd('package.json')) Path.mergeDirs({ + routes: 'tests/fixtures/storage/routes', models: 'tests/fixtures/storage/app/models', seeders: 'tests/fixtures/storage/database/seeders', migrations: 'tests/fixtures/storage/database/migrations', diff --git a/tests/unit/commands/MakeCrudCommandTest.ts b/tests/unit/commands/MakeCrudCommandTest.ts index 59dd3bc..8f6594d 100644 --- a/tests/unit/commands/MakeCrudCommandTest.ts +++ b/tests/unit/commands/MakeCrudCommandTest.ts @@ -27,6 +27,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts'))) + assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations'))) } @@ -45,6 +46,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts'))) + assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations'))) } @@ -63,6 +65,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts'))) + assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations'))) } @@ -81,6 +84,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts'))) + assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isFalse(await Folder.exists(Path.fixtures('storage/database/migrations'))) } @@ -99,6 +103,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isFalse(await File.exists(Path.fixtures('storage/app/services/UserService.ts'))) assert.isFalse(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts'))) assert.isFalse(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts'))) + assert.isFalse(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isFalse(await Folder.exists(Path.fixtures('storage/database/migrations'))) } @@ -117,6 +122,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest { assert.isTrue(await File.exists(Path.fixtures('storage/app/services/user.service.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/user.controller.test.ts'))) assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/user.service.test.ts'))) + assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts'))) assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations'))) } }