Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmichael123 committed Feb 5, 2025
1 parent 6fcf927 commit 45dd616
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion storage/framework/actions/src/ActivityIndexOrmAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default new Action({
async handle() {
const results = Activity.all()

return json.response(response)
return response.json(results)
},
})
2 changes: 1 addition & 1 deletion storage/framework/actions/src/UserIndexOrmAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default new Action({
async handle() {
const results = User.all()

return json.response(response)
return response.json(results)
},
})
21 changes: 12 additions & 9 deletions storage/framework/core/orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,26 +548,27 @@ export async function writeOrmActions(apiRoute: string, modelName: string, actio
const formattedApiRoute = apiRoute.charAt(0).toUpperCase() + apiRoute.slice(1)

let method = 'GET'
let actionString = `import { Action } from '@stacksjs/actions'\n`
actionString += `import { response } from '@stacksjs/router'\n`
let actionString = `import { Action } from '@stacksjs/actions'\n import { response } from '@stacksjs/router'\n\n`
let handleString = ``

if (apiRoute === 'index') {
handleString += `async handle() {
const results = ${modelName}.all()
return json.response(response)
return response.json(results)
},`

method = 'GET'
}

if (apiRoute === 'show') {
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n\n`
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n import { response } from '@stacksjs/router'\n\n`
handleString += `async handle(request: ${modelName}RequestType) {
const id = request.getParam('id')
return await ${modelName}.findOrFail(Number(id))
const model = await ${modelName}.findOrFail(Number(id))
return response.json(model)
},`

method = 'GET'
Expand All @@ -589,26 +590,28 @@ export async function writeOrmActions(apiRoute: string, modelName: string, actio
}

if (apiRoute === 'store') {
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n\n`
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n import { response } from '@stacksjs/router'\n\n`
handleString += `async handle(request: ${modelName}RequestType) {
await request.validate()
const model = await ${modelName}.create(request.all())
return model
return response.json(model)
},`

method = 'POST'
}

if (apiRoute === 'update') {
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n\n`
actionString += ` import type { ${modelName}RequestType } from '@stacksjs/orm'\n import { response } from '@stacksjs/router'\n\n`
handleString += `async handle(request: ${modelName}RequestType) {
await request.validate()
const id = request.getParam('id')
const model = await ${modelName}.findOrFail(Number(id))
return model.update(request.all())
const result = model.update(request.all())
return response.json(result)
},`

method = 'PATCH'
Expand Down

0 comments on commit 45dd616

Please sign in to comment.