|
@@ -0,0 +1,106 @@
|
|
|
+<?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,
|
|
|
+ ];
|
|
|
+
|
|
|
+ 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, '修改状态失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|