func GetLocationById(w http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string { str_id := params["id"] if str_id == "" { apierror.GenerateError("You must supply a location identification number.", errors.New("No id."), w, r) } id, err := strconv.Atoi(str_id) if err != nil { apierror.GenerateError("You must supply a location identification number.", err, w, r) } var l customer.CustomerLocation l.Id = id // loc, err := customer.GetLocationById(id) err = l.Get() if err != nil { apierror.GenerateError("Error retrieving locations.", err, w, r) } return encoding.Must(enc.Encode(l)) }
func GetLocation(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string { var cl customer.CustomerLocation var err error id := r.FormValue("id") if id == "" { id = params["id"] } if cl.Id, err = strconv.Atoi(id); err != nil { apierror.GenerateError("Trouble getting location ID", err, rw, r) return "" } if err = cl.Get(); err != nil { apierror.GenerateError("Trouble getting location", err, rw, r) return "" } return encoding.Must(enc.Encode(cl)) }
func SaveLocation(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { var cl customer.CustomerLocation var err error if r.FormValue("id") != "" || params["id"] != "" { id := r.FormValue("id") if id == "" { id = params["id"] } if cl.Id, err = strconv.Atoi(id); err != nil { apierror.GenerateError("Trouble getting location ID", err, rw, r) return "" } if err = cl.Get(); err != nil { apierror.GenerateError("Trouble getting location", err, rw, r) return "" } } name := r.FormValue("name") address := r.FormValue("address") city := r.FormValue("city") state := r.FormValue("stateId") email := r.FormValue("email") phone := r.FormValue("phone") fax := r.FormValue("fax") latitude := r.FormValue("latitude") longitude := r.FormValue("longitude") customerID := r.FormValue("customerId") contactPerson := r.FormValue("contactPerson") isPrimary := r.FormValue("isPrimary") postalCode := r.FormValue("postalCode") shippingDefault := r.FormValue("shippingDefault") if name != "" { cl.Name = name } if address != "" { cl.Address = address } if city != "" { cl.City = city } if state != "" { if cl.State.Id, err = strconv.Atoi(state); err != nil { apierror.GenerateError("Trouble setting state ID", err, rw, r) return "" } } if email != "" { cl.Email = email } if phone != "" { cl.Phone = phone } if fax != "" { cl.Fax = fax } if latitude != "" { if cl.Coordinates.Latitude, err = strconv.ParseFloat(latitude, 64); err != nil { cl.Coordinates.Latitude = 0 } } if longitude != "" { if cl.Coordinates.Longitude, err = strconv.ParseFloat(longitude, 64); err != nil { cl.Coordinates.Longitude = 0 } } if customerID != "" { if cl.CustomerId, err = strconv.Atoi(customerID); err != nil { apierror.GenerateError("Trouble getting customer ID", err, rw, r) return "" } } if contactPerson != "" { cl.ContactPerson = contactPerson } if isPrimary != "" { if cl.IsPrimary, err = strconv.ParseBool(isPrimary); err != nil { cl.IsPrimary = false } } if postalCode != "" { cl.PostalCode = postalCode } if shippingDefault != "" { if cl.ShippingDefault, err = strconv.ParseBool(shippingDefault); err != nil { cl.ShippingDefault = false } } if cl.Id > 0 { err = cl.Update(dtx) } else { err = cl.Create(dtx) } if err != nil { msg := "Trouble creating location" if cl.Id > 0 { msg = "Trouble updating location" } apierror.GenerateError(msg, err, rw, r) return "" } return encoding.Must(enc.Encode(cl)) }