Example #1
0
func GetContact(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string {
	var err error
	var c contact.Contact

	if c.ID, err = strconv.Atoi(params["id"]); err != nil {
		apierror.GenerateError("Trouble getting contact ID", err, rw, req)
	}
	if err = c.Get(); err != nil {
		apierror.GenerateError("Trouble getting contact", err, rw, req)
	}
	return encoding.Must(enc.Encode(c))
}
Example #2
0
func UpdateContact(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string {
	var err error
	var c contact.Contact
	if c.ID, err = strconv.Atoi(params["id"]); err != nil {
		apierror.GenerateError("Trouble getting contact ID", err, rw, req)
	}

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

	contType := req.Header.Get("Content-Type")
	if contType == "application/json" {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Trouble getting JSON request body for updating contact", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &c)
		if err != nil {
			apierror.GenerateError("Trouble unmarshalling JSON request body for updating contact", err, rw, req)
		}
	} else {
		if req.FormValue("first_name") != "" {
			c.FirstName = req.FormValue("first_name")
		}

		if req.FormValue("last_name") != "" {
			c.LastName = req.FormValue("last_name")
		}

		if req.FormValue("email") != "" {
			c.Email = req.FormValue("email")
		}

		if req.FormValue("phone") != "" {
			c.Phone = req.FormValue("phone")
		}

		if req.FormValue("subject") != "" {
			c.Subject = req.FormValue("subject")
		}

		if req.FormValue("message") != "" {
			c.Message = req.FormValue("message")
		}

		if req.FormValue("type") != "" {
			c.Type = req.FormValue("type")
		}

		if req.FormValue("address1") != "" {
			c.Address1 = req.FormValue("address1")
		}

		if req.FormValue("address2") != "" {
			c.Address2 = req.FormValue("address2")
		}

		if req.FormValue("city") != "" {
			c.City = req.FormValue("city")
		}

		if req.FormValue("state") != "" {
			c.State = req.FormValue("state")
		}

		if req.FormValue("postal_code") != "" {
			c.PostalCode = req.FormValue("postal_code")
		}

		if req.FormValue("country") != "" {
			c.Country = req.FormValue("country")
		}
		if req.FormValue("brandID") != "" {
			c.Brand.ID, err = strconv.Atoi(req.FormValue("brandID"))
		}
	}
	if err = c.Update(); err != nil {
		apierror.GenerateError("Trouble updating contact", err, rw, req)
	}
	return encoding.Must(enc.Encode(c))
}