restore page method added, move page updated
parent
bed081881f
commit
c655bac34e
|
|
@ -19,7 +19,7 @@ class PagesController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function root(Request $request): JsonResponse {
|
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 {
|
public function find(Request $request): ?JsonResponse {
|
||||||
|
|
@ -45,12 +45,12 @@ class PagesController extends Controller {
|
||||||
$query = $this->model->byUuid($id);
|
$query = $this->model->byUuid($id);
|
||||||
if ($request->get('with_trashed')) $query->withTrashed();
|
if ($request->get('with_trashed')) $query->withTrashed();
|
||||||
$model = $query->firstOrFail();
|
$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 {
|
public function move(Request $request, $id): JsonResponse {
|
||||||
$model = $this->model->byUuid($id)->firstOrFail();
|
$model = $this->model->byUuid($id)->withTrashed()->firstOrFail();
|
||||||
$parent = Page::byUuid($request->get('parent'))->first();
|
$parent = Page::byUuid($request->get('parent'))->withTrashed()->first();
|
||||||
$model->move($request->get('ord'), $parent);
|
$model->move($request->get('ord'), $parent);
|
||||||
return fractal($model, new PageTransformer())->respond();
|
return fractal($model, new PageTransformer())->respond();
|
||||||
}
|
}
|
||||||
|
|
@ -71,9 +71,16 @@ class PagesController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function destroy(Request $request, $uuid): JsonResponse {
|
public function destroy(Request $request, $uuid): JsonResponse {
|
||||||
$model = $this->model->byUuid($uuid)->firstOrFail();
|
$model = $this->model->byUuid($uuid)->withTrashed()->firstOrFail();
|
||||||
$model->delete();
|
$model->trashed() ? $model->forceDelete() : $model->delete();
|
||||||
return response()->json(null, 204);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ class Page extends Model {
|
||||||
|
|
||||||
|
|
||||||
public function move($ord, ?Page $parent = null) {
|
public function move($ord, ?Page $parent = null) {
|
||||||
$prevParent = $this->parent;
|
$prevParent = $this->parent()->withTrashed()->first();
|
||||||
if (($parent->id ?? 0) === ($prevParent->id ?? 0)) {
|
if (($parent->id ?? 0) === ($prevParent->id ?? 0)) {
|
||||||
($ord > $this->ord) ? $this->moveSet('backward', $this->ord, $ord, $parent) : $this->moveSet('forward', $ord, $this->ord, $parent);
|
($ord > $this->ord) ? $this->moveSet('backward', $this->ord, $ord, $parent) : $this->moveSet('forward', $ord, $this->ord, $parent);
|
||||||
} else $this->moveSet('forward', $ord, null, $parent);
|
} else $this->moveSet('forward', $ord, null, $parent);
|
||||||
|
|
@ -165,7 +165,7 @@ class Page extends Model {
|
||||||
$this->checkConflictedProps();
|
$this->checkConflictedProps();
|
||||||
}
|
}
|
||||||
public function moveSet($dir = 'forward', $ordFrom = null, $ordTo = null, ?Page $parent = null) {
|
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 ($ordFrom !== null) $query->where('ord', '>=', $ordFrom);
|
||||||
if ($ordTo !== null) $query->where('ord', '<=', $ordTo);
|
if ($ordTo !== null) $query->where('ord', '<=', $ordTo);
|
||||||
$query->get()->each(function($page) use($dir) {
|
$query->get()->each(function($page) use($dir) {
|
||||||
|
|
@ -174,14 +174,14 @@ class Page extends Model {
|
||||||
}
|
}
|
||||||
public function trimIndexes($parentIds) {
|
public function trimIndexes($parentIds) {
|
||||||
collect(is_array($parentIds) ? $parentIds : [$parentIds])->unique()->each(function($parentId) {
|
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]);
|
if ($page->ord !== $index) $page->update(['ord' => $index]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMaxOrd(): int {
|
public function getMaxOrd(): int {
|
||||||
$res = $this->siblings()->max('ord');
|
$res = $this->siblings()->withTrashed()->max('ord');
|
||||||
return ($res !== null) ? ($res + 1) : 0;
|
return ($res !== null) ? ($res + 1) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ Route::group(['prefix' => 'pages'], function() {
|
||||||
Route::put('/move/{id}', 'Api\Pages\PagesController@move');
|
Route::put('/move/{id}', 'Api\Pages\PagesController@move');
|
||||||
Route::put('/clone/{id}', 'Api\Pages\PagesController@clone');
|
Route::put('/clone/{id}', 'Api\Pages\PagesController@clone');
|
||||||
Route::delete('/{id}', 'Api\Pages\PagesController@destroy');
|
Route::delete('/{id}', 'Api\Pages\PagesController@destroy');
|
||||||
|
Route::patch('/restore/{id}', 'Api\Pages\PagesController@restore');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue