2024-02-04 14:32:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
|
|
use BookStack\Exceptions\NotFoundException;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
2024-02-05 10:59:20 -05:00
|
|
|
class BookshelfQueries implements ProvidesEntityQueries
|
2024-02-04 14:32:19 -05:00
|
|
|
{
|
2024-02-07 17:41:45 -05:00
|
|
|
protected static array $listAttributes = [
|
|
|
|
'id', 'slug', 'name', 'description', 'created_at', 'updated_at', 'image_id'
|
|
|
|
];
|
|
|
|
|
2024-02-04 14:32:19 -05:00
|
|
|
public function start(): Builder
|
|
|
|
{
|
|
|
|
return Bookshelf::query();
|
|
|
|
}
|
|
|
|
|
2024-02-05 10:59:20 -05:00
|
|
|
public function findVisibleById(int $id): ?Bookshelf
|
|
|
|
{
|
|
|
|
return $this->start()->scopes('visible')->find($id);
|
|
|
|
}
|
|
|
|
|
2024-02-07 16:58:27 -05:00
|
|
|
public function findVisibleByIdOrFail(int $id): Bookshelf
|
|
|
|
{
|
|
|
|
$shelf = $this->findVisibleById($id);
|
|
|
|
|
|
|
|
if (is_null($shelf)) {
|
|
|
|
throw new NotFoundException(trans('errors.bookshelf_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shelf;
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:35:49 -05:00
|
|
|
public function findVisibleBySlugOrFail(string $slug): Bookshelf
|
2024-02-04 14:32:19 -05:00
|
|
|
{
|
|
|
|
/** @var ?Bookshelf $shelf */
|
|
|
|
$shelf = $this->start()
|
|
|
|
->scopes('visible')
|
|
|
|
->where('slug', '=', $slug)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($shelf === null) {
|
|
|
|
throw new NotFoundException(trans('errors.bookshelf_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shelf;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function visibleForList(): Builder
|
|
|
|
{
|
2024-02-07 17:41:45 -05:00
|
|
|
return $this->start()->scopes('visible')->select(static::$listAttributes);
|
2024-02-04 14:32:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function visibleForListWithCover(): Builder
|
|
|
|
{
|
|
|
|
return $this->visibleForList()->with('cover');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function recentlyViewedForCurrentUser(): Builder
|
|
|
|
{
|
|
|
|
return $this->visibleForList()
|
|
|
|
->scopes('withLastView')
|
|
|
|
->having('last_viewed_at', '>', 0)
|
|
|
|
->orderBy('last_viewed_at', 'desc');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function popularForList(): Builder
|
|
|
|
{
|
|
|
|
return $this->visibleForList()
|
|
|
|
->scopes('withViewCount')
|
|
|
|
->having('view_count', '>', 0)
|
|
|
|
->orderBy('view_count', 'desc');
|
|
|
|
}
|
|
|
|
}
|