use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\UserEntityWatchOptions;
-use BookStack\Entities\Models\Bookshelf;
-use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\BookQueries;
+use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
class BookController extends Controller
{
- protected BookRepo $bookRepo;
- protected ShelfContext $shelfContext;
- protected ReferenceFetcher $referenceFetcher;
-
- public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
- {
- $this->bookRepo = $bookRepo;
- $this->shelfContext = $entityContextManager;
- $this->referenceFetcher = $referenceFetcher;
+ public function __construct(
+ protected ShelfContext $shelfContext,
+ protected BookRepo $bookRepo,
+ protected BookQueries $queries,
+ protected BookshelfQueries $shelfQueries,
+ protected ReferenceFetcher $referenceFetcher,
+ ) {
}
/**
'updated_at' => trans('common.sort_updated_at'),
]);
- $books = $this->bookRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
- $recents = $this->isSignedIn() ? $this->bookRepo->getRecentlyViewed(4) : false;
- $popular = $this->bookRepo->getPopular(4);
- $new = $this->bookRepo->getRecentlyCreated(4);
+ $books = $this->queries->visibleForListWithCover()
+ ->orderBy($listOptions->getSort(), $listOptions->getOrder())
+ ->paginate(18);
+ $recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->take(4)->get() : false;
+ $popular = $this->queries->popularForList()->take(4)->get();
+ $new = $this->queries->visibleForList()->orderBy('created_at', 'desc')->take(4)->get();
$this->shelfContext->clearShelfContext();
/**
* Show the form for creating a new book.
*/
- public function create(string $shelfSlug = null)
+ public function create(?string $shelfSlug = null)
{
$this->checkPermission('book-create-all');
$bookshelf = null;
if ($shelfSlug !== null) {
- $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
+ $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
$this->setPageTitle(trans('entities.books_create'));
- $templates = Page::visible()
- ->where('template', '=', true)
- ->orderBy('name', 'asc')
- ->get();
-
return view('books.create', [
'bookshelf' => $bookshelf,
- 'templates' => $templates,
]);
}
* @throws ImageUploadException
* @throws ValidationException
*/
- public function store(Request $request, string $shelfSlug = null)
+ public function store(Request $request, ?string $shelfSlug = null)
{
$this->checkPermission('book-create-all');
$validated = $this->validate($request, [
- 'name' => ['required', 'string', 'max:255'],
- 'description' => ['string', 'max:1000'],
- 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
- 'tags' => ['array'],
- 'default_template' => ['nullable', 'exists:pages,id'],
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
]);
$bookshelf = null;
if ($shelfSlug !== null) {
- $bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
+ $bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
*/
public function show(Request $request, ActivityQueries $activities, string $slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->queries->findVisibleBySlugOrFail($slug);
$bookChildren = (new BookContents($book))->getTree(true);
$bookParentShelves = $book->shelves()->scopes('visible')->get();
'bookParentShelves' => $bookParentShelves,
'watchOptions' => new UserEntityWatchOptions(user(), $book),
'activity' => $activities->entityActivity($book, 20, 1),
- 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($book),
+ 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($book),
]);
}
*/
public function edit(string $slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->queries->findVisibleBySlugOrFail($slug);
$this->checkOwnablePermission('book-update', $book);
$this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
- $templates = Page::visible()
- ->where('template', '=', true)
- ->orderBy('name', 'asc')
- ->get();
-
- return view('books.edit', ['book' => $book, 'current' => $book, 'templates' => $templates]);
+ return view('books.edit', ['book' => $book, 'current' => $book]);
}
/**
*/
public function update(Request $request, string $slug)
{
- $book = $this->bookRepo->getBySlug($slug);
+ $book = $this->queries->findVisibleBySlugOrFail($slug);
$this->checkOwnablePermission('book-update', $book);
$validated = $this->validate($request, [
- 'name' => ['required', 'string', 'max:255'],
- 'description' => ['string', 'max:1000'],
- 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
- 'tags' => ['array'],
- 'default_template' => ['nullable', 'exists:pages,id'],
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
]);
if ($request->has('image_reset')) {
*/
public function showDelete(string $bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('book-delete', $book);
$this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
*/
public function destroy(string $bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('book-delete', $book);
$this->bookRepo->destroy($book);
*/
public function showCopy(string $bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('book-view', $book);
session()->flashInput(['name' => $book->name]);
*/
public function copy(Request $request, Cloner $cloner, string $bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('book-view', $book);
$this->checkPermission('book-create-all');
*/
public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
{
- $book = $this->bookRepo->getBySlug($bookSlug);
+ $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
$this->checkOwnablePermission('book-update', $book);
$this->checkOwnablePermission('book-delete', $book);
$this->checkPermission('bookshelf-create-all');