changed: 代码优化

master
luziqi 3 years ago
parent f6ac0a62fb
commit 990b48146f

@ -23,8 +23,7 @@ func GetDeviceByUdid(c *gin.Context) {
udid := c.DefaultQuery("udid", "")
if udid == "" {
rsp.Error("udid error")
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error("udid error"))
return
}
db, err := dbsql.GetConn(dbsql.DSN)
@ -39,8 +38,7 @@ func GetDeviceByUdid(c *gin.Context) {
db.Model(&models.Device{}).Where("udid = ?", udid).Last(&device)
if device.ID < 1 {
rsp.Error("没有找到该设备udid-" + udid)
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error("没有找到该设备udid-"+udid))
return
}
@ -120,8 +118,8 @@ func CreateDevice(c *gin.Context) {
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
rsp.Error(err.Error())
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error(err.Error()))
return
}
defer dbsql.Close(db)
@ -146,15 +144,14 @@ func CreateDevice(c *gin.Context) {
var _device models.Device
db.Model(models.Device{}).Where("udid = ?", obj.Udid).Find(&_device)
if _device.ID > 0 {
rsp.Error("添加失败,该设备已存在")
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error("添加失败,该设备已存在"))
return
}
db.Model(models.Device{}).Create(&obj)
if db.Error != nil {
rsp.Error(db.Error.Error())
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error(db.Error.Error()))
return
}
rsp.Success()
@ -180,22 +177,21 @@ func UpdateDevice(c *gin.Context) {
id := cast.ToInt(c.PostForm("id"))
if id == 0 {
rsp.Error("device.id error - " + c.PostForm("id"))
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error("device.id error - "+c.PostForm("id")))
return
}
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
rsp.Error(err.Error())
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error(err.Error()))
return
}
defer dbsql.Close(db)
var obj models.Device
db.Model(models.Device{}).Where("id = ?", obj.ID).Find(&obj)
if obj.ID < 1 {
rsp.Error("修改失败,该设备不存在")
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error("修改失败,该设备不存在"))
return
}
if c.PostForm("project") != "" {
@ -219,8 +215,8 @@ func UpdateDevice(c *gin.Context) {
db.Model(models.Device{}).Where("id = ?", obj.ID).Updates(&obj)
if db.Error != nil {
rsp.Error(db.Error.Error())
c.JSON(http.StatusOK, rsp)
c.JSON(http.StatusOK, rsp.Error(db.Error.Error()))
return
}
rsp.Success()

Loading…
Cancel
Save