SubmissionController.js

127 lines | 3.129 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 Submission = use('App/Models/Submission')
const Element = use('App/Models/Element')
const Steward = use('App/Models/Steward')
const Status = use('App/Models/Status')
const { validate } = use('Validator')

/**
 * Resourceful controller for interacting with submissions
 */
class SubmissionController {
  /**
   * Show a list of all submissions.
   * GET submissions
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async index ({ request, response, view }) {
    const submissions = await Submission.query()
      .with('element')
      .with('element.theme')
      .with('steward.contact')
      .with('status')
      .orderBy('created_at', 'desc')
      .fetch()
    return view.render('submissions.index', {
      submissions: submissions.toJSON()
    })
  }

  /**
   * Render a form to be used for creating a new submission.
   * GET submissions/create
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async create ({ request, response, view }) {
    const elements = await Element.all()
    const stewards = await Steward.query().with('contact').fetch()
    const status = await Status.all()
    return view.render('submissions.create', {
      elements: elements.toJSON(),
      stewards: stewards.toJSON(),
      statuses: status.toJSON()
    })
  }

  /**
   * Create/save a new submission.
   * POST submissions
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async store ({ request, response, session }) {
    const data = request.only(['name', 'element_id', 'steward_id', 'public', 'status_id'])

    data['upload_date'] = new Date()
    data['creation_date'] = new Date()
    data['published_date'] = new Date()

    await Submission.create(data)

    return response.redirect('/submissions')
  }

  /**
   * Display a single submission.
   * GET submissions/: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 submission.
   * GET submissions/:id/edit
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   * @param {View} ctx.view
   */
  async edit ({ params, request, response, view }) {
  }

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

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

module.exports = SubmissionController