@@ -15,18 +15,20 @@ class ChapterApiController extends ApiController
1515{
1616 protected $ rules = [
1717 'create ' => [
18- 'book_id ' => ['required ' , 'integer ' ],
19- 'name ' => ['required ' , 'string ' , 'max:255 ' ],
20- 'description ' => ['string ' , 'max:1000 ' ],
21- 'tags ' => ['array ' ],
22- 'priority ' => ['integer ' ],
18+ 'book_id ' => ['required ' , 'integer ' ],
19+ 'name ' => ['required ' , 'string ' , 'max:255 ' ],
20+ 'description ' => ['string ' , 'max:1900 ' ],
21+ 'description_html ' => ['string ' , 'max:2000 ' ],
22+ 'tags ' => ['array ' ],
23+ 'priority ' => ['integer ' ],
2324 ],
2425 'update ' => [
25- 'book_id ' => ['integer ' ],
26- 'name ' => ['string ' , 'min:1 ' , 'max:255 ' ],
27- 'description ' => ['string ' , 'max:1000 ' ],
28- 'tags ' => ['array ' ],
29- 'priority ' => ['integer ' ],
26+ 'book_id ' => ['integer ' ],
27+ 'name ' => ['string ' , 'min:1 ' , 'max:255 ' ],
28+ 'description ' => ['string ' , 'max:1900 ' ],
29+ 'description_html ' => ['string ' , 'max:2000 ' ],
30+ 'tags ' => ['array ' ],
31+ 'priority ' => ['integer ' ],
3032 ],
3133 ];
3234
@@ -61,17 +63,23 @@ public function create(Request $request)
6163
6264 $ chapter = $ this ->chapterRepo ->create ($ requestData , $ book );
6365
64- return response ()->json ($ chapter -> load ([ ' tags ' ] ));
66+ return response ()->json ($ this -> forJsonDisplay ( $ chapter ));
6567 }
6668
6769 /**
6870 * View the details of a single chapter.
6971 */
7072 public function read (string $ id )
7173 {
72- $ chapter = Chapter::visible ()->with (['tags ' , 'createdBy ' , 'updatedBy ' , 'ownedBy ' , 'pages ' => function (HasMany $ query ) {
73- $ query ->scopes ('visible ' )->get (['id ' , 'name ' , 'slug ' ]);
74- }])->findOrFail ($ id );
74+ $ chapter = Chapter::visible ()->findOrFail ($ id );
75+ $ chapter = $ this ->forJsonDisplay ($ chapter );
76+
77+ $ chapter ->load ([
78+ 'createdBy ' , 'updatedBy ' , 'ownedBy ' ,
79+ 'pages ' => function (HasMany $ query ) {
80+ $ query ->scopes ('visible ' )->get (['id ' , 'name ' , 'slug ' ]);
81+ }
82+ ]);
7583
7684 return response ()->json ($ chapter );
7785 }
@@ -93,7 +101,7 @@ public function update(Request $request, string $id)
93101 try {
94102 $ this ->chapterRepo ->move ($ chapter , "book: {$ requestData ['book_id ' ]}" );
95103 } catch (Exception $ exception ) {
96- if ($ exception instanceof PermissionsException) {
104+ if ($ exception instanceof PermissionsException) {
97105 $ this ->showPermissionError ();
98106 }
99107
@@ -103,7 +111,7 @@ public function update(Request $request, string $id)
103111
104112 $ updatedChapter = $ this ->chapterRepo ->update ($ chapter , $ requestData );
105113
106- return response ()->json ($ updatedChapter -> load ([ ' tags ' ] ));
114+ return response ()->json ($ this -> forJsonDisplay ( $ updatedChapter ));
107115 }
108116
109117 /**
@@ -119,4 +127,16 @@ public function delete(string $id)
119127
120128 return response ('' , 204 );
121129 }
130+
131+ protected function forJsonDisplay (Chapter $ chapter ): Chapter
132+ {
133+ $ chapter = clone $ chapter ;
134+ $ chapter ->unsetRelations ()->refresh ();
135+
136+ $ chapter ->load (['tags ' ]);
137+ $ chapter ->makeVisible ('description_html ' )
138+ ->setAttribute ('description_html ' , $ chapter ->descriptionHtml ());
139+
140+ return $ chapter ;
141+ }
122142}
0 commit comments