StewardController.js

128 lines | 3.062 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 } = use('Validator')

/**
 * 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'])
    var data = {}
    data['contact_id'] = request.input('contact_id')
    data['start_date'] = new Date()
    data['end_date'] = new Date()

    var element = request.input('element_id')

    var steward = await Steward.create(data)
    await steward.elements().attach([element])
    
    return response.redirect('/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 }) {
  }

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

  /**
   * 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('/stewards')
  }
}

module.exports = StewardController