FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

do some cleanup and add doc · equippedcoding/BookStack@14bccae · GitHub

Commit 14bccae

Browse files
committed
do some cleanup and add doc
1 parent f14e6e8 commit 14bccae

5 files changed

Lines changed: 88 additions & 30 deletions

File tree

‎app/Http/Controllers/Api/RecycleBinApiController.php‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace BookStack\Http\Controllers\Api;
44

5-
use BookStack\Entities\Models\Book;
6-
use BookStack\Entities\Models\Chapter;
75
use BookStack\Entities\Models\Deletion;
86
use BookStack\Entities\Repos\DeletionRepo;
97
use Closure;
108

119
class RecycleBinApiController extends ApiController
1210
{
11+
protected $fieldsToExpose = [
12+
'id', 'deleted_by', 'created_at', 'updated_at', 'deletable_type', 'deletable_id',
13+
];
14+
1315
public function __construct()
1416
{
1517
$this->middleware(function ($request, $next) {
@@ -20,9 +22,13 @@ public function __construct()
2022
});
2123
}
2224

25+
/**
26+
* Get a top-level listing of the items in the recycle bin.
27+
* Requires the permission to manage settings and restrictions.
28+
*/
2329
public function list()
2430
{
25-
return $this->apiListingResponse(Deletion::query(), [
31+
return $this->apiListingResponse(Deletion::query()->with('deletable'), [
2632
'id',
2733
'deleted_by',
2834
'created_at',
@@ -32,13 +38,22 @@ public function list()
3238
], [Closure::fromCallable([$this, 'listFormatter'])]);
3339
}
3440

41+
/**
42+
* Restore a single deletion from the recycle bin.
43+
* You must provide the deletion id, not the id of the corresponding deleted item.
44+
*/
3545
public function restore(DeletionRepo $deletionRepo, string $id)
3646
{
3747
$restoreCount = $deletionRepo->restore((int) $id);
3848

3949
return response()->json(['restore_count' => $restoreCount]);
4050
}
4151

52+
/**
53+
* Remove a single deletion from the recycle bin.
54+
* Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
55+
* You must provide the deletion id, not the id of the corresponding deleted item.
56+
*/
4257
public function destroy(DeletionRepo $deletionRepo, string $id)
4358
{
4459
$deleteCount = $deletionRepo->destroy((int) $id);
@@ -48,23 +63,26 @@ public function destroy(DeletionRepo $deletionRepo, string $id)
4863

4964
protected function listFormatter(Deletion $deletion)
5065
{
66+
$deletion->makeVisible($this->fieldsToExpose);
67+
$deletion->makeHidden('deletable');
68+
5169
$deletable = $deletion->deletable;
52-
$isBook = $deletable instanceof Book;
70+
$isBook = $deletion->deletable_type === "BookStack\Book";
5371
$parent = null;
5472
$children = null;
5573

5674
if ($isBook) {
57-
$chapterCount = $deletable->chapters()->withTrashed()->count();
58-
$children['Bookstack\Chapter'] = $chapterCount;
75+
$chapterCount = $deletable->chapters()->withTrashed()->count();
76+
$children['BookStack\Chapter'] = $chapterCount;
5977
}
6078

61-
if ($isBook || $deletion->deletable instanceof Chapter) {
62-
$pageCount = $deletable->pages()->withTrashed()->count();
63-
$children['Bookstack\Page'] = $pageCount;
79+
if ($isBook || $deletion->deletable_type === "BookStack\Chapter") {
80+
$pageCount = $deletable->pages()->withTrashed()->count();
81+
$children['BookStack\Page'] = $pageCount;
6482
}
6583

6684
$parentEntity = $deletable->getParent();
67-
$parent = [];
85+
$parent = null;
6886

6987
if ($parentEntity) {
7088
$parent['type'] = $parentEntity->getMorphClass();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"delete_count": 2
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"data": [
3+
{
4+
"id": 25,
5+
"deleted_by": 1,
6+
"created_at": "2022-04-24T07:59:34.000000Z",
7+
"updated_at": "2022-04-24T07:59:34.000000Z",
8+
"deletable_type": "BookStack\\Book",
9+
"deletable_id": 4,
10+
"parent": {
11+
"type": "BookStack\\Book",
12+
"id": 25
13+
},
14+
"children": {
15+
"BookStack\\Chapter": 0,
16+
"BookStack\\Page": 1
17+
}
18+
},
19+
{
20+
"id": 26,
21+
"deleted_by": 1,
22+
"created_at": "2022-04-24T07:59:35.000000Z",
23+
"updated_at": "2022-04-24T07:59:35.000000Z",
24+
"deletable_type": "BookStack\\Book",
25+
"deletable_id": 3,
26+
"parent": [],
27+
"children": {
28+
"BookStack\\Chapter": 1,
29+
"BookStack\\Page": 1
30+
}
31+
}
32+
],
33+
"total": 2
34+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"restore_count": 2
3+
}

‎tests/Api/RecycleBinApiTest.php‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public function test_settings_manage_permission_needed_for_all_endpoints()
3333
}
3434
}
3535

36-
public function test_restrictions_manage_all_permission_neeed_for_all_endpoints()
36+
public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
3737
{
3838
$editor = $this->getEditor();
3939
$this->giveUserPermissions($editor, ['restrictions-manage-all']);
4040
$this->actingAs($editor);
41-
41+
4242
foreach ($this->endpointMap as [$method, $uri]) {
4343
$resp = $this->json($method, $uri);
4444
$resp->assertStatus(403);
@@ -74,15 +74,15 @@ public function test_index_endpoint_returns_expected_page()
7474
});
7575

7676
$resp->assertJson([
77-
'data' => $expectedData->values()->all(),
77+
'data' => $expectedData->values()->all(),
7878
'total' => 2,
7979
]);
8080
}
8181

8282
public function test_index_endpoint_returns_children()
8383
{
8484
$this->actingAsAuthorizedUser();
85-
85+
8686
$book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
8787
$editor = $this->getEditor();
8888
$this->actingAs($editor)->delete($book->getUrl());
@@ -100,15 +100,15 @@ public function test_index_endpoint_returns_children()
100100
'deletable_type' => $book->getMorphClass(),
101101
'deletable_id' => $book->getKey(),
102102
'children' => [
103-
'Bookstack\Page' => $book->pages_count,
104-
'Bookstack\Chapter' => $book->chapters_count,
103+
'BookStack\Page' => $book->pages_count,
104+
'BookStack\Chapter' => $book->chapters_count,
105105
],
106106
'parent' => null,
107-
]
107+
],
108108
];
109109

110110
$resp->assertJson([
111-
'data' => $expectedData,
111+
'data' => $expectedData,
112112
'total' => 1,
113113
]);
114114
}
@@ -136,22 +136,22 @@ public function test_index_endpoint_returns_parent()
136136
'deletable_id' => $page->getKey(),
137137
'parent' => [
138138
'type' => 'BookStack\Chapter',
139-
'id' => $page->chapter->getKey()
139+
'id' => $page->chapter->getKey(),
140140
],
141141
'children' => null,
142-
]
142+
],
143143
];
144144

145145
$resp->assertJson([
146-
'data' => $expectedData,
147-
'total' => 1
146+
'data' => $expectedData,
147+
'total' => 1,
148148
]);
149149
}
150150

151151
public function test_restore_endpoint()
152152
{
153153
$this->actingAsAuthorizedUser();
154-
154+
155155
$page = Page::query()->first();
156156
$editor = $this->getEditor();
157157
$this->actingAs($editor)->delete($page->getUrl());
@@ -160,22 +160,22 @@ public function test_restore_endpoint()
160160
$deletion = Deletion::query()->orderBy('id')->first();
161161

162162
$this->assertDatabaseHas('pages', [
163-
'id' => $page->getKey(),
164-
'deleted_at' => $page->deleted_at,
163+
'id' => $page->getKey(),
164+
'deleted_at' => $page->deleted_at,
165165
]);
166166

167167
$this->putJson($this->baseEndpoint . '/' . $deletion->getKey());
168168

169169
$this->assertDatabaseHas('pages', [
170-
'id' => $page->getKey(),
171-
'deleted_at' => null,
170+
'id' => $page->getKey(),
171+
'deleted_at' => null,
172172
]);
173173
}
174174

175175
public function test_destroy_endpoint()
176176
{
177177
$this->actingAsAuthorizedUser();
178-
178+
179179
$page = Page::query()->first();
180180
$editor = $this->getEditor();
181181
$this->actingAs($editor)->delete($page->getUrl());
@@ -184,8 +184,8 @@ public function test_destroy_endpoint()
184184
$deletion = Deletion::query()->orderBy('id')->first();
185185

186186
$this->assertDatabaseHas('pages', [
187-
'id' => $page->getKey(),
188-
'deleted_at' => $page->deleted_at,
187+
'id' => $page->getKey(),
188+
'deleted_at' => $page->deleted_at,
189189
]);
190190

191191
$this->deleteJson($this->baseEndpoint . '/' . $deletion->getKey());

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL