Skip to content

Commit

Permalink
refactor: AdminService 调整, 增加事务
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Oct 30, 2024
1 parent 75e94ea commit 5d8e5c2
Showing 1 changed file with 63 additions and 26 deletions.
89 changes: 63 additions & 26 deletions src/Services/AdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) . '%');
});
});
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 5d8e5c2

Please sign in to comment.