|
@@ -41,6 +41,7 @@ class TopicRepository {
|
|
}
|
|
}
|
|
return $this->topic->where($where)->paginate($perPage);
|
|
return $this->topic->where($where)->paginate($perPage);
|
|
}
|
|
}
|
|
|
|
+ //新增
|
|
public function create($request){
|
|
public function create($request){
|
|
$topic = $this->topic->where(['name'=>$request['name']])->first();
|
|
$topic = $this->topic->where(['name'=>$request['name']])->first();
|
|
if($topic){
|
|
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){
|
|
public function update($request){
|
|
$topic = $this->topic->where('id', $request['id'])->first();
|
|
$topic = $this->topic->where('id', $request['id'])->first();
|