From 5d8e5c26cd9b00e40902c8ccb8270159e4b811d9 Mon Sep 17 00:00:00 2001 From: slowlyo Date: Wed, 30 Oct 2024 22:08:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20AdminService=20=E8=B0=83=E6=95=B4,?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/AdminService.php | 89 +++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 26 deletions(-) diff --git a/src/Services/AdminService.php b/src/Services/AdminService.php index 885cba39..9cb02424 100644 --- a/src/Services/AdminService.php +++ b/src/Services/AdminService.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; use Slowlyo\OwlAdmin\Renderers\Page; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Schema; @@ -229,7 +230,7 @@ public function searchable($query) collect(array_keys(request()->query())) ->intersect($this->getTableColumns()) ->map(function ($field) use ($query) { - $query->when(request($field), function ($query) use ($field) { + $query->when(filled(request($field)), function ($query) use ($field) { $query->where($field, 'like', '%' . request($field) . '%'); }); }); @@ -293,22 +294,31 @@ public function list() */ public function update($primaryKey, $data) { - $this->saving($data, $primaryKey); + DB::beginTransaction(); + try { + $this->saving($data, $primaryKey); - $model = $this->query()->whereKey($primaryKey)->first(); + $model = $this->query()->whereKey($primaryKey)->first(); - foreach ($data as $k => $v) { - if (!$this->hasColumn($k)) { - continue; + foreach ($data as $k => $v) { + if (!$this->hasColumn($k)) { + continue; + } + + $model->setAttribute($k, $v); } - $model->setAttribute($k, $v); - } + $result = $model->save(); + + if ($result) { + $this->saved($model, true); + } - $result = $model->save(); + DB::commit(); + } catch (\Throwable $e) { + DB::rollBack(); - if ($result) { - $this->saved($model, true); + admin_abort($e->getMessage()); } return $result; @@ -323,22 +333,31 @@ public function update($primaryKey, $data) */ public function store($data) { - $this->saving($data); + DB::beginTransaction(); + try { + $this->saving($data); - $model = $this->getModel(); + $model = $this->getModel(); + + foreach ($data as $k => $v) { + if (!$this->hasColumn($k)) { + continue; + } - foreach ($data as $k => $v) { - if (!$this->hasColumn($k)) { - continue; + $model->setAttribute($k, $v); } - $model->setAttribute($k, $v); - } + $result = $model->save(); + + if ($result) { + $this->saved($model); + } - $result = $model->save(); + DB::commit(); + } catch (\Throwable $e) { + DB::rollBack(); - if ($result) { - $this->saved($model); + admin_abort($e->getMessage()); } return $result; @@ -353,10 +372,19 @@ public function store($data) */ public function delete(string $ids) { - $result = $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->delete(); + DB::beginTransaction(); + try { + $result = $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->delete(); + + if ($result) { + $this->deleted($ids); + } - if ($result) { - $this->deleted($ids); + DB::commit(); + } catch (\Throwable $e) { + DB::rollBack(); + + admin_abort($e->getMessage()); } return $result; @@ -373,8 +401,17 @@ public function quickEdit($data) { $rowsDiff = data_get($data, 'rowsDiff', []); - foreach ($rowsDiff as $item) { - $this->update(Arr::pull($item, $this->primaryKey()), $item); + DB::beginTransaction(); + try { + foreach ($rowsDiff as $item) { + $this->update(Arr::pull($item, $this->primaryKey()), $item); + } + + DB::commit(); + } catch (\Throwable $e) { + DB::rollBack(); + + admin_abort($e->getMessage()); } return true;