DataInventory

chore: commit before pub to dev

3/5/2020 8:40:17 AM

Details

diff --git a/app/Controllers/Http/ElementController.js b/app/Controllers/Http/ElementController.js
index beb2c94..528040e 100644
--- a/app/Controllers/Http/ElementController.js
+++ b/app/Controllers/Http/ElementController.js
@@ -5,7 +5,7 @@
 /** @typedef {import('@adonisjs/framework/src/View')} View */
 const Element = use('App/Models/Element')
 const Theme = use('App/Models/Theme')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 
 /**
  * Resourceful controller for interacting with elements
@@ -51,10 +51,10 @@ class ElementController {
    * @param {Request} ctx.request
    * @param {Response} ctx.response
    */
-  async store ({ request, response }) {
+  async store ({ session, request, response }) {
     const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])
 
-    const validation = await validate(request.all(), {
+    const validation = await validateAll(request.all(), {
       name: 'required|max:255',
       theme_id: 'required',
       priority: 'required',
@@ -98,6 +98,12 @@ class ElementController {
    * @param {View} ctx.view
    */
   async edit ({ params, request, response, view }) {
+    const element = await Element.findOrFail(params.id)
+    const themes = await Theme.all()
+    return view.render('elements.edit', {
+      element: element.toJSON(),
+      themes: themes.toJSON()
+    })
   }
 
   /**
@@ -108,7 +114,38 @@ class ElementController {
    * @param {Request} ctx.request
    * @param {Response} ctx.response
    */
-  async update ({ params, request, response }) {
+  async update ({ session, params, request, response }) {
+    const data = request.only(['name', 'theme_id', 'priority', 'refresh_rate'])
+
+    const validation = await validateAll(data, {
+      name: 'required',
+      theme_id: 'required',
+      priority: 'required',
+      refresh_rate: 'required'
+    })
+
+    /**
+     * If validation fails, early returns with validation message.
+     */
+    if (validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+
+      return response.redirect('back')
+    }
+
+    /**
+     * Finding the post and updating fields on it
+     * before saving it to the database.
+     *
+     * ref: http://adonisjs.com/docs/4.1/lucid#_inserts_updates
+     */
+    const element = await Element.findOrFail(params.id)
+    element.merge(data)
+    await element.save()
+
+    return response.redirect('/elements')
   }
 
   /**
diff --git a/app/Controllers/Http/ThemeController.js b/app/Controllers/Http/ThemeController.js
index ed8f288..45993c3 100644
--- a/app/Controllers/Http/ThemeController.js
+++ b/app/Controllers/Http/ThemeController.js
@@ -5,7 +5,7 @@
 /** @typedef {import('@adonisjs/framework/src/View')} View */
 
 const Theme = use('App/Models/Theme')
-const { validate } = use('Validator')
+const { validate, validateAll } = use('Validator')
 
 /**
  * Resourceful controller for interacting with themes
@@ -91,7 +91,11 @@ class ThemeController {
    * @param {Response} ctx.response
    * @param {View} ctx.view
    */
-  async edit ({ params, request, response, view }) {
+  async edit ({ params, view }) {
+    const theme = await Theme.findOrFail(params.id)
+    return view.render('themes.edit', {
+      theme: theme.toJSON()
+    })
   }
 
   /**
@@ -102,7 +106,35 @@ class ThemeController {
    * @param {Request} ctx.request
    * @param {Response} ctx.response
    */
-  async update ({ params, request, response }) {
+  async update ({ params, request, response, session }) {
+    const data = request.only(['name'])
+
+    const validation = await validateAll(data, {
+      name: 'required'
+    })
+
+    /**
+     * If validation fails, early returns with validation message.
+     */
+    if (validation.fails()) {
+      session
+        .withErrors(validation.messages())
+        .flashAll()
+
+      return response.redirect('back')
+    }
+
+    /**
+     * Finding the post and updating fields on it
+     * before saving it to the database.
+     *
+     * ref: http://adonisjs.com/docs/4.1/lucid#_inserts_updates
+     */
+    const theme = await Theme.findOrFail(params.id)
+    theme.merge(data)
+    await theme.save()
+
+    return response.redirect('/themes')
   }
 
   /**
diff --git a/resources/views/elements/edit.edge b/resources/views/elements/edit.edge
new file mode 100644
index 0000000..c2cd8bc
--- /dev/null
+++ b/resources/views/elements/edit.edge
@@ -0,0 +1,45 @@
+@layout('layouts.main')
+
+@section('content')
+<form method="POST" action="{{ route('/elements.update', {id: element.id}) + '?_method=PUT'}}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Element Name</label>
+    <input class="input" type="text" name="name" placeholder="Zoning" value="{{ element.name }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('name'), hasErrorFor('name')) }}
+  </div>
+  <div class="field">
+    <label class="label">Theme</label>
+    <div class="select">
+      <select name="theme_id">
+        <option>Select dropdown</option>
+        @each(theme in themes)
+          <option value="{{theme.id}}" {{theme.id === element.theme_id ? 'selected' : ''}}>{{theme.name}}</option>
+        @endeach
+      </select>
+    </div>
+  </div>
+
+  <div class="field">
+    <label class="label">Priority</label>
+    <input class="input" type="text" name="priority" placeholder="1 (lowest) - 3 (highest)" value="{{ element.priority }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('priority'), hasErrorFor('priority')) }}
+  </div>
+
+  <div class="field">
+    <label class="label">Refresh Rate (days)</label>
+    <input class="input" type="text" name="refresh_rate" placeholder="90" value="{{ element.refresh_rate }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('refresh_rate'), hasErrorFor('refresh_rate')) }}
+  </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('/elements.index') }}">Cancel</a></button>
+      </div>
+    </div>
+</form>
+
+@endsection 
\ No newline at end of file
diff --git a/resources/views/elements/index.edge b/resources/views/elements/index.edge
index 56a5654..ae2a120 100644
--- a/resources/views/elements/index.edge
+++ b/resources/views/elements/index.edge
@@ -19,7 +19,7 @@
         <th>Theme</th>
         <th>Priority</th>
         <th>Refresh Rate</th>
-        <th>Action</th>
+        <th>Actions</th>
       </tr>
     </thead>
     <tbody class="tbody">
@@ -32,7 +32,9 @@
         <td>
           <form action="{{ route('/elements.destroy', { id: element.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 element?');">Delete</button>
+            <button type="submit" name="button" class="is-small button is-danger is-pulled-left" onclick="return confirm('Are you sure you want to delete this element?');">Delete</button>
+            &nbsp;
+            <a href="{{route('/elements.edit', { id: element.id })}}" class="button is-small is-warning">Edit</a>
           </form>
         </td>
       </tr>
diff --git a/resources/views/themes/edit.edge b/resources/views/themes/edit.edge
new file mode 100644
index 0000000..bbc7bcc
--- /dev/null
+++ b/resources/views/themes/edit.edge
@@ -0,0 +1,21 @@
+@layout('layouts.main')
+
+@section('content')
+<form method="POST" action="{{ route('/themes.update', {id: theme.id}) + '?_method=PUT'}}">
+  {{ csrfField() }}
+  <div class="field">
+    <label class="label">Theme Name</label>
+    <input class="input" type="text" name="name" placeholder="Admin boundaries" value="{{ theme.name }}" />
+    {{ elIf('<span class="has-text-danger">$self</span>', getErrorFor('name'), hasErrorFor('name')) }}
+  </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="{{ route('/themes.index') }}">Cancel</a></button>
+      </div>
+    </div>
+</form>
+@endsection
\ No newline at end of file
diff --git a/resources/views/themes/index.edge b/resources/views/themes/index.edge
index 7d97b00..a8c7b75 100644
--- a/resources/views/themes/index.edge
+++ b/resources/views/themes/index.edge
@@ -16,7 +16,7 @@
     <thead class="thead">
       <tr>
         <th>Theme Name</th>
-        <th>Action</th>
+        <th>Actions</th>
       </tr>
     </thead>
     <tbody class="tbody">
@@ -26,8 +26,10 @@
         <td>
           <form action="{{ route('/themes.destroy', { id: theme.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>
+            <button type="submit" name="button" class="is-small button is-danger is-pulled-left" onclick="return confirm('Are you sure you want to delete this theme?');">Delete</button>
           </form>
+          &nbsp;
+          <a href="{{route('/themes.edit', { id: theme.id })}}" class="button is-small is-warning">Edit</a>
         </td>
       </tr>
       @endeach