|
@@ -0,0 +1,90 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2019-06-10
|
|
|
+ * Time: 18:09
|
|
|
+ */
|
|
|
+
|
|
|
+namespace App\Http\Controllers;
|
|
|
+
|
|
|
+use App\Models\Topic;
|
|
|
+use App\Repositories\CategoryRepository;
|
|
|
+use App\Repositories\TopicRepository;
|
|
|
+use App\Transformers\TopicTransformer;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
+use League\Fractal\Manager;
|
|
|
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
+use League\Fractal\Resource\Collection;
|
|
|
+use League\Fractal\Resource\Item;
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
+
|
|
|
+
|
|
|
+class TopicController extends Controller {
|
|
|
+
|
|
|
+ public function __construct(CategoryRepository $categoryRepository,TopicRepository $topicRepository)
|
|
|
+ {
|
|
|
+ $this->categroupRepository = $categoryRepository;
|
|
|
+ $this->topicRepository = $topicRepository;
|
|
|
+ }
|
|
|
+ //列表
|
|
|
+ public function index(Request $request){
|
|
|
+ $topic= $this->topicRepository->index($request->all());
|
|
|
+ $fractal = new Manager();
|
|
|
+ $resource = new Collection($topic, new TopicTransformer());
|
|
|
+ $resource->setPaginator(new IlluminatePaginatorAdapter($topic));
|
|
|
+ $data = $fractal->createData($resource)->toArray();
|
|
|
+ $data['extra'] = [
|
|
|
+ 'filters' => [
|
|
|
+ 'name',
|
|
|
+ ],
|
|
|
+ 'columns' => [
|
|
|
+ 'id',
|
|
|
+ 'name',
|
|
|
+ 'img',
|
|
|
+ 'created_at',
|
|
|
+ 'is_suggest',
|
|
|
+ 'is_hot',
|
|
|
+ 'is_open',
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ //添加
|
|
|
+ public function create(Request $request){
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
+ 'name' => 'required|string|max:12',
|
|
|
+ 'img' => 'required|url',
|
|
|
+ 'desc' => 'required|string|max:100',
|
|
|
+ 'category_ids' => 'required|string',
|
|
|
+ ]);
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
+ }
|
|
|
+ return $this->topicRepository->create($request->all());
|
|
|
+ }
|
|
|
+ //开启
|
|
|
+ public function isOpen(Request $request) {
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
+ 'id' => 'integer|string|max:12',
|
|
|
+ 'is_open' => ['required', Rule::in(0, 1)]
|
|
|
+ ]);
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
+ }
|
|
|
+ return $this->topicRepository->update($request->all());
|
|
|
+ }
|
|
|
+ //开启
|
|
|
+ public function setStatus(Request $request) {
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
+ 'id' => 'integer|string|max:12',
|
|
|
+ 'type'=>['required', Rule::in(1, 2)],
|
|
|
+ 'status' => ['required', Rule::in(0, 1)],
|
|
|
+ ]);
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
+ }
|
|
|
+ return $this->topicRepository->update($request->all());
|
|
|
+ }
|
|
|
+}
|