ArticleMapController.php 7.3 KB

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