123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Repositories;
- use App\Models\StarNews;
- use Dingo\Api\Http\Response;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- /**
- * Created by PhpStorm.
- * User: durong
- * Date: 2019/6/17
- * Time: 上午10:59
- */
- class StarNewsRepository
- {
- public function __construct(StarNews $starNews)
- {
- $this->starNews = $starNews;
- }
- public function index($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
- $where = [];
- if(isset($request['id'])){
- $where[] = ['id', '=', $request['id']];
- }
- if(isset($request['status'])){
- $where[] = ['status', '=', $request['status']];
- }
- if (isset($request['title'])){
- $where[] = ['title', 'like', "%{$request['title']}%"];
- }
- return $this->starNews->where($where)->orderBy('status', 'desc')->orderBy('sort', 'asc')->paginate($perPage);
- }
- public function create($request)
- {
- if($this->starNews->where('title', $request['title'])->exists()){
- throw new HttpException(500, '该新闻标题已经存在');
- }
- $data = [
- 'title' => $request['title'],
- 'content' => $request['content'],
- 'cover_img' => $request['cover_img'],
- 'sort' => $request['sort'] ?? 0,
- 'status' => $request['status'] ?? 0,
- ];
- $open_news = $this->starNews->where('status',1)->count();
- if ($open_news >= 5 && $request['status'] == 1){
- throw new HttpException(500, '最多开启5条新闻');
- }
- if (!$this->starNews->create($data)) {
- throw new HttpException(500, '添加失败');
- }
- }
- public function edit($request)
- {
- $starNew = $this->starNews->where('id', $request['id'])->first();
- $starNew->title = $request['title'];
- $starNew->content = $request['content'];
- $starNew->cover_img = $request['cover_img'];
- $starNew->sort = $request['sort'] ?? 0;
- $starNew->status = $request['status'] ?? 0;
- $starNew->updated_at = date('Y-m-d H:i:s');
- $open_news = $this->starNews->where('status',1)->count();
- if ($open_news > 5 && $request['status'] == 1){
- throw new HttpException(500, '最多开启5条新闻');
- }
- $res = $starNew->save();
- if (!$res) {
- throw new HttpException(500, '后院新闻更新失败');
- }
- }
- public function delete($request)
- {
- $starNew = $this->starNews->where('id', $request['id'])->first();
- $res = $starNew->delete();
- if (!$res){
- return Response::create([
- 'message' => '删除失败,请重试',
- 'status_code' => 500
- ]);
- }
- }
- public function editStatus($request)
- {
- $starNew = $this->starNews->find($request['id']);
- $starNew->status = $request['status'];
- $starNew->updated_at = date('Y-m-d H:i:s');
- $open_news = $this->starNews->where('status',1)->count();
- if ($open_news >= 5 && $request['status'] == 1){
- throw new HttpException(500, '最多开启5条新闻');
- }
- $res = $starNew->save();
- if (!$res) {
- throw new HttpException(500, '修改状态失败');
- }
- }
- }
|