Dan Brown

Prevent duplicate slugs on sort

...@@ -185,7 +185,7 @@ class BookController extends Controller ...@@ -185,7 +185,7 @@ class BookController extends Controller
185 $isPage = $bookChild->type == 'page'; 185 $isPage = $bookChild->type == 'page';
186 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId; 186 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
187 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id); 187 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
188 - $isPage ? $this->pageRepo->setBookId($bookId, $model) : $this->chapterRepo->setBookId($bookId, $model); 188 + $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
189 $model->priority = $index; 189 $model->priority = $index;
190 if ($isPage) { 190 if ($isPage) {
191 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter; 191 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
......
...@@ -96,7 +96,7 @@ class ChapterRepo ...@@ -96,7 +96,7 @@ class ChapterRepo
96 public function doesSlugExist($slug, $bookId, $currentId = false) 96 public function doesSlugExist($slug, $bookId, $currentId = false)
97 { 97 {
98 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); 98 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
99 - if($currentId) { 99 + if ($currentId) {
100 $query = $query->where('id', '!=', $currentId); 100 $query = $query->where('id', '!=', $currentId);
101 } 101 }
102 return $query->count() > 0; 102 return $query->count() > 0;
...@@ -113,7 +113,7 @@ class ChapterRepo ...@@ -113,7 +113,7 @@ class ChapterRepo
113 public function findSuitableSlug($name, $bookId, $currentId = false) 113 public function findSuitableSlug($name, $bookId, $currentId = false)
114 { 114 {
115 $slug = Str::slug($name); 115 $slug = Str::slug($name);
116 - while($this->doesSlugExist($slug, $bookId, $currentId)) { 116 + while ($this->doesSlugExist($slug, $bookId, $currentId)) {
117 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); 117 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
118 } 118 }
119 return $slug; 119 return $slug;
...@@ -139,18 +139,19 @@ class ChapterRepo ...@@ -139,18 +139,19 @@ class ChapterRepo
139 } 139 }
140 140
141 /** 141 /**
142 - * Sets a chapters book id. 142 + * Changes the book relation of this chapter.
143 * @param $bookId 143 * @param $bookId
144 * @param Chapter $chapter 144 * @param Chapter $chapter
145 * @return Chapter 145 * @return Chapter
146 */ 146 */
147 - public function setBookId($bookId, Chapter $chapter) 147 + public function changeBook($bookId, Chapter $chapter)
148 { 148 {
149 $chapter->book_id = $bookId; 149 $chapter->book_id = $bookId;
150 - foreach($chapter->activity as $activity) { 150 + foreach ($chapter->activity as $activity) {
151 $activity->book_id = $bookId; 151 $activity->book_id = $bookId;
152 $activity->save(); 152 $activity->save();
153 } 153 }
154 + $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
154 $chapter->save(); 155 $chapter->save();
155 return $chapter; 156 return $chapter;
156 } 157 }
......
...@@ -309,19 +309,20 @@ class PageRepo ...@@ -309,19 +309,20 @@ class PageRepo
309 } 309 }
310 310
311 /** 311 /**
312 - * Sets the book id for the specified page. 312 + * Changes the related book for the specified page.
313 * Changes the book id of any relations to the page that store the book id. 313 * Changes the book id of any relations to the page that store the book id.
314 * @param int $bookId 314 * @param int $bookId
315 * @param Page $page 315 * @param Page $page
316 * @return Page 316 * @return Page
317 */ 317 */
318 - public function setBookId($bookId, Page $page) 318 + public function changeBook($bookId, Page $page)
319 { 319 {
320 $page->book_id = $bookId; 320 $page->book_id = $bookId;
321 foreach ($page->activity as $activity) { 321 foreach ($page->activity as $activity) {
322 $activity->book_id = $bookId; 322 $activity->book_id = $bookId;
323 $activity->save(); 323 $activity->save();
324 } 324 }
325 + $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
325 $page->save(); 326 $page->save();
326 return $page; 327 return $page;
327 } 328 }
......