Skip to content

Commit

Permalink
service 新增钩子: saving / saved / deleted / addRelations
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Feb 29, 2024
1 parent eba57e4 commit 466800b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ protected function response()
return Admin::response();
}

/**
* 根据传入的条件, 返回消息响应
*
* @param $flag
* @param $text
*
* @return JsonResponse|JsonResource
*/
protected function autoResponse($flag, $text = '')
{
if (!$text) {
Expand Down Expand Up @@ -114,8 +122,7 @@ public function create()
{
$this->isCreate = true;

$form = amis()
->Card()
$form = amis()->Card()
->className('base-form')
->header(['title' => __('admin.create')])
->toolbar([$this->backButton()])
Expand Down Expand Up @@ -163,8 +170,7 @@ public function show($id)
return $this->response()->success($this->service->getDetail($id));
}

$detail = amis()
->Card()
$detail = amis()->Card()
->className('base-form')
->header(['title' => __('admin.detail')])
->body($this->detail())
Expand Down Expand Up @@ -192,8 +198,7 @@ public function edit($id)
return $this->response()->success($this->service->getEditData($id));
}

$form = amis()
->Card()
$form = amis()->Card()
->className('base-form')
->header(['title' => __('admin.edit')])
->toolbar([$this->backButton()])
Expand Down
123 changes: 112 additions & 11 deletions src/Services/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Slowlyo\OwlAdmin\Services;

use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Slowlyo\OwlAdmin\Renderers\Page;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
Expand All @@ -15,10 +16,17 @@ abstract class AdminService
{
use ErrorTrait;

protected $tableColumn;
protected static $tableColumn;

protected string $modelName;

protected Request $request;

public function __construct()
{
$this->request = request();
}

public static function make(): static
{
return new static;
Expand All @@ -39,12 +47,12 @@ public function primaryKey()

public function getTableColumns()
{
if (!$this->tableColumn) {
$this->tableColumn = Schema::connection($this->getModel()->getConnectionName())
if (!self::$tableColumn) {
self::$tableColumn = Schema::connection($this->getModel()->getConnectionName())
->getColumnListing($this->getModel()->getTable());
}

return $this->tableColumn;
return self::$tableColumn;
}

public function query()
Expand All @@ -61,7 +69,11 @@ public function query()
*/
public function getDetail($id)
{
return $this->query()->find($id);
$query = $this->query();

$this->addRelations($query, 'detail');

return $query->find($id);
}

/**
Expand All @@ -79,7 +91,11 @@ public function getEditData($id)
->filter(fn($item) => $item !== null)
->toArray();

return $this->query()->find($id)->makeHidden($hidden);
$query = $this->query();

$this->addRelations($query, 'edit');

return $query->find($id)->makeHidden($hidden);
}

/**
Expand All @@ -91,15 +107,36 @@ public function listQuery()
{
$query = $this->query();

// 处理排序
$this->sortable($query);

// 自动加载 TableColumn 内的关联关系
$this->loadRelations($query);

// 处理查询
$this->searchable($query);

// 追加关联关系
$this->addRelations($query);

return $query;
}

/**
* 添加关联关系
*
* 预留钩子, 方便处理只需要添加 [关联] 的情况
*
* @param $query
* @param string $scene 场景: list, detail, edit
*
* @return void
*/
public function addRelations($query, string $scene = 'list')
{

}

/**
* 根据 tableColumn 定义的列, 自动加载关联关系
*
Expand Down Expand Up @@ -141,7 +178,7 @@ public function loadRelations($query)
*
* @return void
*/
protected function sortable($query)
public function sortable($query)
{
if (request()->orderBy && request()->orderDir) {
$query->orderBy(request()->orderBy, request()->orderDir ?? 'asc');
Expand All @@ -157,7 +194,7 @@ protected function sortable($query)
*
* @return void
*/
protected function searchable($query)
public function searchable($query)
{
collect(array_keys(request()->query()))
->intersect($this->getTableColumns())
Expand Down Expand Up @@ -214,6 +251,8 @@ public function list()
*/
public function update($primaryKey, $data)
{
$this->saving($data, $primaryKey);

$columns = $this->getTableColumns();
$model = $this->query()->whereKey($primaryKey)->first();

Expand All @@ -225,7 +264,13 @@ public function update($primaryKey, $data)
$model->setAttribute($k, $v);
}

return $model->save();
$result = $model->save();

if ($result) {
$this->saved($model, true);
}

return $result;
}

/**
Expand All @@ -237,6 +282,8 @@ public function update($primaryKey, $data)
*/
public function store($data)
{
$this->saving($data);

$columns = $this->getTableColumns();
$model = $this->getModel();

Expand All @@ -248,7 +295,13 @@ public function store($data)
$model->setAttribute($k, $v);
}

return $model->save();
$result = $model->save();

if ($result) {
$this->saved($model);
}

return $result;
}

/**
Expand All @@ -260,7 +313,13 @@ public function store($data)
*/
public function delete(string $ids)
{
return $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->delete();
$result = $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->delete();

if ($result) {
$this->deleted($ids);
}

return $result;
}

/**
Expand Down Expand Up @@ -292,4 +351,46 @@ public function quickEditItem($data)
{
return $this->update(Arr::pull($data, $this->primaryKey()), $data);
}

/**
* saving 钩子 (执行于新增/修改前)
*
* 可以通过判断 $primaryKey 是否存在来判断是新增还是修改
*
* @param $data
* @param $primaryKey
*
* @return void
*/
public function saving($data, $primaryKey = '')
{

}

/**
* saved 钩子 (执行于新增/修改后)
*
* 可以通过 $isEdit 来判断是新增还是修改
*
* @param $model
* @param $isEdit
*
* @return void
*/
public function saved($model, $isEdit = false)
{

}

/**
* deleted 钩子 (执行于删除后)
*
* @param $ids
*
* @return void
*/
public function deleted($ids)
{

}
}

0 comments on commit 466800b

Please sign in to comment.