示例#1
0
文件: types.go 项目: ninnemana/API
func UpdateContactType(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string {
	var err error
	var ct contact.ContactType

	if ct.ID, err = strconv.Atoi(params["id"]); err != nil {
		apierror.GenerateError("Trouble getting contact type ID", err, rw, req)
	}

	//json?

	if err = ct.Get(); err != nil {
		apierror.GenerateError("Trouble getting contact type", err, rw, req)
	}

	if req.FormValue("name") != "" {
		ct.Name = req.FormValue("name")
	}

	if req.FormValue("show") != "" {
		if show, err := strconv.ParseBool(req.FormValue("show")); err == nil {
			ct.ShowOnWebsite = show
		}
	}
	if req.FormValue("brandId") != "" {
		if brandId, err := strconv.Atoi(req.FormValue("brandId")); err == nil {
			ct.BrandID = brandId
		}
	}

	if err = ct.Update(); err != nil {
		apierror.GenerateError("Trouble updating contact type", err, rw, req)
	}

	return encoding.Must(enc.Encode(ct))
}
示例#2
0
文件: types.go 项目: ninnemana/API
func AddContactType(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string {
	ct := contact.ContactType{
		Name: req.FormValue("name"),
	}
	if req.FormValue("show") != "" {
		if show, err := strconv.ParseBool(req.FormValue("show")); err == nil {
			ct.ShowOnWebsite = show
		}
	}

	if req.FormValue("brandId") != "" {
		if brandId, err := strconv.Atoi(req.FormValue("brandId")); err == nil {
			ct.BrandID = brandId
		}
	}

	if err := ct.Add(); err != nil {
		apierror.GenerateError("Trouble adding contact type", err, rw, req)
	}

	return encoding.Must(enc.Encode(ct))
}