示例#1
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))
}