當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Customer.Get方法代碼示例

本文整理匯總了Golang中github.com/ninnemana/API/models/cart.Customer.Get方法的典型用法代碼示例。如果您正苦於以下問題:Golang Customer.Get方法的具體用法?Golang Customer.Get怎麽用?Golang Customer.Get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/ninnemana/API/models/cart.Customer的用法示例。


在下文中一共展示了Customer.Get方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: DeleteAddress

func DeleteAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	// Make sure this isn't the default address
	if c.DefaultAddress != nil && c.DefaultAddress.Id.Hex() == addressId {
		apierror.GenerateError("removing a default address is not allowed", nil, w, req)
		return ""
	}

	found := false
	for i, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.Addresses = append(c.Addresses[:i], c.Addresses[i+1:]...)
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err := c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return ""
}
開發者ID:ninnemana,項目名稱:API,代碼行數:50,代碼來源:address.go

示例2: AddAddress

func AddAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var address cart.CustomerAddress
	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &address); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	// audit the address
	if err := address.Validate(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	addressId := bson.NewObjectId()
	address.Id = &addressId
	c.Addresses = append(c.Addresses, address)

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:48,代碼來源:address.go

示例3: SetDefaultAddress

func SetDefaultAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	found := false
	for _, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.DefaultAddress = &addr
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err := c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:44,代碼來源:address.go

示例4: GetAddresses

func GetAddresses(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {
	customerId := params["id"]
	limit := 50
	page := 1
	qs := req.URL.Query()

	if l := qs.Get("limit"); l != "" {
		lmt, err := strconv.Atoi(l)
		if err == nil && lmt != 0 {
			limit = lmt
		}
	}
	if p := qs.Get("page"); p != "" {
		pg, err := strconv.Atoi(p)
		if err == nil && pg != 0 {
			page = pg
		}
	}

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}

	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	addr := c.Addresses
	if len(c.Addresses) > 0 {
		addr = c.Addresses[:limit]
		if page > 1 && len(c.Addresses) >= ((page-1)*limit) {
			addr = c.Addresses[((page - 1) / limit):limit]
		}
	}

	return encoding.Must(enc.Encode(addr))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:44,代碼來源:address.go

示例5: GetAccount

// Get an account for a given shop
func GetAccount(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop, token string) string {
	cust := cart.Customer{
		ShopId: shop.Id,
	}
	var err error

	cust.Id, err = cart.IdentifierFromToken(token)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = cust.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(cust))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:20,代碼來源:account.go

示例6: GetCustomer

// Get a specific customer for a
// given shop.
func GetCustomer(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	customerId := params["id"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}
	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:22,代碼來源:customer.go

示例7: GetAddress

func GetAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {
	customerId := params["id"]
	addressId := params["address"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}

	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	var address *cart.CustomerAddress
	for _, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			address = &addr
			break
		}
	}

	if address == nil {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:38,代碼來源:address.go

示例8: EditAddress

func EditAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var address cart.CustomerAddress
	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &address); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	// audit the address
	if err := address.Validate(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	found := false
	for i, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.Addresses[i] = address
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}
開發者ID:ninnemana,項目名稱:API,代碼行數:63,代碼來源:address.go


注:本文中的github.com/ninnemana/API/models/cart.Customer.Get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。