From b112c9a5615e89819fcfb3679c628da106db30ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Fri, 29 Jan 2021 19:23:07 +0300 Subject: [PATCH] added api for dashboards --- app/Abstracts/Http/Controller.php | 2 +- app/Abstracts/Widget.php | 4 + .../Controllers/Api/Common/Dashboards.php | 121 ++++++++++++++++++ app/Http/Controllers/Api/Common/Items.php | 3 - app/Transformers/Common/Dashboard.php | 43 +++++++ app/Transformers/Common/Widget.php | 36 ++++++ routes/api.php | 5 + 7 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/Api/Common/Dashboards.php create mode 100644 app/Transformers/Common/Dashboard.php create mode 100644 app/Transformers/Common/Widget.php diff --git a/app/Abstracts/Http/Controller.php b/app/Abstracts/Http/Controller.php index 86cde56ee..ce3a6b3a4 100644 --- a/app/Abstracts/Http/Controller.php +++ b/app/Abstracts/Http/Controller.php @@ -56,7 +56,7 @@ abstract class Controller extends BaseController */ public function response($view, $data = []) { - $class_name = str_replace('Controllers', 'Responses', (new \ReflectionClass($this))->getName()); + $class_name = str_replace('Controllers', 'Responses', get_class($this)); if (class_exists($class_name)) { $response = new $class_name($view, $data); diff --git a/app/Abstracts/Widget.php b/app/Abstracts/Widget.php index 3484da0f2..82bbefd6b 100644 --- a/app/Abstracts/Widget.php +++ b/app/Abstracts/Widget.php @@ -43,6 +43,10 @@ abstract class Widget public function view($name, $data = []) { + if (request()->isApi()) { + return $data; + } + return view($name, array_merge(['class' => $this], (array) $data)); } diff --git a/app/Http/Controllers/Api/Common/Dashboards.php b/app/Http/Controllers/Api/Common/Dashboards.php new file mode 100644 index 000000000..e2552d34f --- /dev/null +++ b/app/Http/Controllers/Api/Common/Dashboards.php @@ -0,0 +1,121 @@ +collect(); + + return $this->response->paginator($dashboards, new Transformer()); + } + + /** + * Display the specified resource. + * + * @param int|string $id + * @return \Dingo\Api\Http\Response + */ + public function show($id) + { + $dashboard = Dashboard::with('widgets')->find($id); + + return $this->response->item($dashboard, new Transformer()); + } + + /** + * Store a newly created resource in storage. + * + * @param $request + * @return \Dingo\Api\Http\Response + */ + public function store(Request $request) + { + $dashboard = $this->dispatch(new CreateDashboard($request)); + + return $this->response->created(route('api.dashboards.show', $dashboard->id)); + } + + /** + * Update the specified resource in storage. + * + * @param $dashboard + * @param $request + * @return \Dingo\Api\Http\Response + */ + public function update(Dashboard $dashboard, Request $request) + { + try { + $dashboard = $this->dispatch(new UpdateDashboard($dashboard, $request)); + + return $this->item($dashboard->fresh(), new Transformer()); + } catch(\Exception $e) { + $this->response->errorUnauthorized($e->getMessage()); + } + } + + /** + * Enable the specified resource in storage. + * + * @param Dashboard $dashboard + * @return \Dingo\Api\Http\Response + */ + public function enable(Dashboard $dashboard) + { + try { + $dashboard = $this->dispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 1]))); + + return $this->item($dashboard->fresh(), new Transformer()); + } catch(\Exception $e) { + $this->response->errorUnauthorized($e->getMessage()); + } + } + + /** + * Disable the specified resource in storage. + * + * @param Dashboard $dashboard + * @return \Dingo\Api\Http\Response + */ + public function disable(Dashboard $dashboard) + { + try { + $dashboard = $this->dispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 0]))); + + return $this->item($dashboard->fresh(), new Transformer()); + } catch(\Exception $e) { + $this->response->errorUnauthorized($e->getMessage()); + } + } + + /** + * Remove the specified resource from storage. + * + * @param Dashboard $dashboard + * @return \Dingo\Api\Http\Response + */ + public function destroy(Dashboard $dashboard) + { + try { + $this->dispatch(new DeleteDashboard($dashboard)); + + return $this->response->noContent(); + } catch(\Exception $e) { + $this->response->errorUnauthorized($e->getMessage()); + } + } +} diff --git a/app/Http/Controllers/Api/Common/Items.php b/app/Http/Controllers/Api/Common/Items.php index 138dd34e8..5910ac2b1 100644 --- a/app/Http/Controllers/Api/Common/Items.php +++ b/app/Http/Controllers/Api/Common/Items.php @@ -9,12 +9,9 @@ use App\Jobs\Common\DeleteItem; use App\Jobs\Common\UpdateItem; use App\Models\Common\Item; use App\Transformers\Common\Item as Transformer; -use App\Traits\Uploads; class Items extends ApiController { - use Uploads; - /** * Display a listing of the resource. * diff --git a/app/Transformers/Common/Dashboard.php b/app/Transformers/Common/Dashboard.php new file mode 100644 index 000000000..b73b22c9a --- /dev/null +++ b/app/Transformers/Common/Dashboard.php @@ -0,0 +1,43 @@ + $model->id, + 'company_id' => $model->company_id, + 'name' => $model->name, + 'enabled' => $model->enabled, + 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', + 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', + ]; + } + + /** + * @param Model $model + * @return mixed + */ + public function includeWidgets(Model $model) + { + if (!$model->widgets) { + return $this->null(); + } + + return $this->collection($model->widgets, new Widget()); + } +} diff --git a/app/Transformers/Common/Widget.php b/app/Transformers/Common/Widget.php new file mode 100644 index 000000000..9042be290 --- /dev/null +++ b/app/Transformers/Common/Widget.php @@ -0,0 +1,36 @@ + $model->dashboard_id]); + + return [ + 'id' => $model->id, + 'company_id' => $model->company_id, + 'dashboard_id' => $model->dashboard_id, + 'class' => $model->class, + 'name' => $model->name, + 'sort' => $model->sort, + 'settings' => $model->settings, + 'data' => show_widget($model->class), + 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', + 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', + ]; + } +} diff --git a/routes/api.php b/routes/api.php index 5a4b97977..18b567d60 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,6 +18,11 @@ $api->version('v3', ['middleware' => ['api']], function($api) { $api->get('companies/{company}/disable', 'Common\Companies@disable')->name('companies.disable'); $api->resource('companies', 'Common\Companies'); + // Dashboards + $api->get('dashboards/{dashboard}/enable', 'Common\Dashboards@enable')->name('dashboards.enable'); + $api->get('dashboards/{dashboard}/disable', 'Common\Dashboards@disable')->name('dashboards.disable'); + $api->resource('dashboards', 'Common\Dashboards'); + // Items $api->get('items/{item}/enable', 'Common\Items@enable')->name('items.enable'); $api->get('items/{item}/disable', 'Common\Items@disable')->name('items.disable');