Kaynağa Gözat

话题列表

wzq 5 yıl önce
ebeveyn
işleme
302f656f09

+ 15 - 0
app/Http/Controllers/V1/PostController.php

@@ -18,6 +18,7 @@ use App\Transformers\Post\ReplyTransformer;
 use App\Transformers\Post\SuggestTransformer;
 use App\Transformers\Post\VideoTransformer;
 use App\Transformers\Topic\TopicDetailTransformer;
+use App\Transformers\Topic\TopicListTransformer;
 use App\Transformers\Topic\TopicPostTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Carbon;
@@ -237,6 +238,20 @@ class PostController extends Controller
         return jsonSuccess($data);
     }
 
+    /**
+     * 话题列表
+     */
+    public function topicList(Request $request)
+    {
+        $list = $this->postRepositories->topicList($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($list, new TopicListTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
+        $data = $fractal->createData($resource)->toArray();
+
+        return jsonSuccess($data);
+    }
+
 
     /**
      * 话题详情

+ 23 - 0
app/Repositories/PostRepositories.php

@@ -423,6 +423,29 @@ class PostRepositories
             ->paginate($perPage);
     }
 
+    /**
+     * 话题列表
+     */
+    public function topicList($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+
+        $where = [];
+        if(isset($request['category_id'])){
+           $topic = $this->topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*');
+           $where[] = ['category_topic.category_id', $request['category_id']];
+        }else{
+            $topic = $this->topic;
+        }
+        if(isset($request['is_suggest'])){
+            $where[] = ['topic.is_suggest', $request['is_suggest']];
+        }
+        return $topic
+            ->where($where)
+            ->orderBy('id','desc')
+            ->paginate($perPage);
+    }
+
 
     /**
      * 更新帖子统计数量

+ 24 - 0
app/Transformers/Topic/TopicListTransformer.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/18
+ * Time: 13:57
+ */
+namespace  App\Transformers\Topic;
+
+use App\Models\Topic;
+use Carbon\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class TopicListTransformer extends TransformerAbstract
+{
+    public function transform(Topic $topic)
+    {
+        return [
+            'id' => $topic['id'],
+            'name' => $topic['name'],
+            'follow_count' => $topic->follow->count() + 9876,
+        ];
+    }
+}

+ 2 - 0
routes/api.php

@@ -44,6 +44,8 @@ $api->version('v1', [
         $api->get('topicCategory/getTopics', 'CategoryController@getTopics');
         //话题内容列表
         $api->get('post/topic', 'PostController@topicPost');
+        //话题列表
+        $api->get('topic', 'PostController@topicList');
         //话题详情
         $api->get('topic/detail', 'PostController@topicDetail');
         //关注推荐话题