Skip to content

Commit 1ea2ac8

Browse files
committed
Queries: Update API to align data with previous versions
Ensures fields returned match API docs and previous versions of BookStack where we were accidentally returning more fields than expected. Updates tests to cover many of these. Also updated clockwork to ignore image requests for less noisy debugging. Also updated chapter page query to not be loading all page data, via new query in PageQueries.
1 parent ed9c013 commit 1ea2ac8

File tree

11 files changed

+56
-14
lines changed

11 files changed

+56
-14
lines changed

app/Config/clockwork.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@
173173

174174
// List of URIs that should not be collected
175175
'except' => [
176+
'/uploads/images/.*', // BookStack image requests
177+
176178
'/horizon/.*', // Laravel Horizon requests
177179
'/telescope/.*', // Laravel Telescope requests
178180
'/_debugbar/.*', // Laravel DebugBar requests

app/Entities/Controllers/BookApiController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public function __construct(
2626
*/
2727
public function list()
2828
{
29-
$books = $this->queries->visibleForList();
29+
$books = $this->queries
30+
->visibleForList()
31+
->addSelect(['created_by', 'updated_by']);
3032

3133
return $this->apiListingResponse($books, [
3234
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',

app/Entities/Controllers/BookshelfApiController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public function __construct(
2424
*/
2525
public function list()
2626
{
27-
$shelves = $this->queries->visibleForList();
27+
$shelves = $this->queries
28+
->visibleForList()
29+
->addSelect(['created_by', 'updated_by']);
2830

2931
return $this->apiListingResponse($shelves, [
3032
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',

app/Entities/Controllers/ChapterApiController.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace BookStack\Entities\Controllers;
44

55
use BookStack\Entities\Models\Chapter;
6-
use BookStack\Entities\Queries\BookQueries;
76
use BookStack\Entities\Queries\ChapterQueries;
7+
use BookStack\Entities\Queries\EntityQueries;
88
use BookStack\Entities\Repos\ChapterRepo;
99
use BookStack\Exceptions\PermissionsException;
1010
use BookStack\Http\ApiController;
@@ -38,7 +38,7 @@ class ChapterApiController extends ApiController
3838
public function __construct(
3939
protected ChapterRepo $chapterRepo,
4040
protected ChapterQueries $queries,
41-
protected BookQueries $bookQueries,
41+
protected EntityQueries $entityQueries,
4242
) {
4343
}
4444

@@ -47,7 +47,8 @@ public function __construct(
4747
*/
4848
public function list()
4949
{
50-
$chapters = $this->queries->visibleForList();
50+
$chapters = $this->queries->visibleForList()
51+
->addSelect(['created_by', 'updated_by']);
5152

5253
return $this->apiListingResponse($chapters, [
5354
'id', 'book_id', 'name', 'slug', 'description', 'priority',
@@ -63,7 +64,7 @@ public function create(Request $request)
6364
$requestData = $this->validate($request, $this->rules['create']);
6465

6566
$bookId = $request->get('book_id');
66-
$book = $this->bookQueries->findVisibleByIdOrFail(intval($bookId));
67+
$book = $this->entityQueries->books->findVisibleByIdOrFail(intval($bookId));
6768
$this->checkOwnablePermission('chapter-create', $book);
6869

6970
$chapter = $this->chapterRepo->create($requestData, $book);
@@ -79,12 +80,14 @@ public function read(string $id)
7980
$chapter = $this->queries->findVisibleByIdOrFail(intval($id));
8081
$chapter = $this->forJsonDisplay($chapter);
8182

82-
$chapter->load([
83-
'createdBy', 'updatedBy', 'ownedBy',
84-
'pages' => function (HasMany $query) {
85-
$query->scopes('visible')->get(['id', 'name', 'slug']);
86-
}
87-
]);
83+
$chapter->load(['createdBy', 'updatedBy', 'ownedBy']);
84+
85+
// Note: More fields than usual here, for backwards compatibility,
86+
// due to previously accidentally including more fields that desired.
87+
$pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)
88+
->addSelect(['created_by', 'updated_by', 'revision_count', 'editor'])
89+
->get();
90+
$chapter->setRelation('pages', $pages);
8891

8992
return response()->json($chapter);
9093
}

app/Entities/Controllers/ChapterController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function show(string $bookSlug, string $chapterSlug)
7979
$this->checkOwnablePermission('chapter-view', $chapter);
8080

8181
$sidebarTree = (new BookContents($chapter->book))->getTree();
82-
$pages = $chapter->getVisiblePages();
82+
$pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
83+
8384
$nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
8485
View::incrementFor($chapter);
8586

app/Entities/Controllers/PageApiController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function __construct(
4545
*/
4646
public function list()
4747
{
48-
$pages = $this->queries->visibleForList();
48+
$pages = $this->queries->visibleForList()
49+
->addSelect(['created_by', 'updated_by', 'revision_count', 'editor']);
4950

5051
return $this->apiListingResponse($pages, [
5152
'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',

app/Entities/Queries/PageQueries.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public function visibleForList(): Builder
7373
->select($this->mergeBookSlugForSelect(static::$listAttributes));
7474
}
7575

76+
public function visibleForChapterList(int $chapterId): Builder
77+
{
78+
return $this->visibleForList()
79+
->where('chapter_id', '=', $chapterId)
80+
->orderBy('draft', 'desc')
81+
->orderBy('priority', 'asc');
82+
}
83+
7684
public function visibleWithContents(): Builder
7785
{
7886
return $this->start()

tests/Api/BooksApiTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function test_index_endpoint_returns_expected_book()
2424
'id' => $firstBook->id,
2525
'name' => $firstBook->name,
2626
'slug' => $firstBook->slug,
27+
'owned_by' => $firstBook->owned_by,
28+
'created_by' => $firstBook->created_by,
29+
'updated_by' => $firstBook->updated_by,
2730
],
2831
]]);
2932
}

tests/Api/ChaptersApiTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function test_index_endpoint_returns_expected_chapter()
2828
'book_id' => $firstChapter->book->id,
2929
'priority' => $firstChapter->priority,
3030
'book_slug' => $firstChapter->book->slug,
31+
'owned_by' => $firstChapter->owned_by,
32+
'created_by' => $firstChapter->created_by,
33+
'updated_by' => $firstChapter->updated_by,
3134
],
3235
]]);
3336
}
@@ -149,6 +152,16 @@ public function test_read_endpoint()
149152
'id' => $page->id,
150153
'slug' => $page->slug,
151154
'name' => $page->name,
155+
'owned_by' => $page->owned_by,
156+
'created_by' => $page->created_by,
157+
'updated_by' => $page->updated_by,
158+
'book_id' => $page->id,
159+
'chapter_id' => $chapter->id,
160+
'priority' => $page->priority,
161+
'book_slug' => $chapter->book->slug,
162+
'draft' => $page->draft,
163+
'template' => $page->template,
164+
'editor' => $page->editor,
152165
],
153166
],
154167
'default_template_id' => null,

tests/Api/PagesApiTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function test_index_endpoint_returns_expected_page()
2727
'slug' => $firstPage->slug,
2828
'book_id' => $firstPage->book->id,
2929
'priority' => $firstPage->priority,
30+
'owned_by' => $firstPage->owned_by,
31+
'created_by' => $firstPage->created_by,
32+
'updated_by' => $firstPage->updated_by,
33+
'revision_count' => $firstPage->revision_count,
3034
],
3135
]]);
3236
}

0 commit comments

Comments
 (0)