StatusController.js

147 lines | 3.363 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 Status = use('App/Models/Status')
const { validate, validateAll } = use('Validator')
const Env = use('Env')
/**
 * Resourceful controller for interacting with statuses
 */
class StatusController {
  /**
   * Show a list of all statuses.
   * GET statuses
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async index ({ request, response, view }) {
    const statuses = await Status.all()
  
    return view.render('status.index', {
      statuses: statuses.toJSON()
    })
  }

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

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

    const data = request.only(['status'])

    const validation = await validate(request.all(), {
      status: 'required',
    }, {
      'status.required': `Status is required`,
    })

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

    await Status.create(data)

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

  }

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

  /**
   * Update status details.
   * PUT or PATCH statuses/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async update ({ params, request, response }) {
    const data = request.only(['status'])

    const validation = await validateAll(data, {
      status: 'required'
    })

    if (validation.fails()) {
      session
        .withErrors(validation.messages())
        .flashAll()

      return response.redirect('back')
    }

    const status = await Status.findOrFail(params.id)
    status.merge(data)
    await status.save()

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

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

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

module.exports = StatusController