Ejemplo n.º 1
0
func (l *CustomerLocation) Get() error {

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(getLocation)
	if err != nil {
		return err
	}
	defer stmt.Close()

	var name, address, city, email, phone, fax, contactPerson, postal []byte
	err = stmt.QueryRow(l.Id).Scan(
		&l.Id,
		&name,
		&address,
		&city,
		&l.State.Id,
		&email,
		&phone,
		&fax,
		&l.Coordinates.Latitude,
		&l.Coordinates.Longitude,
		&l.CustomerId,
		&contactPerson,
		&l.IsPrimary,
		&postal,
		&l.ShippingDefault,
	)
	if err != nil {
		return err
	}
	l.Name, err = conversions.ByteToString(name)
	l.Address, err = conversions.ByteToString(address)
	l.City, err = conversions.ByteToString(city)
	l.Email, err = conversions.ByteToString(email)
	l.Phone, err = conversions.ByteToString(phone)
	l.Fax, err = conversions.ByteToString(fax)
	l.ContactPerson, err = conversions.ByteToString(contactPerson)
	l.PostalCode, err = conversions.ByteToString(postal)
	if err != nil {
		return err
	}

	return err
}
Ejemplo n.º 2
0
func (cu *CustomerUser) ResetPass() (string, 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(setCustomerUserPasswordWithID)
	if err != nil {
		return "", err
	}

	randPass := encryption.GeneratePassword()

	// encrypt the random password:
	encryptPass, err := bcrypt.GenerateFromPassword([]byte(randPass), bcrypt.DefaultCost)
	e, err := conversions.ByteToString(encryptPass)
	if err != nil {
		return "", err
	}

	result, err := stmt.Exec(e, cu.Email, cu.CustID)
	if err != nil {
		tx.Rollback()
		return "", err
	}
	tx.Commit()

	//Num Rows
	numRows, err := result.RowsAffected()
	if err != nil {
		return "", err
	}
	if numRows == 0 {
		return "", errors.New("No Users with that email/custID combination.")
	}

	return randPass, nil
}
Ejemplo n.º 3
0
func GetAllLocations(dtx *apicontext.DataContext) (CustomerLocations, error) {
	var ls CustomerLocations
	var err error
	redis_key := "customers:locations:" + dtx.BrandString
	data, err := redis.Get(redis_key)
	if err == nil && len(data) > 0 {
		err = json.Unmarshal(data, &ls)
		return ls, err
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return ls, err
	}
	defer db.Close()

	stmt, err := db.Prepare(getLocations)
	if err != nil {
		return ls, err
	}
	defer stmt.Close()
	res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID)
	var name, address, city, email, phone, fax, contactPerson, postal []byte
	for res.Next() {
		var l CustomerLocation
		err = res.Scan(
			&l.Id,
			&name,
			&address,
			&city,
			&l.State.Id,
			&email,
			&phone,
			&fax,
			&l.Coordinates.Latitude,
			&l.Coordinates.Longitude,
			&l.CustomerId,
			&contactPerson,
			&l.IsPrimary,
			&postal,
			&l.ShippingDefault,
		)
		if err != nil {
			return ls, err
		}
		l.Name, err = conversions.ByteToString(name)
		l.Address, err = conversions.ByteToString(address)
		l.City, err = conversions.ByteToString(city)
		l.Email, err = conversions.ByteToString(email)
		l.Phone, err = conversions.ByteToString(phone)
		l.Fax, err = conversions.ByteToString(fax)
		l.ContactPerson, err = conversions.ByteToString(contactPerson)
		l.PostalCode, err = conversions.ByteToString(postal)
		if err != nil {
			return ls, err
		}
		ls = append(ls, l)
	}
	defer res.Close()
	go redis.Setex(redis_key, ls, 86400)
	return ls, err
}