Esempio n. 1
0
func LoginUser(r *handlers.HttpRequest) *handlers.JsonResponder {
	authToken := r.Request.FormValue("at")
	provider := r.Request.FormValue("p")
	if authToken == "" || provider == "" {
		return handlers.ErrorJsonResponder("authToken and provider parameters are required")
	}

	errorResponder := func(err error) *handlers.JsonResponder {
		return handlers.ErrorJsonResponder(err.Error())
	}

	oauth, err := authorizeToken(authToken, provider)
	if err != nil {
		return errorResponder(err)
	}

	fmt.Println(oauth)
	user, err := saveUserIfNew(oauth)
	if err != nil {
		return errorResponder(err)
	}

	bytes, err := json.Marshal(user)
	if err != nil {
		return errorResponder(err)
	}

	cookie := &http.Cookie{Name: "uid", Value: user.Id.Hex()}
	responder := &handlers.JsonResponder{}
	responder.AddCookie(*cookie)
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))

	return responder
}
Esempio n. 2
0
func AddMenuCategory(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request
	rkey := req.FormValue("rkey")
	if !isValidKey(rkey) {
		return handlers.ErrorJsonResponder("Valid RestaurantKey is required")
	}

	name := req.FormValue("name")
	if name == "" {
		return handlers.ErrorJsonResponder("Name is 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())
	}

	category := menu.NewCategory()
	category.Name = name
	menu.AddCategory(category)

	menu.Save(rkey)

	return menuJsonResponder(menu)
}
Esempio n. 3
0
func AddNewBranch(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

	rid := req.FormValue("rid")
	if rid == "" {
		return handlers.ErrorJsonResponder("Restaurant Id (rid) required")
	}
	res, err := dbr.GetRestaurantById(rid)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	b := res.NewBranch()
	b.Name = req.FormValue("bname")
	b.Address = req.FormValue("address")
	b.City = req.FormValue("city")
	b.State = req.FormValue("state")
	b.Zipcode = req.FormValue("zipcode")

	res.AddBranch(b)
	err = res.Save()
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	bytes, err := json.Marshal(res)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}
	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))

	return responder
}
Esempio n. 4
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)

}
Esempio n. 5
0
func AddNewRestaurant(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

	restaurant := dbr.NewRestaurant()
	restaurant.Name = req.FormValue("name")
	restaurant.About = req.FormValue("about")

	errorResponder := func(err error) *handlers.JsonResponder {
		return handlers.ErrorJsonResponder(err.Error())
	}

	if err := restaurant.Save(); err != nil {
		return errorResponder(err)
	}

	bytes, err := json.Marshal(restaurant)
	if err != nil {
		return errorResponder(err)
	}

	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))

	return responder
}
Esempio n. 6
0
func AddItem(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

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

	item := dbi.NewItem()
	item.Name = req.FormValue("name")
	item.Desc = req.FormValue("desc")

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

	return itemJsonResponder(item)
}
Esempio n. 7
0
func AddMenu(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

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

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

	menu := dbm.NewMenu()
	menu.Name = name
	menu.Save(rkey)

	return menuJsonResponder(menu)
}
Esempio n. 8
0
func itemJsonResponder(item interface{}) *handlers.JsonResponder {
	bytes, err := json.Marshal(item)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))
	return responder
}
Esempio n. 9
0
func menuJsonResponder(menu *dbm.Menu) *handlers.JsonResponder {
	bytes, err := json.Marshal(menu)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))
	return responder
}
Esempio n. 10
0
func Restaurant(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

	rid := req.FormValue("rid")
	if rid == "" {
		return handlers.ErrorJsonResponder("Restaurant Id (rid) is required")
	}

	res, err := dbr.GetRestaurantById(rid)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	bytes, err := json.Marshal(res)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))

	return responder
}
Esempio n. 11
0
func EditBranch(r *handlers.HttpRequest) *handlers.JsonResponder {
	req := r.Request

	rid := req.FormValue("rid")
	bidx := req.FormValue("bidx")
	if rid == "" || bidx == "" {
		return handlers.ErrorJsonResponder("Restaurant Id (rid) and Branch Index (bidx) are required")
	}
	res, err := dbr.GetRestaurantById(rid)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}
	bidxInt, err := strconv.Atoi(bidx)
	if err != nil {
		return handlers.ErrorJsonResponder("Invalid Branch Idx - " + bidx)
	}
	if len(res.Branches) <= bidxInt {
		return handlers.ErrorJsonResponder("Invalid branch index")
	}
	b := res.Branches[bidxInt]
	if name := req.FormValue("bname"); name != "" {
		b.Name = name
	}
	if address := req.FormValue("address"); address != "" {
		b.Address = address
	}
	if city := req.FormValue("city"); city != "" {
		b.City = city
	}
	if state := req.FormValue("state"); state != "" {
		b.State = state
	}
	if zipcode := req.FormValue("zipcode"); zipcode != "" {
		b.Zipcode = zipcode
	}

	res.Branches[bidxInt] = b
	if err := res.Save(); err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}

	bytes, err := json.Marshal(res)
	if err != nil {
		return handlers.ErrorJsonResponder(err.Error())
	}
	responder := &handlers.JsonResponder{}
	responder.SetStatus(http.StatusOK)
	responder.Write(string(bytes))

	return responder

}
Esempio n. 12
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)
}