Prechádzať zdrojové kódy

Implements credential edit and live-update.

Christopher Leggett 5 rokov pred
rodič
commit
20724da7f8

+ 39 - 0
app/Events/CredentialUpdated.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Events;
+
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use App\Credential;
+
+class CredentialUpdated implements ShouldBroadcast
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $data;
+
+    /**
+     * Create a new event instance.
+     *
+     * @return void
+     */
+    public function __construct($credential)
+    {
+        $this->data = $credential->toJson();
+    }
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new Channel('credentials');
+    }
+}

+ 69 - 0
app/Http/Controllers/Api/CredentialsController.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Credential;
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+
+class CredentialsController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Credential  $credential
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Credential $credential)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Credential  $credential
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, Credential $credential)
+    {
+        $credential->creduser = $request->input('creduser');
+        $credential->credpass = $request->input('credpass');
+        $credential->save();
+        event(new \App\Events\CredentialUpdated($credential));
+        return response()->json($credential, 200);
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Credential  $credential
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Credential $credential)
+    {
+        //
+    }
+}

+ 6 - 0
public/js/app.js

@@ -2111,6 +2111,12 @@ __webpack_require__.r(__webpack_exports__);
     },
     getRandomPassword: function getRandomPassword() {
       this.data.credpass = this.rndStr(16);
+    },
+    updateCredential: function updateCredential() {
+      axios.put('/api/credentials/' + this.data.credid, this.data).then(function (response) {})["catch"](function (error) {
+        console.error(error);
+      });
+      $('#credential' + this.data.credid + 'editModal').modal('hide');
     }
   }
 });

+ 4 - 0
resources/js/components/credential.vue

@@ -75,6 +75,10 @@ export default {
         },
         getRandomPassword() {
             this.data.credpass = this.rndStr(16);
+        },
+        updateCredential() {
+            axios.put('/api/credentials/' + this.data.credid, this.data).then(response => {}).catch(error =>{console.error(error)});
+            $('#credential'+this.data.credid+'editModal').modal('hide');
         }
     }
 }

+ 2 - 0
routes/api.php

@@ -26,4 +26,6 @@ Route::middleware('auth:api')->group( function() {
     Route::get('/stores', 'Api\StoresController@index');
     Route::get('/stores/{store}', 'Api\StoresController@show');
 
+    Route::put('/credentials/{credential}', 'Api\CredentialsController@update');
+
 });