DiscussionController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Discussion;
  4. use App\Models\Wbw;
  5. use App\Models\WbwBlock;
  6. use App\Models\PaliSentence;
  7. use App\Models\Sentence;
  8. use Illuminate\Http\Request;
  9. use App\Http\Resources\DiscussionResource;
  10. use App\Http\Api\MdRender;
  11. use App\Http\Api\AuthApi;
  12. use App\Http\Api\Mq;
  13. class DiscussionController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. switch ($request->get('view')) {
  24. case 'question-by-topic':
  25. $topic = Discussion::where('id',$request->get('id'))->select('res_id')->first();
  26. if(!$topic){
  27. return $this->error("无效的id");
  28. }
  29. $table = Discussion::where('res_id',$topic->res_id)->where('parent',null);
  30. break;
  31. case 'question':
  32. $table = Discussion::where('res_id',$request->get('id'))->where('parent',null);
  33. break;
  34. case 'answer':
  35. $table = Discussion::where('parent',$request->get('id'));
  36. break;
  37. case 'all':
  38. $table = Discussion::where('parent',null);
  39. break;
  40. }
  41. if(!empty($search)){
  42. $table->where('title', 'like', $search."%");
  43. }
  44. $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
  45. $count = $table->count();
  46. $table->skip($request->get("offset",0))
  47. ->take($request->get('limit',1000));
  48. $result = $table->get();
  49. if($result){
  50. return $this->ok(["rows"=>DiscussionResource::collection($result),"count"=>$count]);
  51. }else{
  52. return $this->error("没有查询到数据");
  53. }
  54. }
  55. public function discussion_tree(Request $request){
  56. $output = [];
  57. $sentences = $request->get("data");
  58. foreach ($sentences as $key => $sentence) {
  59. # 先查句子信息
  60. $sentInfo = Sentence::where('book_id',$sentence['book'])
  61. ->where('paragraph',$sentence['paragraph'])
  62. ->where('word_start',$sentence['word_start'])
  63. ->where('word_end',$sentence['word_end'])
  64. ->where('channel_uid',$sentence['channel_id'])
  65. ->first();
  66. if($sentInfo){
  67. $sentPr = Discussion::where('res_id',$sentInfo['uid'])
  68. ->whereNull('parent')
  69. ->select('title','children_count','editor_uid')
  70. ->orderBy('created_at','desc')->get();
  71. $output[] = [
  72. 'sentence' => [
  73. 'book' => $sentInfo->book_id,
  74. 'paragraph' => $sentInfo->paragraph,
  75. 'word_start' => $sentInfo->word_start,
  76. 'word_end' => $sentInfo->word_end,
  77. 'channel_id' => $sentInfo->channel_uid,
  78. 'content' => $sentInfo->content,
  79. 'pr_count' => count($sentPr),
  80. ],
  81. 'pr' => $sentPr,
  82. ];
  83. }
  84. }
  85. return $this->ok(['rows'=>$output,'count'=>count($output)]);
  86. }
  87. /**
  88. * Store a newly created resource in storage.
  89. *
  90. * @param \Illuminate\Http\Request $request
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function store(Request $request)
  94. {
  95. $user = AuthApi::current($request);
  96. if(!$user){
  97. return $this->error(__('auth.failed'));
  98. }
  99. //
  100. // validate
  101. // read more on validation at http://laravel.com/docs/validation
  102. if($request->has('parent')){
  103. $rules = [];
  104. $parentInfo = Discussion::find($request->get('parent'));
  105. if(!$parentInfo){
  106. return $this->error('no record');
  107. }
  108. }else{
  109. $rules = array(
  110. 'res_id' => 'required',
  111. 'res_type' => 'required',
  112. 'title' => 'required',
  113. );
  114. }
  115. $validated = $request->validate($rules);
  116. $discussion = new Discussion;
  117. if($request->has('parent')){
  118. $discussion->res_id = $parentInfo->res_id;
  119. $discussion->res_type = $parentInfo->res_type;
  120. }else{
  121. $discussion->res_id = $request->get('res_id');
  122. $discussion->res_type = $request->get('res_type');
  123. }
  124. $discussion->title = $request->get('title',null);
  125. $discussion->content = $request->get('content',null);
  126. $discussion->content_type = $request->get('content_type',"markdown");
  127. $discussion->parent = $request->get('parent',null);
  128. $discussion->editor_uid = $user['user_uid'];
  129. $discussion->save();
  130. //更新parent children_count
  131. if($request->has('parent')){
  132. $parentInfo->increment('children_count',1);
  133. $parentInfo->save();
  134. }
  135. Mq::publish('discussion',new DiscussionResource($discussion));
  136. return $this->ok(new DiscussionResource($discussion));
  137. }
  138. /**
  139. * Display the specified resource.
  140. *
  141. * @param \App\Models\Discussion $discussion
  142. * @return \Illuminate\Http\Response
  143. */
  144. public function show(Discussion $discussion)
  145. {
  146. //
  147. return $this->ok(new DiscussionResource($discussion));
  148. }
  149. /**
  150. * 获取discussion 锚点的数据。以句子为最小单位,逐词解析也要显示单词所在的句子
  151. *
  152. * @param string $id
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function anchor($id)
  156. {
  157. //
  158. $discussion = Discussion::find($id);
  159. switch ($discussion->res_type) {
  160. case 'wbw':
  161. # 从逐词解析表获取逐词解析数据
  162. $wbw = Wbw::where('uid',$discussion->res_id)->first();
  163. if(!$wbw){
  164. return $this->error('no wbw data');
  165. }
  166. $wbwBlock = WbwBlock::where('uid',$wbw->block_uid)->first();
  167. if(!$wbwBlock){
  168. return $this->error('no wbwBlock data');
  169. }
  170. $sent = PaliSentence::where('book',$wbw->book_id)
  171. ->where('paragraph',$wbw->paragraph)
  172. ->where('word_begin','<=',$wbw->wid)
  173. ->where('word_end','>=',$wbw->wid)
  174. ->first();
  175. if(!$sent){
  176. return $this->error('no sent data');
  177. }
  178. $sentId = "{$sent['book']}-{$sent['paragraph']}-{$sent['word_begin']}-{$sent['word_end']}";
  179. $channel = $wbwBlock->channel_uid;
  180. $content = MdRender::render("{{".$sentId."}}",[$channel]);
  181. return $this->ok($content);
  182. break;
  183. default:
  184. # code...
  185. break;
  186. }
  187. return $this->ok();
  188. }
  189. /**
  190. * Update the specified resource in storage.
  191. *
  192. * @param \Illuminate\Http\Request $request
  193. * @param \App\Models\Discussion $discussion
  194. * @return \Illuminate\Http\Response
  195. */
  196. public function update(Request $request, Discussion $discussion)
  197. {
  198. //
  199. $user = AuthApi::current($request);
  200. if(!$user){
  201. return $this->error(__('auth.failed'),[403],403);
  202. }
  203. //
  204. if($discussion->editor_uid !== $user['user_uid']){
  205. return $this->error(__('auth.failed'),[403],403);
  206. }
  207. $discussion->title = $request->get('title',null);
  208. $discussion->content = $request->get('content',null);
  209. $discussion->editor_uid = $user['user_uid'];
  210. $discussion->save();
  211. return $this->ok(new DiscussionResource($discussion));
  212. }
  213. /**
  214. * Remove the specified resource from storage.
  215. *
  216. * @param \App\Models\Discussion $discussion
  217. * @return \Illuminate\Http\Response
  218. */
  219. public function destroy(Request $request,Discussion $discussion)
  220. {
  221. //
  222. $user = AuthApi::current($request);
  223. if(!$user){
  224. return $this->error(__('auth.failed'),[401],401);
  225. }
  226. //TODO 其他有权限的人也可以删除
  227. if($discussion->editor_uid !== $user['user_uid']){
  228. return $this->error(__('auth.failed'),[403],403);
  229. }
  230. $delete = $discussion->delete();
  231. return $this->ok($delete);
  232. }
  233. }