2021-01-29 19:23:07 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Http\ApiController;
|
|
|
|
use App\Http\Requests\Common\Dashboard as Request;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Http\Resources\Common\Dashboard as Resource;
|
2021-01-29 19:23:07 +03:00
|
|
|
use App\Jobs\Common\CreateDashboard;
|
|
|
|
use App\Jobs\Common\DeleteDashboard;
|
|
|
|
use App\Jobs\Common\UpdateDashboard;
|
|
|
|
use App\Models\Common\Dashboard;
|
2021-01-29 21:38:10 +03:00
|
|
|
use App\Traits\Users;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Http\Response;
|
2021-01-29 19:23:07 +03:00
|
|
|
|
|
|
|
class Dashboards extends ApiController
|
|
|
|
{
|
2021-01-29 21:38:10 +03:00
|
|
|
use Users;
|
|
|
|
|
2021-01-29 19:23:07 +03:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2021-01-29 21:38:10 +03:00
|
|
|
$dashboards = user()->dashboards()->with('widgets')->collect();
|
2021-01-29 19:23:07 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return Resource::collection($dashboards);
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int|string $id
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2021-01-29 21:38:10 +03:00
|
|
|
try {
|
|
|
|
$dashboard = Dashboard::with('widgets')->find($id);
|
|
|
|
|
2022-07-20 10:54:13 +03:00
|
|
|
if (! $dashboard instanceof Dashboard) {
|
|
|
|
return $this->errorInternal('No query results for model [' . Dashboard::class . '] ' . $id);
|
|
|
|
}
|
|
|
|
|
2021-01-29 21:38:10 +03:00
|
|
|
// Check if user can access dashboard
|
|
|
|
$this->canAccess($dashboard);
|
2021-01-29 19:23:07 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return new Resource($dashboard);
|
2021-01-29 21:38:10 +03:00
|
|
|
} catch (\Exception $e) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($e->getMessage());
|
2021-01-29 21:38:10 +03:00
|
|
|
}
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param $request
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$dashboard = $this->dispatch(new CreateDashboard($request));
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return $this->created(route('api.dashboards.show', $dashboard->id), new Resource($dashboard));
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param $dashboard
|
|
|
|
* @param $request
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function update(Dashboard $dashboard, Request $request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dashboard = $this->dispatch(new UpdateDashboard($dashboard, $request));
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return new Resource($dashboard->fresh());
|
2021-01-29 19:23:07 +03:00
|
|
|
} catch(\Exception $e) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($e->getMessage());
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function enable(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dashboard = $this->dispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 1])));
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return new Resource($dashboard->fresh());
|
2021-01-29 19:23:07 +03:00
|
|
|
} catch(\Exception $e) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($e->getMessage());
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function disable(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dashboard = $this->dispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 0])));
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return new Resource($dashboard->fresh());
|
2021-01-29 19:23:07 +03:00
|
|
|
} catch(\Exception $e) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($e->getMessage());
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\Response
|
2021-01-29 19:23:07 +03:00
|
|
|
*/
|
|
|
|
public function destroy(Dashboard $dashboard)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->dispatch(new DeleteDashboard($dashboard));
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return $this->noContent();
|
2021-01-29 19:23:07 +03:00
|
|
|
} catch(\Exception $e) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($e->getMessage());
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 21:38:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check user dashboard assignment
|
|
|
|
*
|
|
|
|
* @param Dashboard $dashboard
|
|
|
|
*
|
2022-06-01 10:15:55 +03:00
|
|
|
* @return \Illuminate\Http\Response
|
2021-01-29 21:38:10 +03:00
|
|
|
*/
|
|
|
|
public function canAccess($dashboard)
|
|
|
|
{
|
|
|
|
if (!empty($dashboard) && $this->isUserDashboard($dashboard->id)) {
|
|
|
|
return new Response('');
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = trans('dashboards.error.not_user_dashboard');
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->errorUnauthorized($message);
|
2021-01-29 21:38:10 +03:00
|
|
|
}
|
2021-01-29 19:23:07 +03:00
|
|
|
}
|