2
0

ArticleMapController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\ArticleMapResource;
  4. use App\Models\Article;
  5. use App\Models\ArticleCollection;
  6. use App\Models\Collection;
  7. use App\Services\AuthService;
  8. use App\Services\CollectionService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. class ArticleMapController extends Controller
  12. {
  13. public function __construct(protected CollectionService $service) {}
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return Response
  18. */
  19. public function index(Request $request)
  20. {
  21. $request->validate([
  22. 'id' => ['required', 'uuid'],
  23. ]);
  24. switch ($request->input('view')) {
  25. case 'anthology':
  26. $table = ArticleCollection::where('collect_id', $request->input('id'))
  27. ->leftJoin('articles', 'articles.uid', '=', 'article_collections.article_id');
  28. break;
  29. case 'article':
  30. $table = ArticleCollection::where('article_id', $request->input('id'))
  31. ->leftJoin('articles', 'articles.uid', '=', 'article_collections.article_id');
  32. break;
  33. default:
  34. return $this->error('unknown view', null, 422);
  35. }
  36. $count = $table->count();
  37. $result = [];
  38. if (! empty($request->input('parent'))) {
  39. // 输出某节点的子节点
  40. $node = $table->where('article_id', $request->input('parent'))->first();
  41. if ($node) {
  42. $nodeList = ArticleCollection::where('collect_id', $request->input('id'))
  43. ->where('id', '>', (int) $node->id)->orderBy('id')->get();
  44. foreach ($nodeList as $key => $curr) {
  45. if ($curr->level <= $node->level) {
  46. break;
  47. }
  48. if ($request->has('lazy')) {
  49. if ($curr->level === $node->level + 1) {
  50. $result[] = $curr;
  51. }
  52. } else {
  53. $result[] = $curr;
  54. }
  55. }
  56. }
  57. } else {
  58. if ($request->has('lazy') && $count > 300) {
  59. $table = $table->where('level', 1);
  60. }
  61. $result = $table->select([
  62. 'article_collections.id',
  63. 'collect_id',
  64. 'article_id',
  65. 'level',
  66. 'article_collections.title',
  67. 'children',
  68. 'article_collections.editor_id',
  69. 'article_collections.deleted_at',
  70. 'articles.status',
  71. ])->orderBy('id')->get();
  72. }
  73. return $this->ok(['rows' => ArticleMapResource::collection($result), 'count' => $count]);
  74. }
  75. /**
  76. * Store a newly created resource in storage.
  77. *
  78. * @return Response
  79. */
  80. public function store(Request $request)
  81. {
  82. //
  83. $validated = $request->validate([
  84. 'anthology_id' => 'required',
  85. 'operation' => 'required',
  86. ]);
  87. $collection = Collection::find($request->input('anthology_id'));
  88. if (! $collection) {
  89. return $this->error('no recorder');
  90. }
  91. // 鉴权
  92. $user = AuthService::current($request);
  93. if (! $user) {
  94. return $this->error(__('auth.failed'));
  95. }
  96. if (! $this->service->userCanEdit($user['user_uid'], $collection)) {
  97. return $this->error(__('auth.failed'));
  98. }
  99. switch ($validated['operation']) {
  100. case 'add':
  101. // 添加多个文章到文集
  102. $count = 0;
  103. foreach ($request->input('article_id') as $key => $article) {
  104. // code...
  105. if (! ArticleCollection::where('article_id', $article)
  106. ->where('collect_id', $request->input('anthology_id'))
  107. ->exists()) {
  108. $new = new ArticleCollection;
  109. $new->id = app('snowflake')->id();
  110. $new->article_id = $article;
  111. $new->collect_id = $request->input('anthology_id');
  112. $new->title = Article::find($article)->title;
  113. $new->level = 1;
  114. $new->editor_id = $user['user_id'];
  115. $new->save();
  116. $count++;
  117. }
  118. }
  119. return $this->ok($count);
  120. break;
  121. default:
  122. return $this->error('unknown operation');
  123. break;
  124. }
  125. }
  126. /**
  127. * Display the specified resource.
  128. *
  129. * @param ArticleCollection $articleCollection
  130. * @return Response
  131. */
  132. public function show(string $articleCollection)
  133. {
  134. //
  135. $id = explode('_', $articleCollection);
  136. $result = ArticleCollection::where('article_id', $id[0])
  137. ->where('collect_id', $id[1])
  138. ->first();
  139. if ($result) {
  140. return $this->ok(new ArticleMapResource($result));
  141. } else {
  142. return $this->error('no');
  143. }
  144. }
  145. /**
  146. * Update the specified resource in storage.
  147. *
  148. * @return Response
  149. */
  150. public function update(Request $request, string $id)
  151. {
  152. //
  153. $validated = $request->validate([
  154. 'operation' => 'required',
  155. ]);
  156. $collection = Collection::find($id);
  157. if (! $collection) {
  158. return $this->error('no recorder');
  159. }
  160. // 鉴权
  161. $user = AuthService::current($request);
  162. if (! $user) {
  163. return $this->error(__('auth.failed'));
  164. }
  165. if (! $this->service->userCanEdit($user['user_uid'], $collection)) {
  166. return $this->error(__('auth.failed'));
  167. }
  168. switch ($validated['operation']) {
  169. case 'anthology':
  170. $delete = ArticleCollection::where('collect_id', $id)->delete();
  171. $count = 0;
  172. foreach ($request->input('data') as $key => $row) {
  173. // code...
  174. $new = new ArticleCollection;
  175. $new->id = app('snowflake')->id();
  176. $new->article_id = $row['article_id'];
  177. $new->collect_id = $id;
  178. $new->title = $row['title'];
  179. $new->level = $row['level'];
  180. $new->children = $row['children'];
  181. $new->editor_id = $user['user_id'];
  182. if (isset($row['deleted_at'])) {
  183. $new->deleted_at = $row['deleted_at'];
  184. }
  185. $new->save();
  186. $count++;
  187. }
  188. ArticleMapController::updateCollection($id);
  189. return $this->ok($count);
  190. break;
  191. }
  192. }
  193. /**
  194. * Remove the specified resource from storage.
  195. *
  196. * @return Response
  197. */
  198. public function destroy(ArticleCollection $articleCollection)
  199. {
  200. //
  201. }
  202. public static function deleteArticle(string $articleId)
  203. {
  204. // 查找有这个文章的文集
  205. $collections = ArticleCollection::where('article_id', $articleId)
  206. ->select('collect_id')
  207. ->groupBy('collect_id')
  208. ->get();
  209. // 设置为删除
  210. ArticleCollection::where('article_id', $articleId)
  211. ->update(['deleted_at' => now()]);
  212. // 查找没有下级文章的文集
  213. $updateCollections = ArticleCollection::where('article_id', $articleId)
  214. ->where('children', 0)
  215. ->select('collect_id')
  216. ->groupBy('collect_id')
  217. ->get();
  218. // 真的删除没有下级文章的文集中的文章
  219. $count = ArticleCollection::where('article_id', $articleId)
  220. ->where('children', 0)
  221. ->delete();
  222. // 更新改动的文集
  223. foreach ($updateCollections as $collection) {
  224. // code...
  225. ArticleMapController::updateCollection($collection->collect_id);
  226. }
  227. return [count($collections), $count];
  228. }
  229. public static function deleteCollection(string $collectionId)
  230. {
  231. $count = ArticleCollection::where('collect_id', $collectionId)
  232. ->delete();
  233. return $count;
  234. }
  235. /**
  236. * 用表中的数据生成json,更新collection 表中的字段
  237. */
  238. public static function updateCollection(string $collectionId)
  239. {
  240. $result = ArticleCollection::where('collect_id', $collectionId)
  241. ->select(['article_id', 'level', 'title'])
  242. ->orderBy('id')->get();
  243. Collection::where('uid', $collectionId)
  244. ->update(['article_list' => json_encode($result, JSON_UNESCAPED_UNICODE)]);
  245. return count($result);
  246. }
  247. }