3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Deletion;
8 use BookStack\Entities\Repos\DeletionRepo;
11 class RecycleBinApiController extends ApiController
13 public function __construct()
15 $this->middleware(function ($request, $next) {
16 $this->checkPermission('settings-manage');
17 $this->checkPermission('restrictions-manage-all');
19 return $next($request);
23 public function list()
25 return $this->apiListingResponse(Deletion::query(), [
32 ], [Closure::fromCallable([$this, 'listFormatter'])]);
35 public function restore(DeletionRepo $deletionRepo, string $id)
37 $restoreCount = $deletionRepo->restore((int) $id);
39 return response()->json(['restore_count' => $restoreCount]);
42 public function destroy(DeletionRepo $deletionRepo, string $id)
44 $deleteCount = $deletionRepo->destroy((int) $id);
46 return response()->json(['delete_count' => $deleteCount]);
49 protected function listFormatter(Deletion $deletion)
51 $deletable = $deletion->deletable;
52 $isBook = $deletable instanceof Book;
57 $chapterCount = $deletable->chapters()->withTrashed()->count();
58 $children['Bookstack\Chapter'] = $chapterCount;
61 if ($isBook || $deletion->deletable instanceof Chapter) {
62 $pageCount = $deletable->pages()->withTrashed()->count();
63 $children['Bookstack\Page'] = $pageCount;
66 $parentEntity = $deletable->getParent();
70 $parent['type'] = $parentEntity->getMorphClass();
71 $parent['id'] = $parentEntity->getKey();
74 $deletion->setAttribute('parent', $parent);
75 $deletion->setAttribute('children', $children);