Queries: Migrated revision repo queries to new class

This commit is contained in:
Dan Brown 2024-02-07 15:09:16 +00:00
parent 222c665018
commit c95f4ca40f
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 11 additions and 46 deletions

View File

@ -27,7 +27,10 @@ class PageRevisionQueries
public function findLatestCurrentUserDraftsForPageId(int $pageId): ?PageRevision
{
return $this->latestCurrentUserDraftsForPageId($pageId)->first();
/** @var ?PageRevision $revision */
$revision = $this->latestCurrentUserDraftsForPageId($pageId)->first();
return $revision;
}
public function latestCurrentUserDraftsForPageId(int $pageId): Builder

View File

@ -2,7 +2,7 @@
namespace BookStack\Entities\Queries;
use BookStack\App\Model;
use BookStack\Entities\Models\Entity;
use Illuminate\Database\Eloquent\Builder;
/**

View File

@ -4,39 +4,13 @@ namespace BookStack\Entities\Repos;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
use Illuminate\Database\Eloquent\Builder;
use BookStack\Entities\Queries\PageRevisionQueries;
class RevisionRepo
{
/**
* Get a revision by its stored book and page slug values.
*/
public function getBySlugs(string $bookSlug, string $pageSlug): ?PageRevision
{
/** @var ?PageRevision $revision */
$revision = PageRevision::query()
->whereHas('page', function (Builder $query) {
$query->scopes('visible');
})
->where('slug', '=', $pageSlug)
->where('type', '=', 'version')
->where('book_slug', '=', $bookSlug)
->orderBy('created_at', 'desc')
->with('page')
->first();
return $revision;
}
/**
* Get the latest draft revision, for the given page, belonging to the current user.
*/
public function getLatestDraftForCurrentUser(Page $page): ?PageRevision
{
/** @var ?PageRevision $revision */
$revision = $this->queryForCurrentUserDraft($page->id)->first();
return $revision;
public function __construct(
protected PageRevisionQueries $queries,
) {
}
/**
@ -44,7 +18,7 @@ class RevisionRepo
*/
public function deleteDraftsForCurrentUser(Page $page): void
{
$this->queryForCurrentUserDraft($page->id)->delete();
$this->queries->latestCurrentUserDraftsForPageId($page->id)->delete();
}
/**
@ -53,7 +27,7 @@ class RevisionRepo
*/
public function getNewDraftForCurrentUser(Page $page): PageRevision
{
$draft = $this->getLatestDraftForCurrentUser($page);
$draft = $this->queries->findLatestCurrentUserDraftsForPageId($page->id);
if ($draft) {
return $draft;
@ -116,16 +90,4 @@ class RevisionRepo
PageRevision::query()->whereIn('id', $revisionsToDelete->pluck('id'))->delete();
}
}
/**
* Query update draft revisions for the current user.
*/
protected function queryForCurrentUserDraft(int $pageId): Builder
{
return PageRevision::query()
->where('created_by', '=', user()->id)
->where('type', 'update_draft')
->where('page_id', '=', $pageId)
->orderBy('created_at', 'desc');
}
}