TopicRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-10
  6. * Time: 18:12
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Category;
  10. use App\Models\CategoryTopic;
  11. use App\Models\Topic;
  12. use Dingo\Api\Http\Response;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Database\QueryException;
  15. class TopicRepository {
  16. public function __construct(Topic $topic,CategoryTopic $categoryTopic){
  17. $this->topic = $topic;
  18. $this->categoryTopic = $categoryTopic;
  19. }
  20. //列表
  21. public function index($request){
  22. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  23. $where = [];
  24. if(isset($request['name'])){
  25. $where[] = ['name', 'like', "%{$request['name']}%"];
  26. }
  27. if(isset($request['is_suggest'])){
  28. $where[] = ['is_suggest', $request['is_suggest']];
  29. }
  30. if(isset($request['is_hot'])){
  31. $where[] = ['is_hot', $request['is_hot']];
  32. }
  33. if(isset($request['is_open'])){
  34. $where[] = ['is_open', $request['is_open']];
  35. }
  36. return $this->topic->where($where)->paginate($perPage);
  37. }
  38. public function create($request){
  39. $topic = $this->topic->where(['name'=>$request['name']])->first();
  40. if($topic){
  41. return Response::create([
  42. 'message' => '该话题已存在',
  43. 'status_code' => 500
  44. ]);
  45. }
  46. $data = [
  47. 'name' => $request['name'],
  48. 'img' => $request['img'],
  49. 'desc' => $request['desc'],
  50. ];
  51. if(isset($request['category_ids'])){
  52. $category_ids = explode(',', $request['category_ids']);
  53. }
  54. DB::beginTransaction();
  55. try{
  56. $topicInfo = $this->topic->create($data);
  57. if($topicInfo){
  58. if($category_ids){
  59. $category_topic_data = [];
  60. foreach($category_ids as $value){
  61. $category_topic_data[] = [
  62. 'category_id' => $value,
  63. 'topic_id' => $topicInfo->id,
  64. ];
  65. }
  66. $this->categoryTopic->insert($category_topic_data);
  67. }
  68. }
  69. DB::commit();
  70. return Response::create();
  71. }catch (QueryException $exception){
  72. DB::rollBack();
  73. Log::debug('新增话题:'.$exception->getMessage());
  74. return Response::create([
  75. 'message' => '新增话题,请重试',
  76. 'error' => $exception->getMessage(),
  77. 'status_code' => 500
  78. ]);
  79. }
  80. }
  81. //修改
  82. public function update($request){
  83. $topic = $this->topic->where('id', $request['id'])->first();
  84. if(!$topic){
  85. return Response::create([
  86. 'message' => '该话题不存在',
  87. 'status_code' => 500
  88. ]);
  89. }
  90. if(isset($request['is_open']) && $request['is_open'] !== null){
  91. $topic->is_open = $request['is_open'];
  92. }
  93. if($request['type']){
  94. if($topic->is_open == 0){
  95. return Response::create([
  96. 'message' => '该话题未开启',
  97. 'status_code' => 500
  98. ]);
  99. }
  100. if(isset($request['type']) == 1){
  101. $topic->is_suggest = $request['is_suggest'];
  102. }
  103. if(isset($request['type']) == 2){
  104. $topic->is_hot = $request['is_hot'];
  105. }
  106. }
  107. $res = $topic->save();
  108. if($res){
  109. return Response::create();
  110. }
  111. }
  112. }