restore page method added, move page updated

master
Константин 2023-11-20 18:09:50 +03:00
parent bed081881f
commit c655bac34e
3 changed files with 18 additions and 10 deletions

View File

@ -19,7 +19,7 @@ class PagesController extends Controller {
}
public function root(Request $request): JsonResponse {
return fractal(Page::root($request->get('with_trashed')), new PageTransformer())->respond();
return fractal(Page::root($request->get('with_trashed')), new PageTransformer($request->get('with_trashed', false)))->respond();
}
public function find(Request $request): ?JsonResponse {
@ -45,12 +45,12 @@ class PagesController extends Controller {
$query = $this->model->byUuid($id);
if ($request->get('with_trashed')) $query->withTrashed();
$model = $query->firstOrFail();
return fractal($model, new PageTransformer($request->get('with_trashed')))->respond();
return fractal($model, new PageTransformer($request->get('with_trashed', false)))->respond();
}
public function move(Request $request, $id): JsonResponse {
$model = $this->model->byUuid($id)->firstOrFail();
$parent = Page::byUuid($request->get('parent'))->first();
$model = $this->model->byUuid($id)->withTrashed()->firstOrFail();
$parent = Page::byUuid($request->get('parent'))->withTrashed()->first();
$model->move($request->get('ord'), $parent);
return fractal($model, new PageTransformer())->respond();
}
@ -71,9 +71,16 @@ class PagesController extends Controller {
}
public function destroy(Request $request, $uuid): JsonResponse {
$model = $this->model->byUuid($uuid)->firstOrFail();
$model->delete();
$model = $this->model->byUuid($uuid)->withTrashed()->firstOrFail();
$model->trashed() ? $model->forceDelete() : $model->delete();
return response()->json(null, 204);
}
public function restore(Request $request, $uuid): JsonResponse {
$model = $this->model->byUuid($uuid)->withTrashed()->firstOrFail();
$model->restore();
$model->checkConflictedProps();
return fractal($model, new PageTransformer())->respond();
}
}

View File

@ -156,7 +156,7 @@ class Page extends Model {
public function move($ord, ?Page $parent = null) {
$prevParent = $this->parent;
$prevParent = $this->parent()->withTrashed()->first();
if (($parent->id ?? 0) === ($prevParent->id ?? 0)) {
($ord > $this->ord) ? $this->moveSet('backward', $this->ord, $ord, $parent) : $this->moveSet('forward', $ord, $this->ord, $parent);
} else $this->moveSet('forward', $ord, null, $parent);
@ -165,7 +165,7 @@ class Page extends Model {
$this->checkConflictedProps();
}
public function moveSet($dir = 'forward', $ordFrom = null, $ordTo = null, ?Page $parent = null) {
$query = Page::query()->where(['parent_id' => $parent->id ?? 0])->orderBy('ord');
$query = Page::query()->where(['parent_id' => $parent->id ?? 0])->withTrashed()->orderBy('ord');
if ($ordFrom !== null) $query->where('ord', '>=', $ordFrom);
if ($ordTo !== null) $query->where('ord', '<=', $ordTo);
$query->get()->each(function($page) use($dir) {
@ -174,14 +174,14 @@ class Page extends Model {
}
public function trimIndexes($parentIds) {
collect(is_array($parentIds) ? $parentIds : [$parentIds])->unique()->each(function($parentId) {
Page::query()->where(['parent_id' => $parentId])->orderBy('ord')->orderBy('id')->get()->each(function($page, $index) {
Page::query()->where(['parent_id' => $parentId])->withTrashed()->orderBy('ord')->orderBy('id')->get()->each(function($page, $index) {
if ($page->ord !== $index) $page->update(['ord' => $index]);
});
});
}
public function getMaxOrd(): int {
$res = $this->siblings()->max('ord');
$res = $this->siblings()->withTrashed()->max('ord');
return ($res !== null) ? ($res + 1) : 0;
}

View File

@ -27,6 +27,7 @@ Route::group(['prefix' => 'pages'], function() {
Route::put('/move/{id}', 'Api\Pages\PagesController@move');
Route::put('/clone/{id}', 'Api\Pages\PagesController@clone');
Route::delete('/{id}', 'Api\Pages\PagesController@destroy');
Route::patch('/restore/{id}', 'Api\Pages\PagesController@restore');
});
});