DataInventory

feat: CRUD submission status

3/13/2020 9:59:04 AM

Details

diff --git a/app/Controllers/Http/StatusController.js b/app/Controllers/Http/StatusController.js
index feced1e..067f502 100644
--- a/app/Controllers/Http/StatusController.js
+++ b/app/Controllers/Http/StatusController.js
@@ -4,7 +4,7 @@
 /** @typedef {import('@adonisjs/framework/src/Response')} Response */
 /** @typedef {import('@adonisjs/framework/src/View')} View */
 const Status = use('App/Models/Status')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 const Env = use('Env')
 /**
  * Resourceful controller for interacting with statuses
@@ -91,6 +91,10 @@ class StatusController {
    * @param {View} ctx.view
    */
   async edit ({ params, request, response, view }) {
+    const status = await Status.findOrFail(params.id)
+    return view.render('status.edit', {
+      status: status.toJSON()
+    })
   }
 
   /**
@@ -102,6 +106,25 @@ class StatusController {
    * @param {Response} ctx.response
    */
   async update ({ params, request, response }) {
+    const data = request.only(['status'])
+
+    const validation = await validateAll(data, {
+      status: 'required'
+    })
+
+    if (validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+
+      return response.redirect('back')
+    }
+
+    const status = await Status.findOrFail(params.id)
+    status.merge(data)
+    await status.save()
+
+    return response.redirect(Env.get('APP_URL') + '/status')
   }
 
   /**
@@ -113,6 +136,10 @@ class StatusController {
    * @param {Response} ctx.response
    */
   async destroy ({ params, request, response }) {
+    const status = await Status.findOrFail(params.id)
+    await status.delete()
+
+    return response.redirect(Env.get('APP_URL') + '/status')
   }
 }
 
diff --git a/resources/views/status/edit.edge b/resources/views/status/edit.edge
new file mode 100644
index 0000000..da05f5b
--- /dev/null
+++ b/resources/views/status/edit.edge
@@ -0,0 +1,29 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ APP_URL() + route('/status.update', {id: status.id}) + '?_method=PUT' }}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Status Title</label>
+    <input class="input" type="text" name="status" placeholder="Admin boundaries" value="{{ status.status }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('status'), hasErrorFor('status')) }}
+  </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('/status.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/status/index.edge b/resources/views/status/index.edge
index ab469a9..4f1bf0f 100644
--- a/resources/views/status/index.edge
+++ b/resources/views/status/index.edge
@@ -27,6 +27,8 @@
           <form action="{{ APP_URL() + route('/status.destroy', { id: status.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 status?');">Delete</button>
+            &nbsp;
+            <a href="{{APP_URL() + route('/status.edit', { id: status.id })}}" class="button is-small is-warning">Edit</a>
           </form>
         </td>
       </tr>