2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2020-05-22 19:28:41 -04:00
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
2020-05-22 19:28:41 -04:00
|
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
2023-05-30 15:55:24 -04:00
|
|
|
use BookStack\Exceptions\PermissionsException;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\ApiController;
|
2023-05-30 15:55:24 -04:00
|
|
|
use Exception;
|
2020-05-22 19:28:41 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class ChapterApiController extends ApiController
|
|
|
|
{
|
|
|
|
protected $rules = [
|
|
|
|
'create' => [
|
2021-11-04 20:26:55 -04:00
|
|
|
'book_id' => ['required', 'integer'],
|
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
'description' => ['string', 'max:1000'],
|
|
|
|
'tags' => ['array'],
|
2023-06-12 09:12:46 -04:00
|
|
|
'priority' => ['integer'],
|
2020-05-22 19:28:41 -04:00
|
|
|
],
|
|
|
|
'update' => [
|
2021-11-04 20:26:55 -04:00
|
|
|
'book_id' => ['integer'],
|
|
|
|
'name' => ['string', 'min:1', 'max:255'],
|
|
|
|
'description' => ['string', 'max:1000'],
|
|
|
|
'tags' => ['array'],
|
2023-06-12 09:12:46 -04:00
|
|
|
'priority' => ['integer'],
|
2020-05-22 19:28:41 -04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2023-05-30 15:55:24 -04:00
|
|
|
public function __construct(
|
|
|
|
protected ChapterRepo $chapterRepo
|
|
|
|
) {
|
2020-05-22 19:28:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a listing of chapters visible to the user.
|
|
|
|
*/
|
|
|
|
public function list()
|
|
|
|
{
|
|
|
|
$chapters = Chapter::visible();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-05-22 19:28:41 -04:00
|
|
|
return $this->apiListingResponse($chapters, [
|
|
|
|
'id', 'book_id', 'name', 'slug', 'description', 'priority',
|
2021-01-03 17:29:58 -05:00
|
|
|
'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
|
2020-05-22 19:28:41 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new chapter in the system.
|
|
|
|
*/
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
2023-05-30 15:55:24 -04:00
|
|
|
$requestData = $this->validate($request, $this->rules['create']);
|
2020-05-22 19:28:41 -04:00
|
|
|
|
|
|
|
$bookId = $request->get('book_id');
|
|
|
|
$book = Book::visible()->findOrFail($bookId);
|
|
|
|
$this->checkOwnablePermission('chapter-create', $book);
|
|
|
|
|
2023-05-30 15:55:24 -04:00
|
|
|
$chapter = $this->chapterRepo->create($requestData, $book);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-05-22 19:28:41 -04:00
|
|
|
return response()->json($chapter->load(['tags']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View the details of a single chapter.
|
|
|
|
*/
|
|
|
|
public function read(string $id)
|
|
|
|
{
|
2021-01-03 17:29:58 -05:00
|
|
|
$chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) {
|
2021-11-22 18:33:55 -05:00
|
|
|
$query->scopes('visible')->get(['id', 'name', 'slug']);
|
2020-05-22 19:28:41 -04:00
|
|
|
}])->findOrFail($id);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-05-22 19:28:41 -04:00
|
|
|
return response()->json($chapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the details of a single chapter.
|
2023-05-30 15:55:24 -04:00
|
|
|
* Providing a 'book_id' property will essentially move the chapter
|
|
|
|
* into that parent element if you have permissions to do so.
|
2020-05-22 19:28:41 -04:00
|
|
|
*/
|
|
|
|
public function update(Request $request, string $id)
|
|
|
|
{
|
2023-05-30 15:55:24 -04:00
|
|
|
$requestData = $this->validate($request, $this->rules()['update']);
|
2020-05-22 19:28:41 -04:00
|
|
|
$chapter = Chapter::visible()->findOrFail($id);
|
|
|
|
$this->checkOwnablePermission('chapter-update', $chapter);
|
|
|
|
|
2023-05-30 15:55:24 -04:00
|
|
|
if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
|
|
|
|
$this->checkOwnablePermission('chapter-delete', $chapter);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception instanceof PermissionsException) {
|
|
|
|
$this->showPermissionError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->jsonError(trans('errors.selected_book_not_found'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$updatedChapter = $this->chapterRepo->update($chapter, $requestData);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-05-22 19:28:41 -04:00
|
|
|
return response()->json($updatedChapter->load(['tags']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-28 10:21:54 -05:00
|
|
|
* Delete a chapter.
|
|
|
|
* This will typically send the chapter to the recycle bin.
|
2020-05-22 19:28:41 -04:00
|
|
|
*/
|
|
|
|
public function delete(string $id)
|
|
|
|
{
|
|
|
|
$chapter = Chapter::visible()->findOrFail($id);
|
|
|
|
$this->checkOwnablePermission('chapter-delete', $chapter);
|
|
|
|
|
|
|
|
$this->chapterRepo->destroy($chapter);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-05-22 19:28:41 -04:00
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
}
|