StarNewsRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\StarNews;
  4. use Dingo\Api\Http\Response;
  5. use Symfony\Component\HttpKernel\Exception\HttpException;
  6. /**
  7. * Created by PhpStorm.
  8. * User: durong
  9. * Date: 2019/6/17
  10. * Time: 上午10:59
  11. */
  12. class StarNewsRepository
  13. {
  14. public function __construct(StarNews $starNews)
  15. {
  16. $this->starNews = $starNews;
  17. }
  18. public function index($request)
  19. {
  20. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  21. $where = [];
  22. if(isset($request['id'])){
  23. $where[] = ['id', '=', $request['id']];
  24. }
  25. if(isset($request['status'])){
  26. $where[] = ['status', '=', $request['status']];
  27. }
  28. if (isset($request['title'])){
  29. $where[] = ['title', 'like', "%{$request['title']}%"];
  30. }
  31. return $this->starNews->where($where)->orderBy('status', 'desc')->orderBy('sort', 'asc')->paginate($perPage);
  32. }
  33. public function create($request)
  34. {
  35. if($this->starNews->where('title', $request['title'])->exists()){
  36. throw new HttpException(500, '该新闻标题已经存在');
  37. }
  38. $data = [
  39. 'title' => $request['title'],
  40. 'content' => $request['content'],
  41. 'cover_img' => $request['cover_img'],
  42. 'sort' => $request['sort'] ?? 0,
  43. 'status' => $request['status'] ?? 0,
  44. ];
  45. $open_news = $this->starNews->where('status',1)->count();
  46. if ($open_news >= 5 && $request['status'] == 1){
  47. throw new HttpException(500, '最多开启5条新闻');
  48. }
  49. if (!$this->starNews->create($data)) {
  50. throw new HttpException(500, '添加失败');
  51. }
  52. }
  53. public function edit($request)
  54. {
  55. $starNew = $this->starNews->where('id', $request['id'])->first();
  56. $starNew->title = $request['title'];
  57. $starNew->content = $request['content'];
  58. $starNew->cover_img = $request['cover_img'];
  59. $starNew->sort = $request['sort'] ?? 0;
  60. $starNew->status = $request['status'] ?? 0;
  61. $starNew->updated_at = date('Y-m-d H:i:s');
  62. $open_news = $this->starNews->where('status',1)->count();
  63. if ($open_news > 5 && $request['status'] == 1){
  64. throw new HttpException(500, '最多开启5条新闻');
  65. }
  66. $res = $starNew->save();
  67. if (!$res) {
  68. throw new HttpException(500, '后院新闻更新失败');
  69. }
  70. }
  71. public function delete($request)
  72. {
  73. $starNew = $this->starNews->where('id', $request['id'])->first();
  74. $res = $starNew->delete();
  75. if (!$res){
  76. return Response::create([
  77. 'message' => '删除失败,请重试',
  78. 'status_code' => 500
  79. ]);
  80. }
  81. }
  82. public function editStatus($request)
  83. {
  84. $starNew = $this->starNews->find($request['id']);
  85. $starNew->status = $request['status'];
  86. $starNew->updated_at = date('Y-m-d H:i:s');
  87. $open_news = $this->starNews->where('status',1)->count();
  88. if ($open_news >= 5 && $request['status'] == 1){
  89. throw new HttpException(500, '最多开启5条新闻');
  90. }
  91. $res = $starNew->save();
  92. if (!$res) {
  93. throw new HttpException(500, '修改状态失败');
  94. }
  95. }
  96. }