Exemplo n.º 1
0
/* 后台操作数据表 */
func Restful(this BaseInterface, contro *beego.Controller) {
	ok, data := func() (bool, interface{}) {
		switch contro.GetString("type") {
		case "add": //增
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			//数据验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			if err := this.BaseInsert(); err != nil {
				return false, err.Error()
			}

			return true, "" // 插入成功
		case "delete": //删
			if _ok, _data := this.BaseSetId(contro.GetString("_id")); !_ok {
				return false, _data
			}

			err := this.BaseDelete()

			if err != nil {
				return false, err
			}

			return true, nil
		case "update": //改
			//根据id查询对象
			if _ok, _data := this.BaseFind(contro.GetString("_id")); !_ok {
				return false, _data
			}

			//解析请求参数->对象
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			//验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			if err := this.BaseUpdate(); err != nil {
				return false, err.Error()
			}
			return true, nil
		case "get": //查
			from, _ := contro.GetInt("from")
			number, _ := contro.GetInt("number")

			if number > 100 {
				return false, "最多查询100条"
			}

			if contro.GetString("like") != "" && contro.GetString("likeKey") != "" { // 模糊搜索
				var result []BaseInterface
				var count int

				if contro.GetString("likeMethod") == "like" {
					result = this.BaseSelectLike(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseLikeCount(contro.GetString("likeKey"), contro.GetString("like"))
				} else if contro.GetString("likeMethod") == "accuracy" {
					result = this.BaseSelectAccuracy(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseAccuracyCount(contro.GetString("likeKey"), contro.GetString("like"))
				}

				return true, map[string]interface{}{
					"lists": result,
					"count": count,
				}
			} else { // 普通查询
				if contro.GetString("filter") != "" {
					filter, _ := contro.GetInt("filter")
					count := this.BaseFilterCount(contro.GetString("filterKey"), filter)
					result := this.BaseFilterSelect(from, number, contro.GetString("sort"), contro.GetString("filterKey"), filter)

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				} else {
					count := this.BaseCount()
					result := this.BaseSelect(from, number, contro.GetString("sort"))

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				}
			}
		}
		return true, nil
	}()

	contro.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	contro.ServeJson()
}