示例#1
0
文件: part.go 项目: ninnemana/API
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,
	}
}
示例#2
0
func GetCustomerPrice(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 price float64
	if price, err = customer.GetCustomerPrice(dtx, p.ID); err != nil {
		apierror.GenerateError("Trouble getting price", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(price))
}
示例#3
0
func Prices(w http.ResponseWriter, r *http.Request, params martini.Params, enc encoding.Encoder, dtx *apicontext.DataContext) string {
	id, err := strconv.Atoi(params["part"])
	if err != nil {
		apierror.GenerateError("Trouble getting part ID", err, w, r)
		return ""
	}
	p := products.Part{
		ID: id,
	}

	priceChan := make(chan int)
	custChan := make(chan int)

	go func() {
		err = p.Get(dtx)
		priceChan <- 1
	}()

	go func() {
		price, custErr := customer.GetCustomerPrice(dtx, p.ID)
		if custErr != nil {
			err = custErr
		}
		p.Pricing = append(p.Pricing, products.Price{0, 0, "Customer", price, false, time.Now()})
		custChan <- 1
	}()

	<-priceChan
	<-custChan

	if err != nil {
		apierror.GenerateError("Trouble getting part prices", err, w, r)
		return ""
	}

	return encoding.Must(enc.Encode(p.Pricing))
}