func GetBrand(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string { var err error var br brand.Brand if br.ID, err = strconv.Atoi(params["id"]); err != nil { apierror.GenerateError("Trouble getting brand ID", err, rw, req) } if err := br.Get(); err != nil { apierror.GenerateError("Trouble getting brand", err, rw, req) } return encoding.Must(enc.Encode(br)) }
func CreateBrand(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string { br := brand.Brand{ Name: req.FormValue("name"), Code: req.FormValue("code"), } if err := br.Create(); err != nil { apierror.GenerateError("Trouble creating brand", err, rw, req) } return encoding.Must(enc.Encode(br)) }
func UpdateBrand(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string { var err error var br brand.Brand if br.ID, err = strconv.Atoi(params["id"]); err != nil { apierror.GenerateError("Trouble getting brand ID", err, rw, req) } if err = br.Get(); err != nil { apierror.GenerateError("Trouble getting brand", err, rw, req) } if req.FormValue("name") != "" { br.Name = req.FormValue("name") } if req.FormValue("code") != "" { br.Code = req.FormValue("code") } if err := br.Update(); err != nil { apierror.GenerateError("Trouble updating brand", err, rw, req) } return encoding.Must(enc.Encode(br)) }
func AddDealerContact(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { flag.Parse() var d contact.DealerContact var ct contact.ContactType var subject string var err error var brandName string brandIDs, err := dtx.GetBrandsFromKey() if err != nil { brandName = "Unknown Brand" } for _, bID := range brandIDs { var brand brand.Brand brand.ID = bID brand.Get() brandName = brand.Name } ct.ID, err = strconv.Atoi(params["contactTypeID"]) //determines to whom emails go if err != nil { apierror.GenerateError("Trouble getting contact type ID", err, rw, req) return "" } d.Type = ct.Name contType := req.Header.Get("Content-Type") if strings.Contains(contType, "application/json") { //this is our json payload requestBody, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError("Trouble reading JSON request body for adding contact", err, rw, req) return "" } if err = json.Unmarshal(requestBody, &d); err != nil { apierror.GenerateError("Trouble unmarshalling JSON request body into contact", err, rw, req) return "" } } ct.ID, err = strconv.Atoi(d.Type) if err != nil { apierror.GenerateError("Trouble parsing contact type Id.", err, rw, req) return "" } d.Type, err = contact.GetContactTypeNameFromId(ct.ID) if err != nil { apierror.GenerateError("Trouble getting contact type from Id.", err, rw, req) return "" } if d.Type == "" { d.Type = "New Customer" d.Subject = "Becoming a Dealer" subject = d.Subject } else { d.Subject = "Contact Submission regarding " + d.Type + ", from " + d.FirstName + " " + d.LastName + "." } //state/country st := d.State id, err := strconv.Atoi(st) if id > 0 && err == nil { countries, err := geography.GetAllCountriesAndStates() if err == nil { for _, ctry := range countries { for _, state := range *ctry.States { if state.Id == id { d.State = state.State d.Country = ctry.Country break } } } } } if err := d.Add(dtx); err != nil { apierror.GenerateError("Trouble adding contact", err, rw, req) return "" } emailBody := fmt.Sprintf( `This %s contact is inquiring about: %s. Name: %s Email: %s Phone: %s Address 1: %s Address 2: %s City, State, Zip: %s, %s %s Country: %s Subject: %s Message: %s`, brandName, d.Type, d.FirstName+" "+d.LastName, d.Email, d.Phone, d.Address1, d.Address2, d.City, d.State, d.PostalCode, d.Country, d.Subject, d.Message, ) emailAppend1 := fmt.Sprintf(` Business: %s Business Type: %s`, d.BusinessName, d.BusinessType.Type, ) if d.BusinessName != "" || d.BusinessType.Type != "" { emailBody += emailAppend1 } if emailBody != "" && *noEmail == false { if err := contact.SendEmail(ct, subject, emailBody); err != nil { apierror.GenerateError("Trouble sending email to receivers", err, rw, req) return "" } } return encoding.Must(enc.Encode(d)) }