Skip to content

Commit 6d7b9c4

Browse files
authored
Merge pull request #170 from AthennaIO/develop
feat(command): add routes to route file
2 parents 1a4c979 + 6119ef4 commit 6d7b9c4

10 files changed

+59
-8
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/database",
3-
"version": "4.58.0",
3+
"version": "4.59.0",
44
"description": "The Athenna database handler for SQL/NoSQL.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/commands/MakeCrudCommand.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import { sep } from 'node:path'
11-
import { Json, Path, String } from '@athenna/common'
11+
import { Json, Path, File, String } from '@athenna/common'
1212
import { BaseCommand, Option, Argument, Generator } from '@athenna/artisan'
1313

1414
export class MakeCrudCommand extends BaseCommand {
@@ -154,6 +154,7 @@ export class MakeCrudCommand extends BaseCommand {
154154

155155
if (Config.get('rc.commands.make:crud.controller.enabled', true)) {
156156
task.addPromise('Creating controller', () => this.makeController())
157+
task.addPromise('Adding CRUD routes', () => this.addRoutes())
157158
}
158159

159160
if (Config.get('rc.commands.make:crud.service.enabled', true)) {
@@ -255,7 +256,7 @@ export class MakeCrudCommand extends BaseCommand {
255256

256257
if (this.properties.length - 1 !== i) {
257258
properties += '\n\n'
258-
if (definitions.length) definitions += ',\n'
259+
if (definitions.length && property.custom) definitions += ',\n'
259260
}
260261
})
261262

@@ -392,6 +393,46 @@ export class MakeCrudCommand extends BaseCommand {
392393
await this.rc.pushTo('controllers', importPath).save()
393394
}
394395

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+
395436
public async makeService() {
396437
this.cleanGenerator()
397438

@@ -475,6 +516,7 @@ export class MakeCrudCommand extends BaseCommand {
475516
.fileName(this.toCase(`${this.name}ControllerTest`))
476517
.destination(destination)
477518
.properties({
519+
hasDeletedAt: this.properties.find(p => p.name === 'deletedAt'),
478520
createBody: createBody.slice(0, createBody.length - 2),
479521
updateBody: updateBody.slice(0, updateBody.length - 2),
480522
showAssertBody: showAssertBody.slice(0, showAssertBody.length - 2),

templates/crud-controller-test.edge

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ export default class {{ namePascal }} extends BaseHttpTest {
3737
const response = await request.get('/{{ crudNameLowerPlural }}/' + {{ crudNameLower }}.id)
3838

3939
response.assertStatusCode(200)
40-
response.assertBodyContains({
41-
{{{ showAssertBody }}}
42-
})
40+
response.assertBodyContains({ id: {{ crudNameLower }}.id })
4341
}
4442

4543
@Test()

tests/fixtures/consoles/crud/disabled.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config'))
2323
Rc.setFile(Path.pwd('package.json'))
2424

2525
Path.mergeDirs({
26+
routes: 'tests/fixtures/storage/routes',
2627
models: 'tests/fixtures/storage/app/models',
2728
seeders: 'tests/fixtures/storage/database/seeders',
2829
migrations: 'tests/fixtures/storage/database/migrations',

tests/fixtures/consoles/crud/file-case.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config'))
2323
Rc.setFile(Path.pwd('package.json'))
2424

2525
Path.mergeDirs({
26+
routes: 'tests/fixtures/storage/routes',
2627
models: 'tests/fixtures/storage/app/models',
2728
seeders: 'tests/fixtures/storage/database/seeders',
2829
migrations: 'tests/fixtures/storage/database/migrations',

tests/fixtures/consoles/crud/with-id-and-timestamps.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config'))
2323
Rc.setFile(Path.pwd('package.json'))
2424

2525
Path.mergeDirs({
26+
routes: 'tests/fixtures/storage/routes',
2627
models: 'tests/fixtures/storage/app/models',
2728
seeders: 'tests/fixtures/storage/database/seeders',
2829
migrations: 'tests/fixtures/storage/database/migrations',

tests/fixtures/consoles/crud/with-properties.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config'))
2323
Rc.setFile(Path.pwd('package.json'))
2424

2525
Path.mergeDirs({
26+
routes: 'tests/fixtures/storage/routes',
2627
models: 'tests/fixtures/storage/app/models',
2728
seeders: 'tests/fixtures/storage/database/seeders',
2829
migrations: 'tests/fixtures/storage/database/migrations',

tests/fixtures/consoles/crud/without-id-and-timestamps.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ await Config.loadAll(Path.fixtures('config'))
2323
Rc.setFile(Path.pwd('package.json'))
2424

2525
Path.mergeDirs({
26+
routes: 'tests/fixtures/storage/routes',
2627
models: 'tests/fixtures/storage/app/models',
2728
seeders: 'tests/fixtures/storage/database/seeders',
2829
migrations: 'tests/fixtures/storage/database/migrations',

tests/unit/commands/MakeCrudCommandTest.ts

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
2727
assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts')))
2828
assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts')))
2929
assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts')))
30+
assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts')))
3031
assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations')))
3132
}
3233

@@ -45,6 +46,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
4546
assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts')))
4647
assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts')))
4748
assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts')))
49+
assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts')))
4850
assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations')))
4951
}
5052

@@ -63,6 +65,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
6365
assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts')))
6466
assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts')))
6567
assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts')))
68+
assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts')))
6669
assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations')))
6770
}
6871

@@ -81,6 +84,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
8184
assert.isTrue(await File.exists(Path.fixtures('storage/app/services/UserService.ts')))
8285
assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts')))
8386
assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts')))
87+
assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts')))
8488
assert.isFalse(await Folder.exists(Path.fixtures('storage/database/migrations')))
8589
}
8690

@@ -99,6 +103,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
99103
assert.isFalse(await File.exists(Path.fixtures('storage/app/services/UserService.ts')))
100104
assert.isFalse(await File.exists(Path.fixtures('storage/tests/e2e/UserControllerTest.ts')))
101105
assert.isFalse(await File.exists(Path.fixtures('storage/tests/unit/UserServiceTest.ts')))
106+
assert.isFalse(await File.exists(Path.fixtures('storage/routes/http.ts')))
102107
assert.isFalse(await Folder.exists(Path.fixtures('storage/database/migrations')))
103108
}
104109

@@ -117,6 +122,7 @@ export default class MakeCrudCommandTest extends BaseCommandTest {
117122
assert.isTrue(await File.exists(Path.fixtures('storage/app/services/user.service.ts')))
118123
assert.isTrue(await File.exists(Path.fixtures('storage/tests/e2e/user.controller.test.ts')))
119124
assert.isTrue(await File.exists(Path.fixtures('storage/tests/unit/user.service.test.ts')))
125+
assert.isTrue(await File.exists(Path.fixtures('storage/routes/http.ts')))
120126
assert.isTrue(await Folder.exists(Path.fixtures('storage/database/migrations')))
121127
}
122128
}

0 commit comments

Comments
 (0)