Bläddra i källkod

新增话题编辑

zhangchangchun 5 år sedan
förälder
incheckning
8e686f9621
3 ändrade filer med 71 tillägg och 0 borttagningar
  1. 14 0
      app/Http/Controllers/Topic/TopicController.php
  2. 55 0
      app/Repositories/TopicRepository.php
  3. 2 0
      routes/api.php

+ 14 - 0
app/Http/Controllers/Topic/TopicController.php

@@ -71,6 +71,20 @@ class TopicController extends Controller {
         }
         return  $this->topicRepository->create($request->all());
     }
+    //编辑
+    public function edit(Request $request){
+        $validator = Validator::make($request->all(), [
+            'id'=>'integer|string|max:12',
+            'name' => 'required|string|max:12',
+            'img' => 'required|url',
+            'desc' => 'required|string|max:100',
+            'category_ids' => 'required|string',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->topicRepository->edit($request->all());
+    }
     //开启
     public function isOpen(Request $request) {
         $validator = Validator::make($request->all(), [

+ 55 - 0
app/Repositories/TopicRepository.php

@@ -41,6 +41,7 @@ class TopicRepository {
         }
         return $this->topic->where($where)->paginate($perPage);
     }
+    //新增
     public function create($request){
         $topic = $this->topic->where(['name'=>$request['name']])->first();
         if($topic){
@@ -85,6 +86,60 @@ class TopicRepository {
             ]);
         }
     }
+
+    public function edit($request){
+        $topic = $this->topic->where(['id'=>$request['id']])->first();
+        if(!$topic){
+            return Response::create([
+                'message'  => '该话题不存在',
+                'status_code'   => 500
+            ]);
+        }
+        $_topic = $this->topic->where(['name'=>$request['name']])->where('id','<>',$topic->id)->first();
+        if($_topic){
+            return Response::create([
+                'message'  => '该话题已存在',
+                'status_code'   => 500
+            ]);
+        }
+
+        $topic->name = $request['name'];
+        $topic->img = $request['img'];
+        $topic->desc = $request['desc'];
+        $category_ids = [];
+        if(!empty($request['category_ids'])){
+            $category_ids = explode(',', $request['category_ids']);
+        }
+        DB::beginTransaction();
+        try{
+            //保存
+            $topicInfo = $topic->save();
+            //删除原来关联关系
+            $this->categoryTopic->where(['topic_id'=>$topic->id])->delete();
+            if($topicInfo){
+                if($category_ids){
+                    $category_topic_data = [];
+                    foreach($category_ids as $value){
+                        $category_topic_data[] = [
+                            'category_id' => $value,
+                            'topic_id' => $topic->id,
+                        ];
+                    }
+                    $this->categoryTopic->insert($category_topic_data);
+                }
+            }
+            DB::commit();
+            return Response::create();
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::debug('编辑话题:'.$exception->getMessage());
+            return Response::create([
+                'message'  => '编辑话题,请重试',
+                'error' => $exception->getMessage(),
+                'status_code'   => 500
+            ]);
+        }
+    }
     //修改
     public function update($request){
         $topic = $this->topic->where('id', $request['id'])->first();

+ 2 - 0
routes/api.php

@@ -72,6 +72,8 @@ $api->version('v1', [
             $api->put('topic/categoryEdit', 'CategoryController@edit');
             //新增话题
             $api->post('topic/topicCreate', 'TopicController@create');
+            //编辑话题
+            $api->put('topic/topicEdit', 'TopicController@edit');
             //列表
             $api->get('topic/topicIndex', 'TopicController@index');
             //开启话题