Преглед изворни кода

自提点分组列表查看与新增的接口返回修改

duqinya пре 6 година
родитељ
комит
24a9cf514e

+ 33 - 69
app/Http/Controllers/ConfigPickupGroupController.php

@@ -1,11 +1,15 @@
 <?php
 namespace App\Http\Controllers;
 use App\Models\ConfigPickupGroup;
+use App\Repositories\ConfigPickupGroupRepository;
 use Illuminate\Http\Request;
 use App\Http\ApiHelper;
 use Illuminate\Support\Facades\Validator;
 use App\Transformers\PickupGroupTransformer;
-
+//use Illuminate\Support\Facades\Validator;
+use League\Fractal\Resource\Collection;
+use League\Fractal\Manager;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
 /**
  * Created by PhpStorm.
  * User: qinyaer
@@ -15,87 +19,47 @@ use App\Transformers\PickupGroupTransformer;
 
  class ConfigPickupGroupController extends BaseController
  {
-     /**
-      * @api {get} /pickupGroup/index 自提点分组列表
-      * @apiVersion 0.1
-      * @apiName ConfigPickupGroup index
-      * @apiGroup ConfigPickupGroup
-      * @apiPermission none
-      * @apiSuccessExample 成功响应:
-
+     public function __construct(ConfigPickupGroupRepository $configPickupGroupRepository)
      {
-     "data": [],
-     "extra": {
-     "filters": [
-     "筛选字段1",
-     "筛选字段2"
-     ],
-     "columns": [
-     "id",
-     "name"
-     ]
-     },
-     "meta": {
-     "pagination": {
-     "total": 2,
-     "count": 2,
-     "per_page": 15,
-     "current_page": 1,
-     "total_pages": 1,
-     "links": []
+         $this->configPickupGroupRepository = $configPickupGroupRepository;
      }
-     }
-      */
-     public function index()
-     {
-         $ConfigPickupGroup = ConfigPickupGroup::orderBy('id', 'desc')
-             ->paginate();
 
-         return $this->response->paginator($ConfigPickupGroup, new PickupGroupTransformer());
-     }
+    //自提点分组列表
+     public function index(Request $request)
+     {
 
-     /**
-      * @api {post} /pickupGroup/add 新建自提点分组
-      * @apiVersion 0.1
-      * @apiName ConfigPickupGroup add
-      * @apiGroup ConfigPickupGroup
-      * @apiPermission none
-      * @apiSuccessExample 成功响应:
+         $configPickup = $this->configPickupGroupRepository->index($request->all());
 
-     {
-     "meta": {
-     "message": "Success.",
-     "status_code": 200
-     }
+         $fractal = new Manager();
+         $resource = new Collection($configPickup, new PickupGroupTransformer());
+         $resource->setPaginator(new IlluminatePaginatorAdapter($configPickup));
+         $data = $fractal->createData($resource)->toArray();
+         $data['extra'] = [
+             'filters' => [
+                 'id'
+             ],
+             'columns' => [
+                 'id',
+                 'name',
+             ]
+         ];
+         return $data;
      }
-      */
 
-     public function add(Request $request)
+
+     //新建自提点分组
+     public function create(Request $request)
      {
-         $name = $request->input('name') ? $request->input('name') : '';
-         $all = [
-             'name' => $name,
-         ];
-         $rules = [
-             'name' => 'required|max:20',
-             ];
-         $massage = [
-             'name.required' => '自提点分组名称不能为空',
-             'name.max' => '自提点分组名称不能超过20个字符',
-             ];
-         $validator = Validator::make($all, $rules, $massage);
 
+         $validator = Validator::make($request->all(), [
+             'name' => 'required|string|max:20',
+         ]);
          if ($validator->fails()) {
-
-             return $this->response->array(ApiHelper::error('请求参数格式不正确!', 412));
+             return $this->response->error($validator->errors()->first(), 500);
          }
-         $pickup_group = ConfigPickupGroup::create($all);
-         if(!$pickup_group) {
 
-             return $this->response->array(ApiHelper::error('新建自提点分组失败!', 500));
-         }
+         return  $this->configPickupGroupRepository->create($request->all());
 
-         return $this->response->array(ApiHelper::success());
      }
 
  }

+ 46 - 0
app/Repositories/ConfigPickupGroupRepository.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace App\Repositories;
+
+
+use App\Models\ConfigPickupGroup;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Dingo\Api\Http\Response;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Database\QueryException;
+
+class ConfigPickupGroupRepository {
+    public function __construct(ConfigPickupGroup $configPickupGroup) {
+
+        $this->configPickupGroup = $configPickupGroup;
+    }
+    //列表
+    public function index($request)
+    {
+        $where = [];
+        if(isset($request['id'])){
+            $where[] = ['id', '=', $request['id']];
+        }
+
+        return $this->configPickupGroup->where($where)->orderBy('id', 'asc')->paginate(20);
+
+    }
+
+    /**
+     * 添加自提点
+     */
+    public function create($request)
+    {
+        if($this->configPickupGroup->where('name', trim($request['name']))->exists()){
+            throw new HttpException(500, '该自提点已经存在');
+        }
+
+        $data = [
+            'name' => $request['name'],
+        ];
+
+        if (!$this->configPickupGroup->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+}