示例#1
0
文件: user.go 项目: ninnemana/API
// GetComnetAccounts ...
func (u *CustomerUser) GetComnetAccounts() error {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

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

	var uname, pwd *string
	var typeID, warehouseID, acctNum *int
	var freight, lat, long *float64
	var actType, actURL *string
	var wName, code, add, city, postal, toll, fax, localPh, manager *string
	var stateID, countryID *int
	var state, stateAbbr, country, countryAbbr *string

	rows, err := stmt.Query(u.Id)
	if err != nil {
		return err
	}
	defer rows.Close()

	for rows.Next() {
		var ca ComnetAccount
		err = rows.Scan(
			&uname,
			&pwd,
			&acctNum,
			&freight,
			&typeID,
			&actType,
			&actURL,
			&warehouseID,
			&wName,
			&code,
			&add,
			&city,
			&postal,
			&toll,
			&fax,
			&localPh,
			&manager,
			&long,
			&lat,
			&stateID,
			&state,
			&stateAbbr,
			&countryID,
			&country,
			&countryAbbr,
		)
		if err != nil {
			if err == sql.ErrNoRows {
				return nil
			}
			return err
		}
		if uname != nil {
			ca.Credentials.Username = *uname
		}
		if pwd != nil {
			ca.Credentials.Password = *pwd
		}
		if acctNum != nil {
			ca.AccountNumber = *acctNum
		}
		if freight != nil {
			ca.FreightLimit = *freight
		}
		if typeID != nil {
			ca.Type.ID = *typeID

			if actType != nil {
				ca.Type.Title = *actType
			}
			if actURL != nil {
				ca.Type.ComnetURL, _ = url.Parse(*actURL)
			}
		}
		if warehouseID != nil {
			ca.Warehouse.ID = *warehouseID
		}
		if wName != nil {
			ca.Warehouse.Name = *wName
		}
		if code != nil {
			ca.Warehouse.Code = *code
		}
		if add != nil {
			ca.Warehouse.Address = *add
		}
		if city != nil {
			ca.Warehouse.City = *city
		}
		if postal != nil {
			ca.Warehouse.PostaCode = *postal
		}
		if toll != nil {
			ca.Warehouse.TollFreePhone = *toll
		}
		if fax != nil {
			ca.Warehouse.Fax = *fax
		}
		if localPh != nil {
			ca.Warehouse.LocalPhone = *localPh
		}
		if manager != nil {
			ca.Warehouse.Manager = *manager
		}
		if long != nil {
			ca.Warehouse.Longitude = *long
		}
		if lat != nil {
			ca.Warehouse.Latitude = *lat
		}

		if stateID != nil {
			ca.Warehouse.State.Id = *stateID
		}
		if state != nil {
			ca.Warehouse.State.State = *state
		}
		if stateAbbr != nil {
			ca.Warehouse.State.Abbreviation = *stateAbbr
		}

		var coun geography.Country
		if countryID != nil {
			coun.Id = *countryID
		}
		if country != nil {
			coun.Country = *country
		}
		if countryAbbr != nil {
			coun.Abbreviation = *countryAbbr
		}
		ca.Warehouse.State.Country = &coun

		u.ComnetAccounts = append(u.ComnetAccounts, ca)
	}

	return rows.Err()
}
示例#2
0
文件: user.go 项目: ninnemana/API
func (u *CustomerUser) GetLocation() error {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

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

	var stateId, countryId *int
	var state, stateAbbr, country, countryAbbr *string

	err = stmt.QueryRow(u.Id).Scan(
		&u.Location.Id,
		&u.Location.Name,
		&u.Location.Email,
		&u.Location.Address,
		&u.Location.City,
		&u.Location.PostalCode,
		&u.Location.Phone,
		&u.Location.Fax,
		&u.Location.Coordinates.Latitude,
		&u.Location.Coordinates.Longitude,
		&u.Location.CustomerId,
		&u.Location.ContactPerson,
		&u.Location.IsPrimary,
		&u.Location.ShippingDefault,
		&stateId,
		&state,
		&stateAbbr,
		&countryId,
		&country,
		&countryAbbr,
	)
	if err != nil {
		if err == sql.ErrNoRows {
			return nil
		}
		return err
	}
	var coun geography.Country

	if stateId != nil {
		u.Location.State.Id = *stateId
	}
	if state != nil {
		u.Location.State.State = *state
	}
	if stateAbbr != nil {
		u.Location.State.Abbreviation = *stateAbbr
	}
	if countryId != nil {
		coun.Id = *countryId
	}
	if country != nil {
		coun.Country = *country
	}
	if countryAbbr != nil {
		coun.Abbreviation = *countryAbbr
	}
	u.Location.State.Country = &coun
	return nil
}