1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

Complete list endpoint and add some tests

This commit is contained in:
julesdevops
2022-04-20 22:58:16 +02:00
parent 55e52e45fb
commit f14e6e8f2d
3 changed files with 116 additions and 16 deletions

View File

@ -2,10 +2,11 @@
namespace BookStack\Http\Controllers\Api;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Deletion;
use BookStack\Entities\Repos\DeletionRepo;
use BookStack\Entities\Tools\TrashCan;
use Closure;
class RecycleBinApiController extends ApiController
{
@ -22,24 +23,55 @@ class RecycleBinApiController extends ApiController
public function list()
{
return $this->apiListingResponse(Deletion::query(), [
'id',
'deleted_by',
'id',
'deleted_by',
'created_at',
'updated_at',
'deletable_type',
'deletable_id'
]);
'deletable_id',
], [Closure::fromCallable([$this, 'listFormatter'])]);
}
public function restore(DeletionRepo $deletionRepo, string $id)
{
$restoreCount = $deletionRepo->restore((int) $id);
return response()->json(['restore_count' => $restoreCount]);
}
public function destroy(DeletionRepo $deletionRepo, string $id)
{
$deleteCount = $deletionRepo->destroy((int) $id);
return response()->json(['delete_count' => $deleteCount]);
}
}
protected function listFormatter(Deletion $deletion)
{
$deletable = $deletion->deletable;
$isBook = $deletable instanceof Book;
$parent = null;
$children = null;
if ($isBook) {
$chapterCount = $deletable->chapters()->withTrashed()->count();
$children['Bookstack\Chapter'] = $chapterCount;
}
if ($isBook || $deletion->deletable instanceof Chapter) {
$pageCount = $deletable->pages()->withTrashed()->count();
$children['Bookstack\Page'] = $pageCount;
}
$parentEntity = $deletable->getParent();
$parent = [];
if ($parentEntity) {
$parent['type'] = $parentEntity->getMorphClass();
$parent['id'] = $parentEntity->getKey();
}
$deletion->setAttribute('parent', $parent);
$deletion->setAttribute('children', $children);
}
}