TopicRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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', 1];
  29. }
  30. if(isset($request['is_hot'])){
  31. $where[] = ['is_hot', 1];
  32. }
  33. if(isset($request['is_open'])){
  34. $where[] = ['is_open', 1];
  35. }
  36. if(isset($request['is_close'])){
  37. $where[] = ['is_open', 0];
  38. }
  39. return $this->topic->where($where)->paginate($perPage);
  40. }
  41. public function create($request){
  42. $topic = $this->topic->where(['name'=>$request['name']])->first();
  43. if($topic){
  44. return Response::create([
  45. 'message' => '该话题已存在',
  46. 'status_code' => 500
  47. ]);
  48. }
  49. $data = [
  50. 'name' => $request['name'],
  51. 'img' => $request['img'],
  52. 'desc' => $request['desc'],
  53. ];
  54. if(isset($request['category_ids'])){
  55. $category_ids = explode(',', $request['category_ids']);
  56. }
  57. DB::beginTransaction();
  58. try{
  59. $topicInfo = $this->topic->create($data);
  60. if($topicInfo){
  61. if($category_ids){
  62. $category_topic_data = [];
  63. foreach($category_ids as $value){
  64. $category_topic_data[] = [
  65. 'category_id' => $value,
  66. 'topic_id' => $topicInfo->id,
  67. ];
  68. }
  69. $this->categoryTopic->insert($category_topic_data);
  70. }
  71. }
  72. DB::commit();
  73. return Response::create();
  74. }catch (QueryException $exception){
  75. DB::rollBack();
  76. Log::debug('新增话题:'.$exception->getMessage());
  77. return Response::create([
  78. 'message' => '新增话题,请重试',
  79. 'error' => $exception->getMessage(),
  80. 'status_code' => 500
  81. ]);
  82. }
  83. }
  84. //修改
  85. public function update($request){
  86. $topic = $this->topic->where('id', $request['id'])->first();
  87. if(!$topic){
  88. return Response::create([
  89. 'message' => '该话题不存在',
  90. 'status_code' => 500
  91. ]);
  92. }
  93. if(isset($request['is_open']) && $request['is_open'] !== null){
  94. $topic->is_open = $request['is_open'];
  95. }
  96. if(!empty($request['type'])){
  97. if($topic->is_open == 0){
  98. return Response::create([
  99. 'message' => '该话题未开启',
  100. 'status_code' => 500
  101. ]);
  102. }
  103. if(isset($request['type']) && $request['type'] == 1){
  104. $topic->is_suggest = $request['status'];
  105. }
  106. if(isset($request['type']) && $request['type'] == 2){
  107. $topic->is_hot = $request['status'];
  108. }
  109. }
  110. $res = $topic->save();
  111. if($res){
  112. return Response::create();
  113. }
  114. }
  115. }