TopicRepository.php 3.3 KB

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