| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Http\Controllers\Api; | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  | use BookStack\Entities\Repos\ChapterRepo; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChapterApiController extends ApiController | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $chapterRepo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $rules = [ | 
					
						
							|  |  |  |         'create' => [ | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'book_id'     => ['required', 'integer'], | 
					
						
							|  |  |  |             'name'        => ['required', 'string', 'max:255'], | 
					
						
							|  |  |  |             'description' => ['string', 'max:1000'], | 
					
						
							|  |  |  |             'tags'        => ['array'], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ], | 
					
						
							|  |  |  |         'update' => [ | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'book_id'     => ['integer'], | 
					
						
							|  |  |  |             'name'        => ['string', 'min:1', 'max:255'], | 
					
						
							|  |  |  |             'description' => ['string', 'max:1000'], | 
					
						
							|  |  |  |             'tags'        => ['array'], | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ], | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * ChapterController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(ChapterRepo $chapterRepo) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->chapterRepo = $chapterRepo; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a listing of chapters visible to the user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function list() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapters = Chapter::visible(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return $this->apiListingResponse($chapters, [ | 
					
						
							|  |  |  |             'id', 'book_id', 'name', 'slug', 'description', 'priority', | 
					
						
							| 
									
										
										
										
											2021-01-04 06:29:58 +08:00
										 |  |  |             'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new chapter in the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->validate($request, $this->rules['create']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookId = $request->get('book_id'); | 
					
						
							|  |  |  |         $book = Book::visible()->findOrFail($bookId); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('chapter-create', $book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $chapter = $this->chapterRepo->create($request->all(), $book); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return response()->json($chapter->load(['tags'])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * View the details of a single chapter. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function read(string $id) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-01-04 06:29:58 +08:00
										 |  |  |         $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) { | 
					
						
							| 
									
										
										
										
											2021-11-23 07:33:55 +08:00
										 |  |  |             $query->scopes('visible')->get(['id', 'name', 'slug']); | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         }])->findOrFail($id); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return response()->json($chapter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the details of a single chapter. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Request $request, string $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::visible()->findOrFail($id); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('chapter-update', $chapter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $updatedChapter = $this->chapterRepo->update($chapter, $request->all()); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return response()->json($updatedChapter->load(['tags'])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-11-28 23:21:54 +08:00
										 |  |  |      * Delete a chapter. | 
					
						
							|  |  |  |      * This will typically send the chapter to the recycle bin. | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function delete(string $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::visible()->findOrFail($id); | 
					
						
							|  |  |  |         $this->checkOwnablePermission('chapter-delete', $chapter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->chapterRepo->destroy($chapter); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 07:28:41 +08:00
										 |  |  |         return response('', 204); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |