DataInventory

feat: CRUD organizations

3/13/2020 9:27:16 AM

Details

diff --git a/app/Controllers/Http/OrganizationController.js b/app/Controllers/Http/OrganizationController.js
index 1dfd0ae..f90f323 100644
--- a/app/Controllers/Http/OrganizationController.js
+++ b/app/Controllers/Http/OrganizationController.js
@@ -4,8 +4,9 @@
 /** @typedef {import('@adonisjs/framework/src/Response')} Response */
 /** @typedef {import('@adonisjs/framework/src/View')} View */
 const Organization = use('App/Models/Organization')
+const OrganizationType = use('App/Models/OrganizationType')
 const Type = use('App/Models/OrganizationType')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 const Env = use('Env')
 
 /**
@@ -23,7 +24,7 @@ class OrganizationController {
    */
   async index ({ request, response, view }) {
     const organizations = await Organization.query().with('type').orderBy('name', 'asc').fetch()
-  
+
     return view.render('organizations.index', {
       organizations: organizations.toJSON()
     })
@@ -102,6 +103,12 @@ class OrganizationController {
    * @param {View} ctx.view
    */
   async edit ({ params, request, response, view }) {
+    const organization = await Organization.findOrFail(params.id)
+    const organization_types = await OrganizationType.all()
+    return view.render('organizations.edit', {
+      organization: organization.toJSON(),
+      types: organization_types.toJSON()
+    })
   }
 
   /**
@@ -112,7 +119,29 @@ class OrganizationController {
    * @param {Request} ctx.request
    * @param {Response} ctx.response
    */
-  async update ({ params, request, response }) {
+  async update ({ params, request, response, session }) {
+    const data = request.only(['name', 'email', 'phone', 'organization_type_id'])
+
+    const validation = await validateAll(data, {
+      name: 'required',
+      email: 'required',
+      phone: 'required',
+      organization_type_id: 'required'
+    })
+
+    if (validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+
+      return response.redirect('back')
+    }
+
+    const organization = await Organization.findOrFail(params.id)
+    organization.merge(data)
+    await organization.save()
+
+    return response.redirect(Env.get('APP_URL') + '/organizations')
   }
 
   /**
diff --git a/resources/views/organization_types/edit.edge b/resources/views/organization_types/edit.edge
index 534aa3a..a6cc2d1 100644
--- a/resources/views/organization_types/edit.edge
+++ b/resources/views/organization_types/edit.edge
@@ -11,7 +11,7 @@
 
   <div class="field is-grouped">
       <div class="control">
-        <button class="button is-link ss-submit" type="submit">Submit</button>
+        <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('/organization_types.index') }}">Cancel</a></button>
diff --git a/resources/views/organizations/edit.edge b/resources/views/organizations/edit.edge
new file mode 100644
index 0000000..59f2cd9
--- /dev/null
+++ b/resources/views/organizations/edit.edge
@@ -0,0 +1,53 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ APP_URL() + route('/organizations.update', {id: organization.id}) + '?_method=PUT'}}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Organization Name</label>
+    <input class="input" type="text" name="name" placeholder="State of Oregon" value="{{ organization.name }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('name'), hasErrorFor('name')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Organization Type</label>
+    <div class="select">
+      <select name="organization_type_id">
+        <option>Select dropdown</option>
+        @each(type in types)
+          <option value="{{type.id}}" {{type.id === organization.organization_type_id ? 'selected' : ''}}>{{type.type}}</option>
+        @endeach
+      </select>
+    </div>
+  </div>
+
+  <div class="field">
+    <label class="label">Organization Email</label>
+    <input class="input" type="text" name="email" placeholder="State of Oregon" value="{{ organization.email }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('email'), hasErrorFor('email')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Organization Phone</label>
+    <input class="input" type="text" name="phone" placeholder="State of Oregon" value="{{ organization.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('/themes.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/organizations/index.edge b/resources/views/organizations/index.edge
index a5248dc..960b0ba 100644
--- a/resources/views/organizations/index.edge
+++ b/resources/views/organizations/index.edge
@@ -33,6 +33,8 @@
           <form action="{{ APP_URL() + route('/organizations.destroy', { id: organization.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('/organizations.edit', { id: organization.id })}}" class="button is-small is-warning">Edit</a>
           </form>
         </td>
       </tr>