예제 #1
0
파일: news_model.go 프로젝트: ninnemana/API
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
}
예제 #2
0
func Mock() (*apicontext.DataContext, error) {
	var dtx apicontext.DataContext
	var err error
	// Needs to create records in the db for the following because of foreign key constraints:
	// Bare Min:
	dtx.CustomerID, err = InsertCustomer()
	if err != nil {
		return &dtx, err
	}

	// CustomerUser
	if dtx.UserID, err = CreateCustomerUser(dtx.CustomerID); err != nil {
		return &dtx, err
	}

	// Brand
	dtx.BrandID = 1

	// ApiKeyType
	keyTypes := []string{"Authentication", "Private", "Public"}

	keys := make(map[string]string)

	for _, keyType := range keyTypes {
		var keyTypeID string
		if keyTypeID, err = CreateApiKeyType(keyType); err != nil {
			return &dtx, err
		}

		// ApiKey and ApiKeyToBrand -
		if _, keys[keyType], err = CreateApiKey(dtx.UserID, keyTypeID, keyType, dtx.BrandID); err != nil {
			return &dtx, err
		}
	}

	dtx.Globals = make(map[string]interface{})
	dtx.Globals["keys"] = keys

	//leaves APIKey as public one
	for t, k := range keys {
		if t == "Public" {
			dtx.APIKey = k
		}
	}
	// Website
	if dtx.WebsiteID, err = CreateWebsite("http://www.testWebsite23sdf.com", "bogus website"); err != nil {
		return &dtx, err
	}

	// WebsiteToBrand
	var BrandIDs []int
	BrandIDs = append(BrandIDs, dtx.BrandID)
	if err = CreateWebsiteToBrands(BrandIDs, dtx.WebsiteID); err != nil {
		return &dtx, err
	}

	//CustomerToBrand
	if err = InsertCustomerToBrand(dtx.CustomerID, BrandIDs); err != nil {
		return &dtx, err
	}

	//brandString and array
	err = dtx.GetBrandsArrayAndString(dtx.APIKey, dtx.BrandID)
	if err != nil {
		return &dtx, err
	}
	return &dtx, nil
}
예제 #3
0
파일: contact.go 프로젝트: ninnemana/API
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))
}