How to structure my adonis app?

#1

i want my adonis rest api to look like this in its URIs:

  1. drivers/id/cars/: access all the cars of driver given his ID
  2. drivers/id/cars/id: access the car known by its ID which belongs to the driver know by his ID

how to structure my application to achieve this?

0 Likes

#2

Adonis Framework offers a command to create a resource controller making available all the http verbs

Simply inside your app, type this command


adonis make:controller DemoController --resource

After that you can go to app/Controllers/Http where you will find something like this


'use strict'

/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */

/**
 * Resourceful controller for interacting with demos
 */
class DemoController {
  /**
   * Show a list of all demos.
   * GET demos
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async index ({ request, response, view }) {
  }

  /**
   * Render a form to be used for creating a new demo.
   * GET demos/create
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async create ({ request, response, view }) {
  }

  /**
   * Create/save a new demo.
   * POST demos
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async store ({ request, response }) {
  }

  /**
   * Display a single demo.
   * GET demos/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async show ({ params, request, response, view }) {
  }

  /**
   * Render a form to update an existing demo.
   * GET demos/:id/edit
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async edit ({ params, request, response, view }) {
  }

  /**
   * Update demo details.
   * PUT or PATCH demos/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async update ({ params, request, response }) {
  }

  /**
   * Delete a demo with id.
   * DELETE demos/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async destroy ({ params, request, response }) {
  }
}

module.exports = DemoController

Now go to app/start/routes.js and write the following


Route.resource('/demo', 'DemoController')

Finally inside your app, through the CLI type this command to see all the routes available for your project

Type the following command


adonis route:list

And you will see something like this


$ adonis route:list
┌───────────────┬───────────┬────────────────────────┬────────────┬──────────────┬────────┐
│ Route         │ Verb(s)   │ Handler                │ Middleware │ Name         │ Domain │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /             │ GET,HEAD  │ Closure                │            │ /            │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app          │ HEAD,GET  │ DemoController.index   │            │ /app.index   │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app/create   │ HEAD,GET  │ DemoController.create  │            │ /app.create  │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app          │ POST      │ DemoController.store   │            │ /app.store   │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app/:id      │ HEAD,GET  │ DemoController.show    │            │ /app.show    │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app/:id/edit │ HEAD,GET  │ DemoController.edit    │            │ /app.edit    │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app/:id      │ PUT,PATCH │ DemoController.update  │            │ /app.update  │        │
├───────────────┼───────────┼────────────────────────┼────────────┼──────────────┼────────┤
│ /app/:id      │ DELETE    │ DemoController.destroy │            │ /app.destroy │        │
└───────────────┴───────────┴────────────────────────┴────────────┴──────────────┴────────┘

0 Likes

#3

Thanks.
I know all that but it does not answer my question since that does not produce the routes I am looking for and noted in bold via my question

0 Likes