2
0

DiscussionController.php 8.9 KB

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