DataInventory

chore: add contacts

1/15/2020 10:51:07 AM

Details

diff --git a/app/Controllers/Http/ContactController.js b/app/Controllers/Http/ContactController.js
new file mode 100644
index 0000000..c073c3b
--- /dev/null
+++ b/app/Controllers/Http/ContactController.js
@@ -0,0 +1,127 @@
+'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 { 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().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 }) {
+    return view.render('contacts.create')
+  }
+
+  /**
+   * Create/save a new contact.
+   * POST contacts
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   */
+  async store ({ request, response }) {
+    const data = request.only(['email', 'first_name', 'last_name', 'phone'])
+
+    const validation = await validate(request.all(), {
+      email: 'required',
+      first_name: 'required',
+      last_name: 'required',
+      phone: '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`,
+    })
+
+    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
diff --git a/app/Models/Contact.js b/app/Models/Contact.js
new file mode 100644
index 0000000..8462701
--- /dev/null
+++ b/app/Models/Contact.js
@@ -0,0 +1,9 @@
+'use strict'
+
+/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
+const Model = use('Model')
+
+class Contact extends Model {
+}
+
+module.exports = Contact
diff --git a/resources/views/contacts/create.edge b/resources/views/contacts/create.edge
new file mode 100644
index 0000000..823a41b
--- /dev/null
+++ b/resources/views/contacts/create.edge
@@ -0,0 +1,47 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ route('/contacts.store') }}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">First Name</label>
+    <input class="input" type="text" name="first_name" placeholder="Arthur" value="{{ old('first_name', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('first_name'), hasErrorFor('first_name')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Contact Last Name</label>
+    <input class="input" type="text" name="last_name" placeholder="Fish" value="{{ old('last_name', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('last_name'), hasErrorFor('last_name')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Contact Email</label>
+    <input class="input" type="text" name="email" placeholder="arthur.fish@oregon.gov" value="{{ old('email', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('email'), hasErrorFor('email')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Contact Phone Number</label>
+    <input class="input" type="text" name="phone" placeholder="Fish" value="{{ old('phone', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('phone'), hasErrorFor('phone')) }}
+  </div>
+
+  <div class="field is-grouped">
+      <div class="control">
+        <button class="button is-link ss-submit" type="submit">Submit</button>
+      </div>
+      <div class="control">
+      <button class="button is-text"><a href="{{ route('/Contacts.index') }}">Cancel</a></button>
+      </div>
+    </div>
+    <!--
+    <div class="notification is-warning">
+      <button class="delete"></button>
+      Building Snapshots for some items may take a couple minutes.  Please be patient!
+    </div>
+    -->
+</form>
+
+@endsection 
\ No newline at end of file
diff --git a/resources/views/contacts/index.edge b/resources/views/contacts/index.edge
new file mode 100644
index 0000000..4e65fbe
--- /dev/null
+++ b/resources/views/contacts/index.edge
@@ -0,0 +1,44 @@
+@layout('layouts.main')
+
+@section('content')
+
+<section class="box is-full-width">
+  <h3 class="is-size-3">Contact Actions</h3>
+  <ul>
+    <li><a href="{{ route('/contacts/create') }}">Add Contact</a></li>
+  </ul>
+</section>
+
+<p>There are {{ contacts.length }} total contacts.</p>
+
+<section class="box">
+  <table class="table">
+    <thead class="thead">
+      <tr>
+        <th>Last Name</th>
+        <th>First Name</th>
+        <th>Email</th>
+        <th>Phone</th>
+        <th>Action</th>
+      </tr>
+    </thead>
+    <tbody class="tbody">
+      @each(contact in contacts)
+      <tr>
+        <td>{{ contact.last_name }}</td>
+        <td>{{ contact.first_name }}</td>
+        <td>{{ contact.email}}</td>
+        <td>{{ contact.phone }}</td>
+        <td>
+          <form action="{{ route('/contacts.destroy', { id: contact.id }) + '?_method=DELETE'}}" method="post">
+            {{ csrfField() }}
+            <button type="submit" name="button" class="is-small button is-danger" onclick="return confirm('Are you sure you want to delete this contact?');">Delete</button>
+          </form>
+        </td>
+      </tr>
+      @endeach
+    </tbody>
+  </table>
+</section>
+
+@endsection
\ No newline at end of file
diff --git a/resources/views/inc/nav.edge b/resources/views/inc/nav.edge
index 9bac3e0..ba9d071 100644
--- a/resources/views/inc/nav.edge
+++ b/resources/views/inc/nav.edge
@@ -14,6 +14,9 @@
     <li>
       <a href="{{ route('/elements.index') }}">Elements</a>
     </li>
+    <li>
+      <a href="{{ route('/contacts.index') }}">Contacts</a>
+    </li>
   </ul>
 </section>
 

start/routes.js 3(+2 -1)

diff --git a/start/routes.js b/start/routes.js
index 8fa8e76..6a81976 100644
--- a/start/routes.js
+++ b/start/routes.js
@@ -21,4 +21,5 @@ Route.on('/').render('index')
 
 // Resource Routes (FULL CRUD)
 Route.resource('/themes','ThemeController')
-Route.resource('/elements', 'ElementController')
\ No newline at end of file
+Route.resource('/elements', 'ElementController')
+Route.resource('/contacts', 'ContactController')
\ No newline at end of file