Bladeren bron

新增城市接口

duqinya 6 jaren geleden
bovenliggende
commit
055f0456ff

+ 20 - 2
app/Http/Controllers/ConfigCityManagementController.php

@@ -50,12 +50,30 @@ class ConfigCityManagementController extends Controller
         return $data;
     }
 
+    /**
+     * 添加城市
+     */
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'province_id' => 'required|int',
+            'province_name' => 'required|string',
+            'city_id' => 'required|int',
+            'city_name' => 'required|string',
+            'express_type' => 'required|string'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->configCityManagementRepository->create($request->all());
+    }
+
 
-    //编辑/新增城市
+    //编辑城市
     public function edit(Request $request)
     {
         $validator = Validator::make($request->all(), [
-//            'id' => 'required|exists:config_city_management',
+            'id' => 'required|exists:config_city_management',
             'province_name' => 'required|string|max:20',
             'province_id' => 'required|integer',
             'city_id' => 'required|integer',

+ 22 - 0
app/Repositories/ConfigCityManagementRepository.php

@@ -27,6 +27,28 @@ class ConfigCityManagementRepository {
 
     }
 
+    public function create($request)
+    {
+        if($this->configCityManagement->where('city_id', $request['city_id'])->exists()){
+            throw new HttpException(500, '该城市已经存在');
+        }
+
+        $data = [
+            'province_id' => $request['province_id'],
+            'province_name' => $request['province_name'],
+            'name' => $request['name'],
+            'city_id' => $request['city_id'],
+            'city_name' => $request['city_name'],
+            'express_type' => $request['express_type'],
+            'status' => 1,
+        ];
+
+        if (!$this->configCityManagement->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+
+    }
+
     public function edit($request)
     {
         $configCity = $this->configCityManagement->where('id', $request['id'])->first();

+ 2 - 0
routes/api.php

@@ -48,6 +48,8 @@ $api->version('v1', [
         $api->post('/cityManagement/edit', 'ConfigCityManagementController@edit');
         //删除城市
         $api->post('/cityManagement/delete', 'ConfigCityManagementController@delete');
+        //新增城市
+        $api->post('/cityManagement/create', 'ConfigCityManagementController@create');
     });
 
 });