Skip to content

Commit

Permalink
feat(command): add routes to route file
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed May 27, 2024
1 parent d822930 commit 6119ef4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
46 changes: 44 additions & 2 deletions src/commands/MakeCrudCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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'
}
})

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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),
Expand Down
4 changes: 1 addition & 3 deletions templates/crud-controller-test.edge
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/consoles/crud/disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/consoles/crud/file-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/consoles/crud/with-id-and-timestamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/consoles/crud/with-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/consoles/crud/without-id-and-timestamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/commands/MakeCrudCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
}

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

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

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

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

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

0 comments on commit 6119ef4

Please sign in to comment.