@ -1,15 +1,18 @@
package com.mashibing.controller ;
import com.github.pagehelper.PageInfo ;
import com.mashibing.entity.Air ;
import com.mashibing.entity.District ;
import com.mashibing.form.AirAddForm ;
import com.mashibing.service.AirService ;
import com.mashibing.util.R ;
import com.mashibing.vo.ResultVO ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.validation.BindingResult ;
import org.springframework.web.bind.annotation.* ;
import javax.validation.Valid ;
import javax.websocket.server.PathParam ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
@ -57,4 +60,116 @@ public class AirController {
return R . ok ( pageInfo . getTotal ( ) , pageInfo . getList ( ) ) ;
}
/ * *
* # 添 加 空 气 质 量 信 息
* # 请 求 方 式 & 路 径
* POST http : //localhost:8080/air/add
*
* # 请 求 参 数 正 常 为 了 解 耦 , 可 以 单 独 声 明 一 个 AirAddForm 专 门 去 接 收 。
* districtId = Integer ( 必 传 项 )
* monitorTime = yyyy - MM - dd ( 必 传 项 )
* pm10 = Integer ( 必 传 项 )
* pm25 = Integer ( 必 传 项 )
* monitoringStation = String ( 必 传 项 )
*
* # 业 务 流 程
* 1 、 接 收 参 数
* 2 、 做 参 数 的 非 空 校 验
* 3 、 数 据 扔 到 数 据 库
* /
@PostMapping ( "/air/add" )
public ResultVO airAdd ( @Valid AirAddForm airAddForm , BindingResult result ) {
// 1、查看参数是否合法
if ( result . hasErrors ( ) ) {
// 有参数不满足要求
String message = result . getFieldError ( ) . getDefaultMessage ( ) ;
// 返回参数不合法信息
return R . error ( 400 , message ) ;
}
//2、调用Service层添加数据
airService . add ( airAddForm ) ;
//3、添加成功
return R . ok ( ) ;
}
/ * *
* # 修 改 空 气 质 量 信 息
* # 请 求 方 式 & 路 径
* POST http : //localhost:8080/air/update
*
* # 请 求 参 数
* id = Integer ( 必 传 项 )
* districtId = Integer ( 非 必 传 )
* monitorTime = yyyy - MM - dd ( 非 必 传 )
* pm10 = Integer ( 非 必 传 )
* pm25 = Integer ( 非 必 传 )
* monitoringStation = String ( 非 必 传 )
*
* # 业 务 流 程
* 1 、 接 收 参 数
* 2 、 做 参 数 的 非 空 校 验
* 3 、 修 改 指 定 id 的 数 据
*
* # 响 应 数 据
* 成 功 响 应 :
* {
* "code" : 0 ,
* "msg" : ""
* }
* 失 败 响 应 :
* {
* "code" : 错 误 编 码 ,
* "msg" : "错误信息"
* }
* /
@PostMapping ( "/air/update" )
public ResultVO airUpdate ( Air air ) {
// 1、做参数的非空校验
Integer id = air . getId ( ) ;
if ( id = = null ) {
return R . error ( 400 , "参数不合法!" ) ;
}
//2、找Service修改数据
airService . updateById ( air ) ;
//3、返回
return R . ok ( ) ;
}
/ * *
* # 删 除 空 气 质 量 信 息
* # 请 求 方 式 & 路 径
* DELETE http : //localhost:8080/air/delete/{id}
*
* # 请 求 参 数
* 路 径 上 的 id
*
* # 业 务 流 程
* 1 、 接 收 参 数
* 2 、 删 除 指 定 id 的 数 据
*
* # 响 应 数 据
* 成 功 响 应 :
* {
* "code" : 0 ,
* "msg" : ""
* }
* 失 败 响 应 :
* {
* "code" : 错 误 编 码 ,
* "msg" : "错误信息"
* }
* /
@DeleteMapping ( "/air/delete/{id}" )
public ResultVO airDelete ( @PathVariable Integer id ) {
//1、调用service删除
airService . deleteById ( id ) ;
//2、响应数据
return R . ok ( ) ;
}
}