added api for dashboards
This commit is contained in:
parent
1029f1492f
commit
b112c9a561
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
121
app/Http/Controllers/Api/Common/Dashboards.php
Normal file
121
app/Http/Controllers/Api/Common/Dashboards.php
Normal file
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Common;
|
||||
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Common\Dashboard as Request;
|
||||
use App\Jobs\Common\CreateDashboard;
|
||||
use App\Jobs\Common\DeleteDashboard;
|
||||
use App\Jobs\Common\UpdateDashboard;
|
||||
use App\Models\Common\Dashboard;
|
||||
use App\Transformers\Common\Dashboard as Transformer;
|
||||
|
||||
class Dashboards extends ApiController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$dashboards = Dashboard::with('widgets')->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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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.
|
||||
*
|
||||
|
43
app/Transformers/Common/Dashboard.php
Normal file
43
app/Transformers/Common/Dashboard.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Transformers\Common;
|
||||
|
||||
use App\Models\Common\Dashboard as Model;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class Dashboard extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = ['widgets'];
|
||||
|
||||
/**
|
||||
* @param Model $model
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Model $model)
|
||||
{
|
||||
return [
|
||||
'id' => $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());
|
||||
}
|
||||
}
|
36
app/Transformers/Common/Widget.php
Normal file
36
app/Transformers/Common/Widget.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Transformers\Common;
|
||||
|
||||
use App\Models\Common\Widget as Model;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class Widget extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = [];
|
||||
|
||||
/**
|
||||
* @param Model $model
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Model $model)
|
||||
{
|
||||
session(['dashboard_id' => $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() : '',
|
||||
];
|
||||
}
|
||||
}
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user