StewardController.js

178 lines | 4.607 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 Steward = use('App/Models/Steward')
const Contact = use('App/Models/Contact')
const Element = use('App/Models/Element')
const { validate, validateAll } = use('Validator')
const Env = use('Env')

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

  /**
   * Render a form to be used for creating a new steward.
   * GET stewards/create
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async create ({ request, response, view }) {
    const contacts = await Contact.all()
    const elements = await Element.all()

    return view.render('stewards.create', {
      contacts: contacts.toJSON(),
      elements: elements.toJSON()
    })
  }

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

    const data = request.only(['contact_id', 'element_id', 'start_date', 'end_date'])

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

    if(validation.fails()) {
      session
        .withErrors(validation.messages())
        .flashAll()
      return response.redirect('back')
    }
    var fData = {
      'contact_id': data['contact_id'],
      'start_date': data['start_date'] || null,
      'end_date': data['end_date'] || null
    }
    var steward = await Steward.create(fData)
    await steward.elements().attach([data['element_id']])
    
    return response.redirect(Env.get('APP_URL') + '/stewards')

  }

  /**
   * Display a single steward.
   * GET stewards/: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 steward.
   * GET stewards/:id/edit
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async edit ({ params, request, response, view }) {
    const fof = await Steward.findOrFail(params.id)
    const steward = await Steward.query()
      .with('elements')
      .where('id', params.id)
      .first()
    const contacts = await Contact.all()
    const elements = await Element.all()
    return view.render('stewards.edit', {
      steward: steward.toJSON(),
      contacts: contacts.toJSON(),
      elements: elements.toJSON(),
    })
  }

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

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

    if (validation.fails()) {
      session
        .withErrors(validation.messages())
        .flashAll()
      return response.redirect('back')
    }
    const steward = await Steward.findOrFail(params.id)

    const prev_steward = await steward.elements().first()
    await steward.elements().detach([prev_steward.id])
    await steward.elements().attach([data['element_id']])
    
    steward.merge({
      'contact_id': data['contact_id'],
      'start_date': data['start_date'] || null,
      'end_date': data['end_date'] || null
    })
    await steward.save()

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

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

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

module.exports = StewardController