ContactController.js

163 lines | 4.262 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 Contact = use('App/Models/Contact')
const Organization = use('App/Models/Organization')
const { validate, validateAll } = use('Validator')
const Env = use('Env')

/**
 * 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(Env.get('APP_URL') + '/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 }) {
    const contact = await Contact.findOrFail(params.id)
    const organizations = await Organization.all()
    return view.render('contacts.edit', {
      contact: contact.toJSON(),
      organizations: organizations.toJSON()
    })
  }

  /**
   * Update contact details.
   * PUT or PATCH contacts/:id
   *
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Response} ctx.response
   */
  async update ({ params, request, response, session }) {
    const data = request.only(['email', 'first_name', 'last_name', 'phone', 'organization_id'])

    const validation = await validateAll(data, {
      email: 'required',
      first_name: 'required',
      last_name: 'required',
      phone: 'required',
      organization_id: 'required'
    })

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

    const contact = await Contact.findOrFail(params.id)
    contact.merge(data)
    await contact.save()

    return response.redirect(Env.get('APP_URL') + '/contacts')
  }

  /**
   * 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(Env.get('APP_URL') + '/contacts')
  }
}

module.exports = ContactController