Esempio n. 1
0
func (p *Part) BindCustomer(dtx *apicontext.DataContext) CustomerPart {
	var price float64
	var ref int

	priceChan := make(chan int)
	refChan := make(chan int)
	contentChan := make(chan int)

	go func() {
		price, _ = customer.GetCustomerPrice(dtx, p.ID)
		priceChan <- 1
	}()

	go func() {
		ref, _ = customer.GetCustomerCartReference(dtx.APIKey, p.ID)
		refChan <- 1
	}()

	go func() {
		content, _ := custcontent.GetPartContent(p.ID, dtx.APIKey)
		for _, con := range content {

			strArr := strings.Split(con.ContentType.Type, ":")
			cType := con.ContentType.Type
			if len(strArr) > 1 {
				cType = strArr[1]
			}
			var c Content
			c.ContentType.Type = cType
			c.Text = con.Text
			p.Content = append(p.Content, c)
		}
		contentChan <- 1
	}()

	<-priceChan
	<-refChan
	<-contentChan

	return CustomerPart{
		Price:         price,
		CartReference: ref,
	}
}
Esempio n. 2
0
func GetCustomerCartReference(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var p products.Part

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if p.ID, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting part ID", err, rw, r)
		return ""
	}

	var ref int
	if ref, err = customer.GetCustomerCartReference(dtx.APIKey, p.ID); err != nil {
		apierror.GenerateError("Trouble getting customer cart reference", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(ref))
}