ElementController.js

169 lines | 4.289 kB Blame History Raw Download
'use strict'

/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */
const Element = use('App/Models/Element')
const Theme = use('App/Models/Theme')
const { validate, validateAll } = use('Validator')
const Env = use('Env')

/**
 * Resourceful controller for interacting with elements
 */
class ElementController {
  /**
   * Show a list of all elements.
   * GET elements
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async index ({ request, response, view }) {
    const elements = await Element.query().with('theme').orderBy('name', 'asc').fetch()
    return view.render('elements.index', {
      elements: elements.toJSON()
    })
  }

  /**
   * Render a form to be used for creating a new element.
   * GET elements/create
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async create ({ request, response, view }) {
    const themes = await Theme.all()
    return view.render('elements.create', {
      themes: themes.toJSON()
    })
  }

  /**
   * Create/save a new element.
   * POST elements
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async store ({ session, request, response }) {
    const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])

    const validation = await validateAll(data, {
      name: 'required|max:255',
      theme_id: 'required',
      priority: 'required',
      refresh_rate: 'required'
    }, {
      'name.required': `Theme name is required`,
      'theme_id.required': `Theme is required`,
      'priority.required': `Priority is required`,
      'refresh_rate.required': `Refresh rate is required`,
    })

    if(validation.fails()) {
      session.withErrors(validation.messages()).flashAll()
      return response.redirect('back')
    }

    await Element.create(data)

    return response.redirect(Env.get('APP_URL') + '/elements')
  }

  /**
   * Display a single element.
   * GET elements/: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 element.
   * GET elements/:id/edit
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async edit ({ params, request, response, view }) {
    const element = await Element.findOrFail(params.id)
    const themes = await Theme.all()
    return view.render('elements.edit', {
      element: element.toJSON(),
      themes: themes.toJSON()
    })
  }

  /**
   * Update element details.
   * PUT or PATCH elements/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async update ({ session, params, request, response }) {
    const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])

    const validation = await validateAll(data, {
      name: 'required',
      theme_id: 'required',
      priority: 'required',
      refresh_rate: 'required'
    })

    /**
     * If validation fails, early returns with validation message.
     */
    if (validation.fails()) {
      session
        .withErrors(validation.messages())
        .flashAll()

      return response.redirect('back')
    }

    /**
     * Finding the post and updating fields on it
     * before saving it to the database.
     *
     * ref: http://adonisjs.com/docs/4.1/lucid#_inserts_updates
     */
    const element = await Element.findOrFail(params.id)
    element.merge(data)
    await element.save()

    return response.redirect(Env.get('APP_URL') + '/elements')
  }

  /**
   * Delete a element with id.
   * DELETE elements/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async destroy ({ params, request, response }) {
    const element = await Element.findOrFail(params.id)
    await element.delete()

    return response.redirect(Env.get('APP_URL') + '/elements')
  }
}

module.exports = ElementController