Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/dashboard' into feature/gestioneGateways
Browse files Browse the repository at this point in the history
  • Loading branch information
BroHPotato committed Mar 30, 2020
2 parents fbf2720 + ce10f69 commit c6293b7
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
24 changes: 23 additions & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Http\Controllers;

use App\Providers\DeviceServiceProvider;
use App\Providers\EntityServiceProvider;
use App\Providers\UserServiceProvider;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
Expand All @@ -12,7 +15,26 @@ public function __construct()
}
public function index()
{
$userProvider = new UserServiceProvider();
$entityProvider = new EntityServiceProvider();
$deviceProvider = new DeviceServiceProvider();

$user = Auth::user();
return view('dashboard.index', compact('user'));
$users = $userProvider->findAll();//utenti registrati
$entities = $entityProvider->findAll();//enti presenti
$devices = $deviceProvider->findAll();//dispositivi registrati

$devicesEntity = $deviceProvider->findAllFromEntity($entityProvider->findFromUser($user->getAuthIdentifier()));
$usersEntity = $userProvider->findAllFromEntity($entityProvider->findFromUser($user->getAuthIdentifier()));

$usersActive = array_filter($users, function ($u) {
return !$u->deleted;
});
$usersActiveEntity = array_filter($usersEntity, function ($u) {
return !$u->deleted;
});
return view('dashboard.index', compact([
'user', 'users', 'entities', 'devices', 'devicesEntity', '$usersEntity', 'usersActive', 'usersActiveEntity'
]));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function index()
*/
public function show($device)
{
//$device = $this->provider->retrieveById($device);
//$device = $this->provider->find($device);
///FAKER
$user = new Device();
$arr = array_combine(
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function getSensors()
public function getEntity()
{
$provider = new EntityServiceProvider();
return $provider->retrieveByDevice($this->deviceId);
return $provider->findFromDevice($this->deviceId);
}
}
24 changes: 23 additions & 1 deletion app/Providers/DeviceServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
* @param mixed $identifier
* @return Device
*/
public function retrieveById($identifier)
public function find($identifier)
{
try {
$response = json_decode($this->request->get('device/' . $identifier, [
Expand Down Expand Up @@ -77,4 +77,26 @@ private function isExpired(RequestException $e)
return redirect('login');
}
}

public function findAllFromEntity($entity)
{
try {
$response = json_decode($this->request->get('devices', [
'headers' => [
'Authorization' => 'Bearer ' . session()->get('token')
],
'query' => 'entityId=' . $entity
])->getBody());
$devices = [];
foreach ($response as $d) {
$device = new Device();
$device->fill((array)$d);
$devices[] = $device;
}
return $devices;
} catch (RequestException $e) {
$this->isExpired($e);
abort($e->getCode(), $e->getResponse()->getReasonPhrase());
}
}
}
20 changes: 20 additions & 0 deletions app/Providers/EntityServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ public function retrieveByDevice($deviceId)
}
}

public function findFromUser($userId)
{
try {
$response = json_decode($this->request->get('entities', [
'headers' => [
'Authorization' => 'Bearer ' . session()->get('token')
],
'query' => 'userId=' . $userId
])->getBody());
$entity = new Entity();
$entity->fill((array)$response);
return $entity;
} catch (RequestException $e) {
$this->isExpired($e);
abort($e->getCode(), $e->getResponse()->getReasonPhrase());
return null;
}
}


private function isExpired(RequestException $e)
{
if ($e->getCode() == 419/*fai il controllo del token*/) {
Expand Down
23 changes: 23 additions & 0 deletions app/Providers/UserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function retrieveById($identifier)
'Authorization' => 'Bearer ' . session()->get('token')
]
])->getBody());

$user = new User();
$user->fill((array)$response);
return $user;
Expand Down Expand Up @@ -117,6 +118,28 @@ public function findAll()
}
}

public function findAllFromEntity($entityId)
{
try {
$response = json_decode($this->request->get('users', [
'headers' => [
'Authorization' => 'Bearer ' . session()->get('token')
],
'query' => 'entityId=' . $entityId
])->getBody());
$users = [];
foreach ($response as $u) {
$user = new User();
$user->fill((array)$u);
$users[] = $user;
}
return $users;
} catch (RequestException $e) {
$this->isExpired($e);
abort($e->getCode(), $e->getResponse()->getReasonPhrase());
}
}

private function retriveByCode(Client $request, $credentials)
{
$response = json_decode($request->post('auth/tfa', [
Expand Down
18 changes: 9 additions & 9 deletions resources/views/dashboard/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Utenti attivi</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">10</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($usersActive)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-users fa-2x text-gray-500"></i>
Expand All @@ -29,7 +29,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">utenti attivi nel tuo ente</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">8</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($usersActiveEntity)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-user-friends fa-2x text-gray-500"></i>
Expand All @@ -44,7 +44,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-success text-uppercase mb-1">utenti registrati</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">15</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($users)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-user-plus fa-2x text-gray-500"></i>
Expand All @@ -59,7 +59,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-success text-uppercase mb-1">utenti registrati nel tuo ente</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">12</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($usersEntity)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-user-tag fa-2x text-gray-500"></i>
Expand All @@ -76,7 +76,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-danger text-uppercase mb-1">dispositivi registrati</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">5</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($devices)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-shapes fa-2x text-gray-500"></i>
Expand All @@ -91,7 +91,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-danger text-uppercase mb-1">dispositivi del tuo ente</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">3</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($devicesEntity)}}</div>
</div>
<div class="col-auto">
<i class="fas fa-microchip fa-2x text-gray-500"></i>
Expand All @@ -106,7 +106,7 @@
<div class="row no-gutters align-items-center">
<div class="col mr-2">
<div class="text-xs font-weight-bold text-warning text-uppercase mb-1">enti presenti</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">2</div>
<div class="h5 mb-0 font-weight-bold text-gray-800">{{count($entities)}}</div>
</div>
<div class="col-auto">
<i class="far fa-building fa-2x text-gray-500"></i>
Expand All @@ -126,8 +126,8 @@
</h6>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Nome e Cognome:</strong> {{Auth::user()->name}} {{Auth::user()->surname}}</li>
<li class="list-group-item"><strong>Indirizzo email:</strong> {{Auth::user()->email}}</li>
<li class="list-group-item"><strong>Nome e Cognome:</strong> {{$user->name}} {{$user->surname}}</li>
<li class="list-group-item"><strong>Indirizzo email:</strong> {{$user->email}}</li>
<li class="list-group-item"><strong>Ente di appartenenza:</strong> NOME ENTE</li>
<li class="list-group-item"><strong>Indirizzo IP:</strong> &nbsp; <code>{{ $_SERVER['REMOTE_ADDR'] }}</code></li>
</ul>
Expand Down

0 comments on commit c6293b7

Please sign in to comment.