mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
Queries: Moved out or removed some class-level items
Also ran auto-removal of unused imports across app folder.
This commit is contained in:
@@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class BookQueries implements ProvidesEntityQueries
|
||||
{
|
||||
protected static array $listAttributes = [
|
||||
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
|
||||
];
|
||||
|
||||
public function start(): Builder
|
||||
{
|
||||
return Book::query();
|
||||
@@ -40,7 +44,8 @@ class BookQueries implements ProvidesEntityQueries
|
||||
|
||||
public function visibleForList(): Builder
|
||||
{
|
||||
return $this->start()->scopes('visible');
|
||||
return $this->start()->scopes('visible')
|
||||
->select(static::$listAttributes);
|
||||
}
|
||||
|
||||
public function visibleForListWithCover(): Builder
|
||||
|
@@ -8,6 +8,10 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class BookshelfQueries implements ProvidesEntityQueries
|
||||
{
|
||||
protected static array $listAttributes = [
|
||||
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
|
||||
];
|
||||
|
||||
public function start(): Builder
|
||||
{
|
||||
return Bookshelf::query();
|
||||
@@ -46,7 +50,7 @@ class BookshelfQueries implements ProvidesEntityQueries
|
||||
|
||||
public function visibleForList(): Builder
|
||||
{
|
||||
return $this->start()->scopes('visible');
|
||||
return $this->start()->scopes('visible')->select(static::$listAttributes);
|
||||
}
|
||||
|
||||
public function visibleForListWithCover(): Builder
|
||||
|
@@ -10,8 +10,7 @@ class ChapterQueries implements ProvidesEntityQueries
|
||||
{
|
||||
protected static array $listAttributes = [
|
||||
'id', 'slug', 'name', 'description', 'priority',
|
||||
'created_at', 'updated_at',
|
||||
'created_by', 'updated_by', 'owned_by',
|
||||
'created_at', 'updated_at'
|
||||
];
|
||||
|
||||
public function start(): Builder
|
||||
|
@@ -3,6 +3,8 @@
|
||||
namespace BookStack\Entities\Queries;
|
||||
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class EntityQueries
|
||||
{
|
||||
@@ -25,9 +27,25 @@ class EntityQueries
|
||||
$explodedId = explode(':', $identifier);
|
||||
$entityType = $explodedId[0];
|
||||
$entityId = intval($explodedId[1]);
|
||||
$queries = $this->getQueriesForType($entityType);
|
||||
|
||||
return $queries->findVisibleById($entityId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a query of visible entities of the given type,
|
||||
* suitable for listing display.
|
||||
*/
|
||||
public function visibleForList(string $entityType): Builder
|
||||
{
|
||||
$queries = $this->getQueriesForType($entityType);
|
||||
return $queries->visibleForList();
|
||||
}
|
||||
|
||||
protected function getQueriesForType(string $type): ProvidesEntityQueries
|
||||
{
|
||||
/** @var ?ProvidesEntityQueries $queries */
|
||||
$queries = match ($entityType) {
|
||||
$queries = match ($type) {
|
||||
'page' => $this->pages,
|
||||
'chapter' => $this->chapters,
|
||||
'book' => $this->books,
|
||||
@@ -36,9 +54,9 @@ class EntityQueries
|
||||
};
|
||||
|
||||
if (is_null($queries)) {
|
||||
return null;
|
||||
throw new InvalidArgumentException("No entity query class configured for {$type}");
|
||||
}
|
||||
|
||||
return $queries->findVisibleById($entityId);
|
||||
return $queries;
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,15 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PageQueries implements ProvidesEntityQueries
|
||||
{
|
||||
protected static array $contentAttributes = [
|
||||
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
|
||||
'template', 'html', 'text', 'created_at', 'updated_at', 'priority'
|
||||
];
|
||||
protected static array $listAttributes = [
|
||||
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
|
||||
'template', 'text', 'created_at', 'updated_at', 'priority'
|
||||
];
|
||||
|
||||
public function start(): Builder
|
||||
{
|
||||
return Page::query();
|
||||
@@ -60,22 +69,14 @@ class PageQueries implements ProvidesEntityQueries
|
||||
{
|
||||
return $this->start()
|
||||
->scopes('visible')
|
||||
->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) {
|
||||
$builder->select('slug')
|
||||
->from('books')
|
||||
->whereColumn('books.id', '=', 'pages.book_id');
|
||||
}]));
|
||||
->select($this->mergeBookSlugForSelect(static::$listAttributes));
|
||||
}
|
||||
|
||||
public function visibleWithContents(): Builder
|
||||
{
|
||||
return $this->start()
|
||||
->scopes('visible')
|
||||
->select(array_merge(Page::$contentAttributes, ['book_slug' => function ($builder) {
|
||||
$builder->select('slug')
|
||||
->from('books')
|
||||
->whereColumn('books.id', '=', 'pages.book_id');
|
||||
}]));
|
||||
->select($this->mergeBookSlugForSelect(static::$contentAttributes));
|
||||
}
|
||||
|
||||
public function currentUserDraftsForList(): Builder
|
||||
@@ -90,4 +91,13 @@ class PageQueries implements ProvidesEntityQueries
|
||||
return $this->visibleForList()
|
||||
->where('template', '=', true);
|
||||
}
|
||||
|
||||
protected function mergeBookSlugForSelect(array $columns): array
|
||||
{
|
||||
return array_merge($columns, ['book_slug' => function ($builder) {
|
||||
$builder->select('slug')
|
||||
->from('books')
|
||||
->whereColumn('books.id', '=', 'pages.book_id');
|
||||
}]);
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,19 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
*/
|
||||
interface ProvidesEntityQueries
|
||||
{
|
||||
/**
|
||||
* Start a new query for this entity type.
|
||||
*/
|
||||
public function start(): Builder;
|
||||
|
||||
/**
|
||||
* Find the entity of the given ID, or return null if not found.
|
||||
*/
|
||||
public function findVisibleById(int $id): ?Entity;
|
||||
|
||||
/**
|
||||
* Start a query for items that are visible, with selection
|
||||
* configured for list display of this item.
|
||||
*/
|
||||
public function visibleForList(): Builder;
|
||||
}
|
||||
|
Reference in New Issue
Block a user