Added better entity deletion and commented up repos

This commit is contained in:
Dan Brown 2015-11-21 18:05:03 +00:00
parent ea55b7f141
commit 61ae96c5f8
10 changed files with 155 additions and 34 deletions

View File

@ -42,7 +42,7 @@ class BookController extends Controller
public function index()
{
$books = $this->bookRepo->getAllPaginated(10);
$recents = $this->bookRepo->getRecentlyViewed(10, 0);
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
return view('books/index', ['books' => $books, 'recents' => $recents]);
}

View File

@ -146,15 +146,8 @@ class ChapterController extends Controller
$this->checkPermission('chapter-delete');
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
if (count($chapter->pages) > 0) {
foreach ($chapter->pages as $page) {
$page->chapter_id = 0;
$page->save();
}
}
Activity::removeEntity($chapter);
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
$chapter->delete();
$this->chapterRepo->destroy($chapter);
return redirect($book->getUrl());
}
}

View File

@ -34,8 +34,8 @@ class HomeController extends Controller
public function index()
{
$activity = Activity::latest();
$recentlyViewed = Views::getUserRecentlyViewed(10, 0);
return view('home', ['activity' => $activity, 'recents' => $recentlyViewed]);
$recents = $this->signedIn ? Views::getUserRecentlyViewed(10, 0) : $this->bookRepo->getLatest(10);
return view('home', ['activity' => $activity, 'recents' => $recents]);
}
}

View File

@ -164,8 +164,7 @@ class PageController extends Controller
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
Activity::addMessage('page_delete', $book->id, $page->name);
Activity::removeEntity($page);
$page->delete();
$this->pageRepo->destroy($page);
return redirect($book->getUrl());
}

View File

@ -1,6 +1,6 @@
<?php namespace BookStack\Repos;
use BookStack\Activity;
use Activity;
use Illuminate\Support\Str;
use BookStack\Book;
use Views;
@ -10,16 +10,19 @@ class BookRepo
protected $book;
protected $pageRepo;
protected $chapterRepo;
/**
* BookRepo constructor.
* @param Book $book
* @param PageRepo $pageRepo
* @param Book $book
* @param PageRepo $pageRepo
* @param ChapterRepo $chapterRepo
*/
public function __construct(Book $book, PageRepo $pageRepo)
public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
{
$this->book = $book;
$this->pageRepo = $pageRepo;
$this->chapterRepo = $chapterRepo;
}
/**
@ -52,6 +55,23 @@ class BookRepo
return $this->book->orderBy('name', 'asc')->paginate($count);
}
/**
* Get the latest books.
* @param int $count
* @return mixed
*/
public function getLatest($count = 10)
{
return $this->book->orderBy('created_at', 'desc')->take($count)->get();
}
/**
* Gets the most recently viewed for a user.
* @param int $count
* @param int $page
* @return mixed
*/
public function getRecentlyViewed($count = 10, $page = 0)
{
return Views::getUserRecentlyViewed($count, $page, $this->book);
@ -105,13 +125,12 @@ class BookRepo
{
$book = $this->getBySlug($bookSlug);
foreach ($book->pages as $page) {
\Activity::removeEntity($page);
$page->delete();
$this->pageRepo->destroy($page);
}
foreach ($book->chapters as $chapter) {
\Activity::removeEntity($chapter);
$chapter->delete();
$this->chapterRepo->destroy($chapter);
}
$book->views()->delete();
$book->delete();
}

View File

@ -1,6 +1,7 @@
<?php namespace BookStack\Repos;
use Activity;
use Illuminate\Support\Str;
use BookStack\Chapter;
@ -18,37 +19,80 @@ class ChapterRepo
$this->chapter = $chapter;
}
/**
* Check if an id exists.
* @param $id
* @return bool
*/
public function idExists($id)
{
return $this->chapter->where('id', '=', $id)->count() > 0;
}
/**
* Get a chapter by a specific id.
* @param $id
* @return mixed
*/
public function getById($id)
{
return $this->chapter->findOrFail($id);
}
/**
* Get all chapters.
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getAll()
{
return $this->chapter->all();
}
/**
* Get a chapter that has the given slug within the given book.
* @param $slug
* @param $bookId
* @return mixed
*/
public function getBySlug($slug, $bookId)
{
return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
}
/**
* Create a new chapter from request input.
* @param $input
* @return $this
*/
public function newFromInput($input)
{
return $this->chapter->fill($input);
}
public function destroyById($id)
/**
* Destroy a chapter and its relations by providing its slug.
* @param Chapter $chapter
*/
public function destroy(Chapter $chapter)
{
$page = $this->getById($id);
$page->delete();
if (count($chapter->pages) > 0) {
foreach ($chapter->pages as $page) {
$page->chapter_id = 0;
$page->save();
}
}
Activity::removeEntity($chapter);
$chapter->views()->delete();
$chapter->delete();
}
/**
* Check if a chapter's slug exists.
* @param $slug
* @param $bookId
* @param bool|false $currentId
* @return bool
*/
public function doesSlugExist($slug, $bookId, $currentId = false)
{
$query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
@ -58,6 +102,14 @@ class ChapterRepo
return $query->count() > 0;
}
/**
* Finds a suitable slug for the provided name.
* Checks database to prevent duplicate slugs.
* @param $name
* @param $bookId
* @param bool|false $currentId
* @return string
*/
public function findSuitableSlug($name, $bookId, $currentId = false)
{
$slug = Str::slug($name);
@ -67,6 +119,12 @@ class ChapterRepo
return $slug;
}
/**
* Get chapters by the given search term.
* @param $term
* @param array $whereTerms
* @return mixed
*/
public function getBySearch($term, $whereTerms = [])
{
$terms = explode(' ', preg_quote(trim($term)));
@ -80,6 +138,12 @@ class ChapterRepo
return $chapters;
}
/**
* Sets a chapters book id.
* @param $bookId
* @param Chapter $chapter
* @return Chapter
*/
public function setBookId($bookId, Chapter $chapter)
{
$chapter->book_id = $bookId;

View File

@ -1,6 +1,7 @@
<?php namespace BookStack\Repos;
use Activity;
use BookStack\Book;
use BookStack\Chapter;
use Illuminate\Http\Request;
@ -26,21 +27,41 @@ class PageRepo
$this->pageRevision = $pageRevision;
}
/**
* Check if a page id exists.
* @param $id
* @return bool
*/
public function idExists($id)
{
return $this->page->where('page_id', '=', $id)->count() > 0;
}
/**
* Get a page via a specific ID.
* @param $id
* @return mixed
*/
public function getById($id)
{
return $this->page->findOrFail($id);
}
/**
* Get all pages.
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getAll()
{
return $this->page->all();
}
/**
* Get a page identified by the given slug.
* @param $slug
* @param $bookId
* @return mixed
*/
public function getBySlug($slug, $bookId)
{
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
@ -56,6 +77,12 @@ class PageRepo
return $page;
}
/**
* Count the pages with a particular slug within a book.
* @param $slug
* @param $bookId
* @return mixed
*/
public function countBySlug($slug, $bookId)
{
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
@ -137,12 +164,14 @@ class PageRepo
return $html;
}
public function destroyById($id)
{
$page = $this->getById($id);
$page->delete();
}
/**
* Gets pages by a search term.
* Highlights page content for showing in results.
* @param string $term
* @param array $whereTerms
* @return mixed
*/
public function getBySearch($term, $whereTerms = [])
{
$terms = explode(' ', preg_quote(trim($term)));
@ -299,7 +328,6 @@ class PageRepo
/**
* Gets a suitable slug for the resource
*
* @param $name
* @param $bookId
* @param bool|false $currentId
@ -314,5 +342,17 @@ class PageRepo
return $slug;
}
/**
* Destroy a given page along with its dependencies.
* @param $page
*/
public function destroy($page)
{
Activity::removeEntity($page);
$page->views()->delete();
$page->revisions()->delete();
$page->delete();
}
}

View File

@ -35,8 +35,10 @@
</div>
<div class="col-sm-4 col-sm-offset-1">
<div class="margin-top large">&nbsp;</div>
<h3>Recently Viewed</h3>
@include('partials/entity-list', ['entities' => $recents])
@if($recents)
<h3>Recently Viewed</h3>
@include('partials/entity-list', ['entities' => $recents])
@endif
</div>
</div>
</div>

View File

@ -6,7 +6,11 @@
<div class="row">
<div class="col-md-7">
<h2>My Recently Viewed</h2>
@if($signedIn)
<h2>My Recently Viewed</h2>
@else
<h2>Recent Books</h2>
@endif
@include('partials/entity-list', ['entities' => $recents])
</div>

View File

@ -12,6 +12,6 @@
@endforeach
@else
<p class="text-muted">
No items available :(
No items available
</p>
@endif