瀏覽代碼

关注话题

wzq 5 年之前
父節點
當前提交
3ea0fa1d2d
共有 1 個文件被更改,包括 28 次插入8 次删除
  1. 28 8
      app/Repositories/MemberFollowTopicRepository.php

+ 28 - 8
app/Repositories/MemberFollowTopicRepository.php

@@ -9,6 +9,7 @@
 namespace App\Repositories;
 
 use App\Models\MemberFollowTopic;
+use App\Models\Topic;
 use Illuminate\Database\QueryException;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
@@ -18,8 +19,11 @@ use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Log;
 
 class MemberFollowTopicRepository {
-    public function __construct(MemberFollowTopic $memberFollowTopic,CategoryRepository $categoryRepository) {
+    public function __construct(MemberFollowTopic $memberFollowTopic,
+                                Topic $topic,
+                                CategoryRepository $categoryRepository) {
         $this->memberFollowTopic = $memberFollowTopic;
+        $this->topic = $topic;
         $this->categoryRepository = $categoryRepository;
     }
     //关注话题
@@ -31,7 +35,9 @@ class MemberFollowTopicRepository {
         if($data){
             $category_topic_data = [];
             $date = Carbon::now()->toDateTimeString();
+            $topicIds = [];
             foreach($data as $value){
+                $topicIds[] =  $value['id'];
                 $category_topic_data[] = [
                     'uid' => $token['user']->uid,
                     'topic_id' => $value['id'],
@@ -42,6 +48,7 @@ class MemberFollowTopicRepository {
             DB::beginTransaction();
             try{
                 $res = $this->memberFollowTopic->insert($category_topic_data);
+                $this->topic->whereIn('id', $topicIds)->increment('follow_count');
                 $topic = $this->updataMemberFollowSuggestTopic();
                 DB::commit();
                 if($res){
@@ -88,14 +95,20 @@ class MemberFollowTopicRepository {
             return jsonError('您已关注该话题');
         }
         $data = ['uid'=>$token['user']->uid,'topic_id'=>$topic_id];
-        $res = $this->memberFollowTopic->create($data);
-        if($res){
+
+        DB::beginTransaction();
+        try{
+            $this->memberFollowTopic->create($data);
+            $this->topic->where('id', $topic_id)->increment('follow_count');
+            DB::commit();
             $key = 'topic.user_uid'.$token['user']->uid;
             if(!Redis::zscore($key, $topic_id)){
                 Redis::zincrby($key, 0, $topic_id);
             }
             return jsonSuccess();
-        }else{
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::error('关注失败:'.$exception->getMessage());
             return jsonError('关注失败');
         }
     }
@@ -106,11 +119,18 @@ class MemberFollowTopicRepository {
         if(!$info){
             return jsonError('您没有关注该话题');
         }
-        $res = $this->memberFollowTopic->where('id',$info->id)->delete();
-        if($res){
+
+        DB::beginTransaction();
+        try{
+            $this->memberFollowTopic->where('id',$info->id)->delete();
+            $this->topic->where('id', $topic_id)->decrement('follow_count');
+            DB::commit();
+            
             return jsonSuccess();
-        }else{
-            return jsonError('取关失败');
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::error('取消关注失败:'.$exception->getMessage());
+            return jsonError('取消关注失败');
         }
     }
     public function list($request){