ContactController.js
Home
/
app /
Controllers /
Http /
ContactController.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 Contact = use('App/Models/Contact')
const Organization = use('App/Models/Organization')
const { validate } = use('Validator')
/**
* Resourceful controller for interacting with contacts
*/
class ContactController {
/**
* Show a list of all contacts.
* GET contacts
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async index ({ request, response, view }) {
const contacts = await Contact.query().with('organization').orderBy('last_name', 'asc').fetch()
return view.render('contacts.index', {
contacts: contacts.toJSON()
})
}
/**
* Render a form to be used for creating a new contact.
* GET contacts/create
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async create ({ request, response, view }) {
const organizations = await Organization.all()
return view.render('contacts.create', {
organizations: organizations.toJSON()
})
}
/**
* Create/save a new contact.
* POST contacts
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async store ({ request, response, session }) {
const data = request.only(['email', 'first_name', 'last_name', 'phone', 'organization_id'])
const validation = await validate(request.all(), {
email: 'required',
first_name: 'required',
last_name: 'required',
phone: 'required',
organization_id: 'required',
}, {
'email.required': `Email is required`,
'first_name.required': `First Name is required`,
'last_name.required': `Last Name is required`,
'phone.required': `Phone Number is required`,
'organization_id': `Orgnaization name required`,
})
if(validation.fails()) {
session.withErrors(validation.messages()).flashAll()
return response.redirect('back')
}
await Contact.create(data)
return response.redirect('/contacts')
}
/**
* Display a single contact.
* GET contacts/: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 contact.
* GET contacts/:id/edit
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
* @param {View} ctx.view
*/
async edit ({ params, request, response, view }) {
}
/**
* Update contact details.
* PUT or PATCH contacts/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async update ({ params, request, response }) {
}
/**
* Delete a contact with id.
* DELETE contacts/:id
*
* @param {object} ctx
* @param {Request} ctx.request
* @param {Response} ctx.response
*/
async destroy ({ params, request, response }) {
const contact = await Contact.findOrFail(params.id)
await contact.delete()
return response.redirect('/contacts')
}
}
module.exports = ContactController