Dan Brown

Added better entity deletion and commented up repos

...@@ -42,7 +42,7 @@ class BookController extends Controller ...@@ -42,7 +42,7 @@ class BookController extends Controller
42 public function index() 42 public function index()
43 { 43 {
44 $books = $this->bookRepo->getAllPaginated(10); 44 $books = $this->bookRepo->getAllPaginated(10);
45 - $recents = $this->bookRepo->getRecentlyViewed(10, 0); 45 + $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
46 return view('books/index', ['books' => $books, 'recents' => $recents]); 46 return view('books/index', ['books' => $books, 'recents' => $recents]);
47 } 47 }
48 48
......
...@@ -146,15 +146,8 @@ class ChapterController extends Controller ...@@ -146,15 +146,8 @@ class ChapterController extends Controller
146 $this->checkPermission('chapter-delete'); 146 $this->checkPermission('chapter-delete');
147 $book = $this->bookRepo->getBySlug($bookSlug); 147 $book = $this->bookRepo->getBySlug($bookSlug);
148 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); 148 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
149 - if (count($chapter->pages) > 0) {
150 - foreach ($chapter->pages as $page) {
151 - $page->chapter_id = 0;
152 - $page->save();
153 - }
154 - }
155 - Activity::removeEntity($chapter);
156 Activity::addMessage('chapter_delete', $book->id, $chapter->name); 149 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
157 - $chapter->delete(); 150 + $this->chapterRepo->destroy($chapter);
158 return redirect($book->getUrl()); 151 return redirect($book->getUrl());
159 } 152 }
160 } 153 }
......
...@@ -34,8 +34,8 @@ class HomeController extends Controller ...@@ -34,8 +34,8 @@ class HomeController extends Controller
34 public function index() 34 public function index()
35 { 35 {
36 $activity = Activity::latest(); 36 $activity = Activity::latest();
37 - $recentlyViewed = Views::getUserRecentlyViewed(10, 0); 37 + $recents = $this->signedIn ? Views::getUserRecentlyViewed(10, 0) : $this->bookRepo->getLatest(10);
38 - return view('home', ['activity' => $activity, 'recents' => $recentlyViewed]); 38 + return view('home', ['activity' => $activity, 'recents' => $recents]);
39 } 39 }
40 40
41 } 41 }
......
...@@ -164,8 +164,7 @@ class PageController extends Controller ...@@ -164,8 +164,7 @@ class PageController extends Controller
164 $book = $this->bookRepo->getBySlug($bookSlug); 164 $book = $this->bookRepo->getBySlug($bookSlug);
165 $page = $this->pageRepo->getBySlug($pageSlug, $book->id); 165 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
166 Activity::addMessage('page_delete', $book->id, $page->name); 166 Activity::addMessage('page_delete', $book->id, $page->name);
167 - Activity::removeEntity($page); 167 + $this->pageRepo->destroy($page);
168 - $page->delete();
169 return redirect($book->getUrl()); 168 return redirect($book->getUrl());
170 } 169 }
171 170
......
1 <?php namespace BookStack\Repos; 1 <?php namespace BookStack\Repos;
2 2
3 -use BookStack\Activity; 3 +use Activity;
4 use Illuminate\Support\Str; 4 use Illuminate\Support\Str;
5 use BookStack\Book; 5 use BookStack\Book;
6 use Views; 6 use Views;
...@@ -10,16 +10,19 @@ class BookRepo ...@@ -10,16 +10,19 @@ class BookRepo
10 10
11 protected $book; 11 protected $book;
12 protected $pageRepo; 12 protected $pageRepo;
13 + protected $chapterRepo;
13 14
14 /** 15 /**
15 * BookRepo constructor. 16 * BookRepo constructor.
16 - * @param Book $book 17 + * @param Book $book
17 - * @param PageRepo $pageRepo 18 + * @param PageRepo $pageRepo
19 + * @param ChapterRepo $chapterRepo
18 */ 20 */
19 - public function __construct(Book $book, PageRepo $pageRepo) 21 + public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
20 { 22 {
21 $this->book = $book; 23 $this->book = $book;
22 $this->pageRepo = $pageRepo; 24 $this->pageRepo = $pageRepo;
25 + $this->chapterRepo = $chapterRepo;
23 } 26 }
24 27
25 /** 28 /**
...@@ -52,6 +55,23 @@ class BookRepo ...@@ -52,6 +55,23 @@ class BookRepo
52 return $this->book->orderBy('name', 'asc')->paginate($count); 55 return $this->book->orderBy('name', 'asc')->paginate($count);
53 } 56 }
54 57
58 +
59 + /**
60 + * Get the latest books.
61 + * @param int $count
62 + * @return mixed
63 + */
64 + public function getLatest($count = 10)
65 + {
66 + return $this->book->orderBy('created_at', 'desc')->take($count)->get();
67 + }
68 +
69 + /**
70 + * Gets the most recently viewed for a user.
71 + * @param int $count
72 + * @param int $page
73 + * @return mixed
74 + */
55 public function getRecentlyViewed($count = 10, $page = 0) 75 public function getRecentlyViewed($count = 10, $page = 0)
56 { 76 {
57 return Views::getUserRecentlyViewed($count, $page, $this->book); 77 return Views::getUserRecentlyViewed($count, $page, $this->book);
...@@ -105,13 +125,12 @@ class BookRepo ...@@ -105,13 +125,12 @@ class BookRepo
105 { 125 {
106 $book = $this->getBySlug($bookSlug); 126 $book = $this->getBySlug($bookSlug);
107 foreach ($book->pages as $page) { 127 foreach ($book->pages as $page) {
108 - \Activity::removeEntity($page); 128 + $this->pageRepo->destroy($page);
109 - $page->delete();
110 } 129 }
111 foreach ($book->chapters as $chapter) { 130 foreach ($book->chapters as $chapter) {
112 - \Activity::removeEntity($chapter); 131 + $this->chapterRepo->destroy($chapter);
113 - $chapter->delete();
114 } 132 }
133 + $book->views()->delete();
115 $book->delete(); 134 $book->delete();
116 } 135 }
117 136
......
1 <?php namespace BookStack\Repos; 1 <?php namespace BookStack\Repos;
2 2
3 3
4 +use Activity;
4 use Illuminate\Support\Str; 5 use Illuminate\Support\Str;
5 use BookStack\Chapter; 6 use BookStack\Chapter;
6 7
...@@ -18,37 +19,80 @@ class ChapterRepo ...@@ -18,37 +19,80 @@ class ChapterRepo
18 $this->chapter = $chapter; 19 $this->chapter = $chapter;
19 } 20 }
20 21
22 + /**
23 + * Check if an id exists.
24 + * @param $id
25 + * @return bool
26 + */
21 public function idExists($id) 27 public function idExists($id)
22 { 28 {
23 return $this->chapter->where('id', '=', $id)->count() > 0; 29 return $this->chapter->where('id', '=', $id)->count() > 0;
24 } 30 }
25 31
32 + /**
33 + * Get a chapter by a specific id.
34 + * @param $id
35 + * @return mixed
36 + */
26 public function getById($id) 37 public function getById($id)
27 { 38 {
28 return $this->chapter->findOrFail($id); 39 return $this->chapter->findOrFail($id);
29 } 40 }
30 41
42 + /**
43 + * Get all chapters.
44 + * @return \Illuminate\Database\Eloquent\Collection|static[]
45 + */
31 public function getAll() 46 public function getAll()
32 { 47 {
33 return $this->chapter->all(); 48 return $this->chapter->all();
34 } 49 }
35 50
51 + /**
52 + * Get a chapter that has the given slug within the given book.
53 + * @param $slug
54 + * @param $bookId
55 + * @return mixed
56 + */
36 public function getBySlug($slug, $bookId) 57 public function getBySlug($slug, $bookId)
37 { 58 {
38 return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); 59 return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
39 } 60 }
40 61
62 + /**
63 + * Create a new chapter from request input.
64 + * @param $input
65 + * @return $this
66 + */
41 public function newFromInput($input) 67 public function newFromInput($input)
42 { 68 {
43 return $this->chapter->fill($input); 69 return $this->chapter->fill($input);
44 } 70 }
45 71
46 - public function destroyById($id) 72 + /**
73 + * Destroy a chapter and its relations by providing its slug.
74 + * @param Chapter $chapter
75 + */
76 + public function destroy(Chapter $chapter)
47 { 77 {
48 - $page = $this->getById($id); 78 + if (count($chapter->pages) > 0) {
49 - $page->delete(); 79 + foreach ($chapter->pages as $page) {
80 + $page->chapter_id = 0;
81 + $page->save();
82 + }
83 + }
84 + Activity::removeEntity($chapter);
85 + $chapter->views()->delete();
86 + $chapter->delete();
50 } 87 }
51 88
89 + /**
90 + * Check if a chapter's slug exists.
91 + * @param $slug
92 + * @param $bookId
93 + * @param bool|false $currentId
94 + * @return bool
95 + */
52 public function doesSlugExist($slug, $bookId, $currentId = false) 96 public function doesSlugExist($slug, $bookId, $currentId = false)
53 { 97 {
54 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); 98 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
...@@ -58,6 +102,14 @@ class ChapterRepo ...@@ -58,6 +102,14 @@ class ChapterRepo
58 return $query->count() > 0; 102 return $query->count() > 0;
59 } 103 }
60 104
105 + /**
106 + * Finds a suitable slug for the provided name.
107 + * Checks database to prevent duplicate slugs.
108 + * @param $name
109 + * @param $bookId
110 + * @param bool|false $currentId
111 + * @return string
112 + */
61 public function findSuitableSlug($name, $bookId, $currentId = false) 113 public function findSuitableSlug($name, $bookId, $currentId = false)
62 { 114 {
63 $slug = Str::slug($name); 115 $slug = Str::slug($name);
...@@ -67,6 +119,12 @@ class ChapterRepo ...@@ -67,6 +119,12 @@ class ChapterRepo
67 return $slug; 119 return $slug;
68 } 120 }
69 121
122 + /**
123 + * Get chapters by the given search term.
124 + * @param $term
125 + * @param array $whereTerms
126 + * @return mixed
127 + */
70 public function getBySearch($term, $whereTerms = []) 128 public function getBySearch($term, $whereTerms = [])
71 { 129 {
72 $terms = explode(' ', preg_quote(trim($term))); 130 $terms = explode(' ', preg_quote(trim($term)));
...@@ -80,6 +138,12 @@ class ChapterRepo ...@@ -80,6 +138,12 @@ class ChapterRepo
80 return $chapters; 138 return $chapters;
81 } 139 }
82 140
141 + /**
142 + * Sets a chapters book id.
143 + * @param $bookId
144 + * @param Chapter $chapter
145 + * @return Chapter
146 + */
83 public function setBookId($bookId, Chapter $chapter) 147 public function setBookId($bookId, Chapter $chapter)
84 { 148 {
85 $chapter->book_id = $bookId; 149 $chapter->book_id = $bookId;
......
1 <?php namespace BookStack\Repos; 1 <?php namespace BookStack\Repos;
2 2
3 3
4 +use Activity;
4 use BookStack\Book; 5 use BookStack\Book;
5 use BookStack\Chapter; 6 use BookStack\Chapter;
6 use Illuminate\Http\Request; 7 use Illuminate\Http\Request;
...@@ -26,21 +27,41 @@ class PageRepo ...@@ -26,21 +27,41 @@ class PageRepo
26 $this->pageRevision = $pageRevision; 27 $this->pageRevision = $pageRevision;
27 } 28 }
28 29
30 + /**
31 + * Check if a page id exists.
32 + * @param $id
33 + * @return bool
34 + */
29 public function idExists($id) 35 public function idExists($id)
30 { 36 {
31 return $this->page->where('page_id', '=', $id)->count() > 0; 37 return $this->page->where('page_id', '=', $id)->count() > 0;
32 } 38 }
33 39
40 + /**
41 + * Get a page via a specific ID.
42 + * @param $id
43 + * @return mixed
44 + */
34 public function getById($id) 45 public function getById($id)
35 { 46 {
36 return $this->page->findOrFail($id); 47 return $this->page->findOrFail($id);
37 } 48 }
38 49
50 + /**
51 + * Get all pages.
52 + * @return \Illuminate\Database\Eloquent\Collection|static[]
53 + */
39 public function getAll() 54 public function getAll()
40 { 55 {
41 return $this->page->all(); 56 return $this->page->all();
42 } 57 }
43 58
59 + /**
60 + * Get a page identified by the given slug.
61 + * @param $slug
62 + * @param $bookId
63 + * @return mixed
64 + */
44 public function getBySlug($slug, $bookId) 65 public function getBySlug($slug, $bookId)
45 { 66 {
46 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); 67 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
...@@ -56,6 +77,12 @@ class PageRepo ...@@ -56,6 +77,12 @@ class PageRepo
56 return $page; 77 return $page;
57 } 78 }
58 79
80 + /**
81 + * Count the pages with a particular slug within a book.
82 + * @param $slug
83 + * @param $bookId
84 + * @return mixed
85 + */
59 public function countBySlug($slug, $bookId) 86 public function countBySlug($slug, $bookId)
60 { 87 {
61 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count(); 88 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
...@@ -137,12 +164,14 @@ class PageRepo ...@@ -137,12 +164,14 @@ class PageRepo
137 return $html; 164 return $html;
138 } 165 }
139 166
140 - public function destroyById($id)
141 - {
142 - $page = $this->getById($id);
143 - $page->delete();
144 - }
145 167
168 + /**
169 + * Gets pages by a search term.
170 + * Highlights page content for showing in results.
171 + * @param string $term
172 + * @param array $whereTerms
173 + * @return mixed
174 + */
146 public function getBySearch($term, $whereTerms = []) 175 public function getBySearch($term, $whereTerms = [])
147 { 176 {
148 $terms = explode(' ', preg_quote(trim($term))); 177 $terms = explode(' ', preg_quote(trim($term)));
...@@ -299,7 +328,6 @@ class PageRepo ...@@ -299,7 +328,6 @@ class PageRepo
299 328
300 /** 329 /**
301 * Gets a suitable slug for the resource 330 * Gets a suitable slug for the resource
302 - *
303 * @param $name 331 * @param $name
304 * @param $bookId 332 * @param $bookId
305 * @param bool|false $currentId 333 * @param bool|false $currentId
...@@ -314,5 +342,17 @@ class PageRepo ...@@ -314,5 +342,17 @@ class PageRepo
314 return $slug; 342 return $slug;
315 } 343 }
316 344
345 + /**
346 + * Destroy a given page along with its dependencies.
347 + * @param $page
348 + */
349 + public function destroy($page)
350 + {
351 + Activity::removeEntity($page);
352 + $page->views()->delete();
353 + $page->revisions()->delete();
354 + $page->delete();
355 + }
356 +
317 357
318 } 358 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -35,8 +35,10 @@ ...@@ -35,8 +35,10 @@
35 </div> 35 </div>
36 <div class="col-sm-4 col-sm-offset-1"> 36 <div class="col-sm-4 col-sm-offset-1">
37 <div class="margin-top large">&nbsp;</div> 37 <div class="margin-top large">&nbsp;</div>
38 - <h3>Recently Viewed</h3> 38 + @if($recents)
39 - @include('partials/entity-list', ['entities' => $recents]) 39 + <h3>Recently Viewed</h3>
40 + @include('partials/entity-list', ['entities' => $recents])
41 + @endif
40 </div> 42 </div>
41 </div> 43 </div>
42 </div> 44 </div>
......
...@@ -6,7 +6,11 @@ ...@@ -6,7 +6,11 @@
6 <div class="row"> 6 <div class="row">
7 7
8 <div class="col-md-7"> 8 <div class="col-md-7">
9 - <h2>My Recently Viewed</h2> 9 + @if($signedIn)
10 + <h2>My Recently Viewed</h2>
11 + @else
12 + <h2>Recent Books</h2>
13 + @endif
10 @include('partials/entity-list', ['entities' => $recents]) 14 @include('partials/entity-list', ['entities' => $recents])
11 </div> 15 </div>
12 16
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
12 @endforeach 12 @endforeach
13 @else 13 @else
14 <p class="text-muted"> 14 <p class="text-muted">
15 - No items available :( 15 + No items available
16 </p> 16 </p>
17 @endif 17 @endif
...\ No newline at end of file ...\ No newline at end of file
......