]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookshelfApiController.php
Input WYSIWYG: Aligned newline handling with old descriptions
[bookstack] / app / Entities / Controllers / BookshelfApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Http\ApiController;
8 use Exception;
9 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
12
13 class BookshelfApiController extends ApiController
14 {
15     protected BookshelfRepo $bookshelfRepo;
16
17     public function __construct(BookshelfRepo $bookshelfRepo)
18     {
19         $this->bookshelfRepo = $bookshelfRepo;
20     }
21
22     /**
23      * Get a listing of shelves visible to the user.
24      */
25     public function list()
26     {
27         $shelves = Bookshelf::visible();
28
29         return $this->apiListingResponse($shelves, [
30             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
31         ]);
32     }
33
34     /**
35      * Create a new shelf in the system.
36      * An array of books IDs can be provided in the request. These
37      * will be added to the shelf in the same order as provided.
38      * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
39      * If the 'image' property is null then the shelf cover image will be removed.
40      *
41      * @throws ValidationException
42      */
43     public function create(Request $request)
44     {
45         $this->checkPermission('bookshelf-create-all');
46         $requestData = $this->validate($request, $this->rules()['create']);
47
48         $bookIds = $request->get('books', []);
49         $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
50
51         return response()->json($shelf);
52     }
53
54     /**
55      * View the details of a single shelf.
56      */
57     public function read(string $id)
58     {
59         $shelf = Bookshelf::visible()->with([
60             'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
61             'books' => function (BelongsToMany $query) {
62                 $query->scopes('visible')->get(['id', 'name', 'slug']);
63             },
64         ])->findOrFail($id);
65
66         return response()->json($shelf);
67     }
68
69     /**
70      * Update the details of a single shelf.
71      * An array of books IDs can be provided in the request. These
72      * will be added to the shelf in the same order as provided and overwrite
73      * any existing book assignments.
74      * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
75      * If the 'image' property is null then the shelf cover image will be removed.
76      *
77      * @throws ValidationException
78      */
79     public function update(Request $request, string $id)
80     {
81         $shelf = Bookshelf::visible()->findOrFail($id);
82         $this->checkOwnablePermission('bookshelf-update', $shelf);
83
84         $requestData = $this->validate($request, $this->rules()['update']);
85         $bookIds = $request->get('books', null);
86
87         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
88
89         return response()->json($shelf);
90     }
91
92     /**
93      * Delete a single shelf.
94      * This will typically send the shelf to the recycle bin.
95      *
96      * @throws Exception
97      */
98     public function delete(string $id)
99     {
100         $shelf = Bookshelf::visible()->findOrFail($id);
101         $this->checkOwnablePermission('bookshelf-delete', $shelf);
102
103         $this->bookshelfRepo->destroy($shelf);
104
105         return response('', 204);
106     }
107
108     protected function rules(): array
109     {
110         return [
111             'create' => [
112                 'name'        => ['required', 'string', 'max:255'],
113                 'description' => ['string', 'max:1000'],
114                 'books'       => ['array'],
115                 'tags'        => ['array'],
116                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
117             ],
118             'update' => [
119                 'name'        => ['string', 'min:1', 'max:255'],
120                 'description' => ['string', 'max:1000'],
121                 'books'       => ['array'],
122                 'tags'        => ['array'],
123                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
124             ],
125         ];
126     }
127 }