]> BookStack Code Mirror - bookstack/blob - app/Repos/BookRepo.php
Made chapters functional and cleaned design features
[bookstack] / app / Repos / BookRepo.php
1 <?php namespace Oxbow\Repos;
2
3 use Oxbow\Book;
4
5 class BookRepo
6 {
7
8     protected $book;
9     protected $pageRepo;
10
11     /**
12      * BookRepo constructor.
13      * @param Book $book
14      * @param PageRepo $pageRepo
15      */
16     public function __construct(Book $book, PageRepo $pageRepo)
17     {
18         $this->book = $book;
19         $this->pageRepo = $pageRepo;
20     }
21
22     public function getById($id)
23     {
24         return $this->book->findOrFail($id);
25     }
26
27     public function getAll()
28     {
29         return $this->book->all();
30     }
31
32     public function getBySlug($slug)
33     {
34         return $this->book->where('slug', '=', $slug)->first();
35     }
36
37     public function newFromInput($input)
38     {
39         return $this->book->fill($input);
40     }
41
42     public function countBySlug($slug)
43     {
44         return $this->book->where('slug', '=', $slug)->count();
45     }
46
47     public function destroyBySlug($bookSlug)
48     {
49         $book = $this->getBySlug($bookSlug);
50         foreach($book->children() as $child) {
51             $child->delete();
52         }
53         $book->delete();
54     }
55
56     public function getNewPriority($book)
57     {
58         $lastElem = $book->children()->pop();
59         return $lastElem ? $lastElem->priority + 1 : 0;
60     }
61
62 }