104 lines
3.4 KiB
Handlebars
Executable File
104 lines
3.4 KiB
Handlebars
Executable File
import type { HttpContext } from '@adonisjs/core/http'
|
|
import {{pascalCaseName}} from '#modules/{{capName}}/model/{{snakeCaseName}}'
|
|
import { create{{pascalCaseName}}ApiValidator } from '#modules/{{capName}}/api/validators/{{snakeCaseName}}_create_api_validator'
|
|
import { update{{pascalCaseName}}ApiValidator } from '#modules/{{capName}}/api/validators/{{snakeCaseName}}_update_api_validator'
|
|
|
|
export default class {{pascalCaseName}}ApiController {
|
|
/**
|
|
* GET /api/{{name}}
|
|
*/
|
|
/**
|
|
* @index
|
|
* @operationId get{{pascalCaseName}}
|
|
* @description Returns array of records
|
|
* @responseBody 200 - [{{{swaggerModel}}}]
|
|
* @responseHeader 200 - X-pages - A description of the header - @example(test)
|
|
*/
|
|
async index({ response }: HttpContext) {
|
|
const result = await {{pascalCaseName}}.all()
|
|
return response.ok(result)
|
|
}
|
|
|
|
/**
|
|
* GET /api/{{name}}/:id
|
|
*/
|
|
|
|
/**
|
|
* @show
|
|
* @operationId show{{pascalCaseName}}
|
|
* @description Returns record
|
|
* @responseBody 200 - {{{swaggerModel}}}
|
|
* @responseBody 404 - { "message": "Not found" }
|
|
* @responseHeader 200 - X-pages - A description of the header - @example(test)
|
|
*/
|
|
async show({ params, response }: HttpContext) {
|
|
const record = await {{pascalCaseName}}.find(params.id)
|
|
if (!record) return response.notFound({ message: 'Not found' })
|
|
return response.ok(record)
|
|
}
|
|
|
|
/**
|
|
* POST /api/{{name}}
|
|
*/
|
|
|
|
/**
|
|
* @store
|
|
* @operationId store{{pascalCaseName}}
|
|
* @description Creates and Returns record
|
|
* @requestBody <create{{pascalCaseName}}ApiValidator>
|
|
* @responseBody 200 - {{{swaggerModel}}}
|
|
* @responseBody 404 - { "message": "Not found" }
|
|
* @responseBody 422 - <Error422> - Returns an error if the request body fails validation.
|
|
* @responseHeader 200 - X-pages - A description of the header - @example(test)
|
|
*/
|
|
async store({ request, response }: HttpContext) {
|
|
const payload = await create{{pascalCaseName}}ApiValidator.validate(request.all())
|
|
const record = await {{pascalCaseName}}.create(payload)
|
|
return response.created(record)
|
|
}
|
|
|
|
/**
|
|
* PUT /api/{{name}}/:id
|
|
*/
|
|
|
|
/**
|
|
* @update
|
|
* @operationId update{{pascalCaseName}}
|
|
* @description Updates and Returns record
|
|
* @requestBody <update{{pascalCaseName}}ApiValidator>
|
|
* @responseBody 200 - {{{swaggerModel}}}
|
|
* @responseBody 404 - { "message": "Not found" }
|
|
* @responseBody 422 - <Error422> - Returns an error if the request body fails validation.
|
|
* @responseHeader 200 - X-pages - A description of the header - @example(test)
|
|
*/
|
|
async update({ params, request, response }: HttpContext) {
|
|
const record = await {{pascalCaseName}}.find(params.id)
|
|
if (!record) return response.notFound({ message: 'Not found' })
|
|
|
|
const payload = await update{{pascalCaseName}}ApiValidator.validate(request.all())
|
|
record.merge(payload)
|
|
await record.save()
|
|
return response.ok(record)
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/{{name}}/:id
|
|
*/
|
|
|
|
/**
|
|
* @destroy
|
|
* @operationId destroy{{pascalCaseName}}
|
|
* @description Destroys record
|
|
* @paramPath id - The ID of the source - @required
|
|
* @responseBody 200 - { "message": "Deleted" }
|
|
* @responseHeader 200 - X-pages - A description of the header - @example(test)
|
|
*/
|
|
async destroy({ params, response }: HttpContext) {
|
|
const record = await {{pascalCaseName}}.find(params.id)
|
|
if (!record) return response.notFound({ message: 'Not found' })
|
|
|
|
await record.delete()
|
|
return response.ok({ message: 'Deleted' })
|
|
}
|
|
}
|