DataInventory

feat: CRUD contacts

3/13/2020 9:49:11 AM

Details

diff --git a/app/Controllers/Http/ContactController.js b/app/Controllers/Http/ContactController.js
index 5297329..555bb02 100644
--- a/app/Controllers/Http/ContactController.js
+++ b/app/Controllers/Http/ContactController.js
@@ -5,7 +5,7 @@
 /** @typedef {import('@adonisjs/framework/src/View')} View */
 const Contact = use('App/Models/Contact')
 const Organization = use('App/Models/Organization')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 const Env = use('Env')
 
 /**
@@ -102,6 +102,12 @@ class ContactController {
    * @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()
+    })
   }
 
   /**
@@ -112,7 +118,29 @@ class ContactController {
    * @param {Request} ctx.request
    * @param {Response} ctx.response
    */
-  async update ({ params, request, 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')
   }
 
   /**
diff --git a/resources/views/contacts/edit.edge b/resources/views/contacts/edit.edge
new file mode 100644
index 0000000..e6b2019
--- /dev/null
+++ b/resources/views/contacts/edit.edge
@@ -0,0 +1,59 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ APP_URL() + route('/contacts.update', {id: contact.id}) + '?_method=PUT' }}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">First Name</label>
+    <input class="input" type="text" name="first_name" placeholder="Arthur" value="{{ contact.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="{{ contact.last_name }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('last_name'), hasErrorFor('last_name')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Organization</label>
+    <div class="select">
+      <select name="organization_id">
+        <option>Select dropdown</option>
+        @each(organization in organizations)
+          <option value="{{organization.id}}" {{organization.id == contact.organization_id ? 'selected' : ''}}>{{organization.name}}</option>
+        @endeach
+      </select>
+    </div>
+  </div>
+
+  <div class="field">
+    <label class="label">Contact Email</label>
+    <input class="input" type="text" name="email" placeholder="arthur.fish@oregon.gov" value="{{ contact.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="{{ contact.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">Modify</button>
+      </div>
+      <div class="control">
+      <button class="button is-text"><a href="{{ APP_URL() + 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
index ed70986..2b84bf3 100644
--- a/resources/views/contacts/index.edge
+++ b/resources/views/contacts/index.edge
@@ -35,6 +35,8 @@
           <form action="{{ APP_URL() + 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>
+            &nbsp;
+            <a href="{{APP_URL() + route('/contacts.edit', { id: contact.id })}}" class="button is-small is-warning">Edit</a>
           </form>
         </td>
       </tr>