DataInventory

feat: CRUD steward

3/13/2020 2:11:41 PM

Details

diff --git a/app/Controllers/Http/StewardController.js b/app/Controllers/Http/StewardController.js
index cbe85bd..25e14a5 100644
--- a/app/Controllers/Http/StewardController.js
+++ b/app/Controllers/Http/StewardController.js
@@ -6,7 +6,7 @@
 const Steward = use('App/Models/Steward')
 const Contact = use('App/Models/Contact')
 const Element = use('App/Models/Element')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 const Env = use('Env')
 
 /**
@@ -95,6 +95,18 @@ class StewardController {
    * @param {View} ctx.view
    */
   async edit ({ params, request, response, view }) {
+    const fof = await Steward.findOrFail(params.id)
+    const steward = await Steward.query()
+      .with('elements')
+      .where('id', params.id)
+      .first()
+    const contacts = await Contact.all()
+    const elements = await Element.all()
+    return view.render('stewards.edit', {
+      steward: steward.toJSON(),
+      contacts: contacts.toJSON(),
+      elements: elements.toJSON(),
+    })
   }
 
   /**
@@ -106,6 +118,29 @@ class StewardController {
    * @param {Response} ctx.response
    */
   async update ({ params, request, response }) {
+    const data = request.only(['contact_id', 'element_id'])
+
+    const validation = await validateAll(data, {
+      contact_id: 'required',
+      element_id: 'required'
+    })
+
+    if (validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+      return response.redirect('back')
+    }
+    const steward = await Steward.findOrFail(params.id)
+
+    const prev_steward = await steward.elements().first()
+    await steward.elements().detach([prev_steward.id])
+    await steward.elements().attach([data['element_id']])
+    
+    steward.merge({contact_id: data['contact_id']})
+    await steward.save()
+
+    return response.redirect(Env.get('APP_URL') + '/stewards')
   }
 
   /**
diff --git a/resources/views/stewards/edit.edge b/resources/views/stewards/edit.edge
new file mode 100644
index 0000000..3393500
--- /dev/null
+++ b/resources/views/stewards/edit.edge
@@ -0,0 +1,47 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ APP_URL() + route('/stewards.update', {id: steward.id}) + '?_method=PUT' }}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Contact</label>
+    <div class="select">
+      <select name="contact_id">
+        <option>Select dropdown</option>
+        @each(contact in contacts)
+        <option value="{{contact.id}}" {{contact.id == steward.contact_id ? 'selected' : ''}}>{{contact.first_name}} {{contact.last_name}}</option>
+        @endeach
+      </select>
+    </div>
+  </div>
+
+  <div class="field">
+    <label class="label">Element</label>
+    <div class="select">
+      <select name="element_id">
+        <option>Select dropdown</option>
+        @each(element in elements)
+        <option value="{{element.id}}" {{element.id == steward.elements[0].id ? 'selected' : ''}}>{{element.name}}</option>
+        @endeach
+      </select>
+    </div>
+  </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('/stewards.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/stewards/index.edge b/resources/views/stewards/index.edge
index 3b7f153..25adba7 100644
--- a/resources/views/stewards/index.edge
+++ b/resources/views/stewards/index.edge
@@ -31,6 +31,8 @@
           <form action="{{ APP_URL() + route('/stewards.destroy', { id: steward.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 theme?');">Delete</button>
+            &nbsp;
+            <a href="{{APP_URL() + route('/stewards.edit', { id: steward.id })}}" class="button is-small is-warning">Edit</a>
           </form>
         </td>
       </tr>