|
@@ -10,9 +10,114 @@ namespace App\Repositories;
|
|
|
|
|
|
|
|
|
|
use App\Models\TopicGroup;
|
|
use App\Models\TopicGroup;
|
|
|
|
+use App\Models\TopicGroupInfo;
|
|
|
|
+use Dingo\Api\Http\Response;
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
+use Illuminate\Database\QueryException;
|
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class TopicGroupRepository {
|
|
class TopicGroupRepository {
|
|
- public function __construct(TopicGroup $topicGroup) {
|
|
|
|
|
|
+ public function __construct(TopicGroup $topicGroup,TopicGroupInfo $topicGroupInfo) {
|
|
$this->topicGroup = $topicGroup;
|
|
$this->topicGroup = $topicGroup;
|
|
|
|
+ $this->topicGroupInfo = $topicGroupInfo;
|
|
|
|
+ }
|
|
|
|
+ public function index($request){
|
|
|
|
+ $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
|
|
+ $where = [];
|
|
|
|
+ if(isset($request['name'])){
|
|
|
|
+ $where[] = ['name', 'like', "%{$request['name']}%"];
|
|
|
|
+ }
|
|
|
|
+ return $this->topicGroup->where($where)->paginate($perPage);
|
|
|
|
+ }
|
|
|
|
+ //添加
|
|
|
|
+ public function create($request){
|
|
|
|
+ $category = $this->topicGroup->where(['name'=>$request['name']])->first();
|
|
|
|
+ if($category){
|
|
|
|
+ return Response::create([
|
|
|
|
+ 'message' => '该分组已存在',
|
|
|
|
+ 'status_code' => 500
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ $data = [
|
|
|
|
+ 'name' => $request['name'],
|
|
|
|
+ 'status' => 1,
|
|
|
|
+ ];
|
|
|
|
+ if(!empty($request['topic_ids'])){
|
|
|
|
+ $topic_ids = explode(',', $request['topic_ids']);
|
|
|
|
+ }
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+ try{
|
|
|
|
+ $topicGroup = $this->topicGroup->create($data);
|
|
|
|
+ if($topicGroup){
|
|
|
|
+ if($topic_ids){
|
|
|
|
+ $topic_group_inof_data = [];
|
|
|
|
+ foreach($topic_ids as $value){
|
|
|
|
+ $topic_group_inof_data[] = [
|
|
|
|
+ 'topic_group_id' => $topicGroup->id,
|
|
|
|
+ 'topic_id' => $value,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ $this->topicGroupInfo->insert($topic_group_inof_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 edit($request){
|
|
|
|
+ $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
|
|
|
|
+ if(!$topicGroup){
|
|
|
|
+ return Response::create([
|
|
|
|
+ 'message' => '该话题组不存在',
|
|
|
|
+ 'status_code' => 500
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+ $topicGroup->name = $request['name'];
|
|
|
|
+
|
|
|
|
+ if(!empty($request['topic_ids'])){
|
|
|
|
+ $topic_ids = explode(',', $request['topic_ids']);
|
|
|
|
+ }
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+ try{
|
|
|
|
+ $topicGroupInfo = $topicGroup->save();
|
|
|
|
+ if($topicGroupInfo){
|
|
|
|
+ //删除
|
|
|
|
+ $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
|
|
|
|
+ if($topic_ids){
|
|
|
|
+ $category_topic_data = [];
|
|
|
|
+ foreach($topic_ids as $value){
|
|
|
|
+ $category_topic_data[] = [
|
|
|
|
+ 'topic_group_id' => $topicGroup->id,
|
|
|
|
+ 'topic_id' => $value,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+ $this->topicGroupInfo->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 view($request){
|
|
|
|
+ return $this->topicGroup->where(['id'=>$request['id']])->first();
|
|
}
|
|
}
|
|
}
|
|
}
|