func (n *News) Create(dtx *apicontext.DataContext) error { db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() tx, err := db.Begin() if err != nil { return err } stmt, err := tx.Prepare(create) res, err := stmt.Exec(n.Title, n.Lead, n.Content, n.PublishStart, n.PublishEnd, n.Active, n.Slug) if err != nil { tx.Rollback() return err } id, err := res.LastInsertId() n.ID = int(id) if err != nil { tx.Rollback() return err } //createToBrand brands, err := dtx.GetBrandsFromKey() if err != nil { tx.Rollback() return err } bChan := make(chan int) go func() (err error) { if len(brands) > 0 { for _, brand := range brands { err = n.CreateJoinBrand(brand) } } bChan <- 1 return err }() <-bChan tx.Commit() return nil }
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)) }