Exemple #1
0
func AddSku(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

	rkey := req.FormValue("rkey")
	if !isValidKey(rkey) {
		return handlers.ErrorJsonResponder("Valid RestaurantKey is required")
	}

	itemId := req.FormValue("itemId")
	price := req.FormValue("price")
	pricef, err := strconv.ParseFloat(price, 32)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	item, err := dbi.GetByItemId(itemId, rkey)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	sku := item.NewSku()
	sku.Price = float32(pricef)
	item.AddSku(sku)

	if err := item.Save(rkey); err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	return itemJsonResponder(item)

}
Exemple #2
0
func AddMenuCatItem(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request
	req.ParseForm()
	rkey := req.FormValue("rkey")
	if !isValidKey(rkey) {
		return handlers.ErrorJsonResponder("Valid RestaurantKey is required")
	}

	itemId := req.FormValue("itemId")
	if itemId == "" {
		return handlers.ErrorJsonResponder("ItemId is required")
	}

	item, err := dbi.GetByItemId(itemId, rkey)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	skus := req.Form["skus"]
	if skus == nil || len(skus) == 0 {
		return handlers.ErrorJsonResponder("Skus are required")
	}

	menuId := req.FormValue("menuId")
	if menuId == "" {
		return handlers.ErrorJsonResponder("MenuId is required")
	}
	menu, err := dbm.GetMenuById(menuId, rkey)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	catId := req.FormValue("catId")
	if catId == "" {
		return handlers.ErrorJsonResponder("CategoryId is required")
	}
	catIdx, err := strconv.Atoi(catId)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}
	if catIdx >= len(menu.Categories) {
		return handlers.ErrorJsonResponder("Invalid CategoryId")
	}
	category := menu.Categories[catIdx]

	catItem := category.NewCatItem()
	catItem.ItemId = item.Id

	ci := *item
	ci.Skus = make([]dbi.Sku, len(skus))
	for idx, sku := range skus {
		if skuInt, err := strconv.Atoi(sku); err != nil {
			return handlers.ErrorJsonResponder(err.Error())
		} else {
			if skuInt >= len(item.Skus) {
				return handlers.ErrorJsonResponder("Invalid sku")
			}

			catItem.Sku = append(catItem.Sku, skuInt)
			ci.Skus[idx] = item.Skus[skuInt]
		}
	}
	catItem.Item = &ci

	category.AddItem(catItem)
	menu.Categories[catIdx] = category
	menu.Save(rkey)

	return menuJsonResponder(menu)
}