DataInventory

chore: add organizations

1/15/2020 11:08:27 AM

Details

diff --git a/app/Controllers/Http/OrganizationController.js b/app/Controllers/Http/OrganizationController.js
new file mode 100644
index 0000000..619ceb3
--- /dev/null
+++ b/app/Controllers/Http/OrganizationController.js
@@ -0,0 +1,129 @@
+'use strict'
+
+/** @typedef {import('@adonisjs/framework/src/Request')} Request */
+/** @typedef {import('@adonisjs/framework/src/Response')} Response */
+/** @typedef {import('@adonisjs/framework/src/View')} View */
+const Organization = use('App/Models/Organization')
+const { validate } = use('Validator')
+
+/**
+ * Resourceful controller for interacting with organizations
+ */
+class OrganizationController {
+  /**
+   * Show a list of all organizations.
+   * GET organizations
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   * @param {View} ctx.view
+   */
+  async index ({ request, response, view }) {
+    const organizations = await Organization.query().with('organization_types').orderBy('name', 'asc').fetch()
+  
+    return view.render('organizations.index', {
+      organizations: organizations.toJSON()
+    })
+  }
+
+  /**
+   * Render a form to be used for creating a new organization.
+   * GET organizations/create
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   * @param {View} ctx.view
+   */
+  async create ({ request, response, view }) {
+    return view.render('organizations.create')
+  }
+
+  /**
+   * Create/save a new organization.
+   * POST organizations
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   */
+  async store ({ request, response }) {
+
+    const data = request.only(['name', 'email', 'phone', 'organization_type_id'])
+
+    const validation = await validate(request.all(), {
+      name: 'required',
+      email: 'required',
+      phone: 'required',
+      organization_type_id: 'required',
+    }, {
+      'name.required': `Theme name is required`,
+      'email.required': `Email address is required`,
+      'phone.required': `Phone number is required`,
+      'organization_type_id.required': `Organization type is required`,
+    })
+
+    if(validation.fails()) {
+      session.withErrors(validation.messages()).flashAll()
+      return response.redirect('back')
+    }
+
+    await Organization.create(data)
+
+    return response.redirect('/organizations')
+
+  }
+
+  /**
+   * Display a single organization.
+   * GET organizations/: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 organization.
+   * GET organizations/:id/edit
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   * @param {View} ctx.view
+   */
+  async edit ({ params, request, response, view }) {
+  }
+
+  /**
+   * Update organization details.
+   * PUT or PATCH organizations/:id
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   */
+  async update ({ params, request, response }) {
+  }
+
+  /**
+   * Delete a organization with id.
+   * DELETE organizations/:id
+   *
+   * @param {object} ctx
+   * @param {Request} ctx.request
+   * @param {Response} ctx.response
+   */
+  async destroy ({ params, request, response }) {
+    const organization = await Organization.findOrFail(params.id)
+    await organization.delete()
+
+    return response.redirect('/organizations')
+  }
+}
+
+module.exports = OrganizationController
diff --git a/app/Models/Organization.js b/app/Models/Organization.js
new file mode 100644
index 0000000..8dc49a5
--- /dev/null
+++ b/app/Models/Organization.js
@@ -0,0 +1,9 @@
+'use strict'
+
+/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
+const Model = use('Model')
+
+class Organization extends Model {
+}
+
+module.exports = Organization
diff --git a/resources/views/inc/nav.edge b/resources/views/inc/nav.edge
index ba9d071..132ab28 100644
--- a/resources/views/inc/nav.edge
+++ b/resources/views/inc/nav.edge
@@ -17,6 +17,9 @@
     <li>
       <a href="{{ route('/contacts.index') }}">Contacts</a>
     </li>
+    <li>
+      <a href="{{ route('/organizations.index') }}">Organizations</a>
+    </li>
   </ul>
 </section>
 
diff --git a/resources/views/organizations/create.edge b/resources/views/organizations/create.edge
new file mode 100644
index 0000000..98a7f1f
--- /dev/null
+++ b/resources/views/organizations/create.edge
@@ -0,0 +1,41 @@
+@layout('layouts.main')
+
+@section('content')
+
+<form method="POST" action="{{ route('/organizations.store') }}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Organization Name</label>
+    <input class="input" type="text" name="name" placeholder="State of Oregon" value="{{ old('name', '') }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('name'), hasErrorFor('name')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Organization Email</label>
+    <input class="input" type="text" name="email" placeholder="State of Oregon" value="{{ old('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="{{ 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('/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
new file mode 100644
index 0000000..9a81cc5
--- /dev/null
+++ b/resources/views/organizations/index.edge
@@ -0,0 +1,44 @@
+@layout('layouts.main')
+
+@section('content')
+
+<section class="box is-full-width">
+  <h3 class="is-size-3">Organization Actions</h3>
+  <ul>
+    <li><a href="{{ route('/organizations/create') }}">Add Organization</a></li>
+  </ul>
+</section>
+
+<p>There are {{ organizations.length }} total organizations.</p>
+
+<section class="box">
+  <table class="table">
+    <thead class="thead">
+      <tr>
+        <th>Name</th>
+        <th>Email</th>
+        <th>Phone</th>
+        <th>Type</th>
+        <th>Action</th>
+      </tr>
+    </thead>
+    <tbody class="tbody">
+      @each(organization in organizations)
+      <tr>
+        <td>{{ organization.name }}</td>
+        <td>{{ organization.email }}</td>
+        <td>{{ organization.phone }}</td>
+        <td>{{ organization.type.type }}</td>
+        <td>
+          <form action="{{ 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>
+          </form>
+        </td>
+      </tr>
+      @endeach
+    </tbody>
+  </table>
+</section>
+
+@endsection
\ No newline at end of file

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

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