Переглянути джерело

平台内容列表、新增、修改

duqinya 5 роки тому
батько
коміт
bb08c9e466

+ 77 - 0
app/Http/Controllers/V2/PlatformContentController.php

@@ -0,0 +1,77 @@
+<?php
+namespace App\Http\Controllers\V2;
+use App\Http\Controllers\BaseController;
+use App\Repositories\PlatformContentRepository;
+use App\Transformers\PlatformContentTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+use League\Fractal\Manager;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午2:04
+ */
+
+class PlatformContentController extends BaseController
+{
+    public function __construct(PlatformContentRepository $platformContentRepository)
+    {
+        $this->platformContentRepository = $platformContentRepository;
+    }
+
+    //平台内容列表
+    public function index(Request $request)
+    {
+        $platformContent = $this->platformContentRepository->index($request->all());
+
+        $fractal = new Manager();
+        $resource = new Collection($platformContent, new PlatformContentTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($platformContent));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'id',
+            ],
+            'columns' => [
+                'id',
+                'title',
+                'content',
+                'updated_at'
+            ]
+        ];
+        return $data;
+    }
+
+    //新增平台内容
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'title' => 'required|string',
+            'content' => 'required|string',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->platformContentRepository->create($request->all());
+
+    }
+
+    //修改平台内容
+    public function edit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:platform_content',
+            'content' => 'required',
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->platformContentRepository->edit($request->all());
+    }
+}

+ 20 - 0
app/Models/PlatformContent.php

@@ -0,0 +1,20 @@
+<?php
+namespace App\Models;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午12:11
+ */
+
+class PlatformContent extends BaseModel
+{
+    protected  $table = 'platform_content';
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['title','content'];
+
+
+}

+ 63 - 0
app/Repositories/PlatformContentRepository.php

@@ -0,0 +1,63 @@
+<?php
+namespace App\Repositories;
+use App\Models\PlatformContent;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午2:06
+ */
+
+class PlatformContentRepository
+{
+    public function __construct(PlatformContent $platformContent)
+    {
+        $this->platformContent = $platformContent;
+    }
+
+    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['title'])){
+            $where[] = ['title', 'like', "%{$request['title']}%"];
+        }
+
+        return $this->platformContent->where($where)->orderBy('updated_at', 'desc')->paginate($perPage);
+
+    }
+
+    public function create($request)
+    {
+        if($this->platformContent->where('title', $request['title'])->exists()){
+            throw new HttpException(500, '该名称已经存在');
+        }
+
+        $data = [
+            'title' => $request['title'],
+            'content' => $request['content'],
+        ];
+
+        if (!$this->platformContent->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+
+    public function edit($request)
+    {
+        $platforms = $this->platformContent->where('id', $request['id'])->first();
+        $platforms->content = $request['content'];
+        $platforms->updated_at = date('Y-m-d H:i:s');
+
+        $res = $platforms->save();
+        if (!$res) {
+            throw new HttpException(500, '平台内容更新失败');
+        }
+    }
+
+}

+ 20 - 0
app/Transformers/PlatformContentTransformer.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Transformers;
+
+use App\Models\PlatformContent;
+use League\Fractal\TransformerAbstract;
+
+class PlatformContentTransformer extends TransformerAbstract
+{
+
+    public function transform(PlatformContent $platformContent)
+    {
+        return [
+            'id'  => $platformContent['id'],
+            'title'    => $platformContent['title'],
+            'content'    => $platformContent['content'],
+            'updated_at'    => date($platformContent['updated_at']),
+        ];
+    }
+}

+ 18 - 1
routes/api.php

@@ -92,7 +92,24 @@ $api->version('v1', [
             $api->delete('/bannerSet/delete', 'BannerController@bannerDelete');
             //修改banner状态
             $api->put('/bannerSet/editStatus', 'BannerController@editStatus');
-        });
 
+            //星球新闻列表
+            $api->get('/starNews/lists', 'StarNewsController@index');
+            //新增星球新闻
+            $api->post('/starNews/create', 'StarNewsController@create');
+            //编辑星球新闻
+            $api->put('/starNews/edit', 'StarNewsController@edit');
+            //删除星球新闻
+            $api->delete('/starNews/delete', 'StarNewsController@delete');
+            //修改星球新闻状态
+            $api->put('/starNews/editStatus', 'StarNewsController@editStatus');
+
+            //平台内容列表
+            $api->get('/platformContent/lists', 'PlatformContentController@index');
+            //新增平台内容
+            $api->post('/platformContent/create', 'PlatformContentController@create');
+            //编辑平台内容
+            $api->put('/platformContent/edit', 'PlatformContentController@edit');
+        });
     });
 });