OrganizationController.js

130 lines | 3.261 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 Organization = use('App/Models/Organization')
const { validate } = use('Validator')

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

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

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

    const data = request.only(['name', 'email', 'phone', 'organization_type_id'])

    const validation = await validate(request.all(), {
      name: 'required',
      email: 'required',
      phone: 'required',
      organization_type_id: 'required',
    }, {
      'name.required': `Theme name is required`,
      'email.required': `Email address is required`,
      'phone.required': `Phone number is required`,
      'organization_type_id.required': `Organization type is required`,
    })

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

    await Organization.create(data)

    return response.redirect('/organizations')

  }

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

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

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

    return response.redirect('/organizations')
  }
}

module.exports = OrganizationController