OrganizationController.js
Home
/
app /
Controllers /
Http /
OrganizationController.js
'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 OrganizationType = use('App/Models/OrganizationType')
const Type = use('App/Models/OrganizationType')
const { validate, validateAll } = use('Validator')
const Env = use('Env')
/**
* 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('type').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 }) {
const types = await Type.all()
return view.render('organizations.create', {
types: types.toJSON()
})
}
/**
* 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(Env.get('APP_URL') + '/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 }) {
const organization = await Organization.findOrFail(params.id)
const organization_types = await OrganizationType.all()
return view.render('organizations.edit', {
organization: organization.toJSON(),
types: organization_types.toJSON()
})
}
/**
* Update organization details.
* PUT or PATCH organizations/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async update ({ params, request, response, session }) {
const data = request.only(['name', 'email', 'phone', 'organization_type_id'])
const validation = await validateAll(data, {
name: 'required',
email: 'required',
phone: 'required',
organization_type_id: 'required'
})
if (validation.fails()) {
session
.withErrors(validation.messages())
.flashAll()
return response.redirect('back')
}
const organization = await Organization.findOrFail(params.id)
organization.merge(data)
await organization.save()
return response.redirect(Env.get('APP_URL') + '/organizations')
}
/**
* 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(Env.get('APP_URL') + '/organizations')
}
}
module.exports = OrganizationController