Example #1
0
// GetItem get an item by ID
func (h *StoreHandler) GetItem(rw http.ResponseWriter, req *http.Request) {
	var bind struct {
		ID string `mux:"id"`
	}
	api.Check(api.Bind(req, &bind))
	h.JSON(rw, h.storeItems[bind.ID])
}
Example #2
0
// PostItem adds an new item to store
func (h *StoreHandler) PostItem(rw http.ResponseWriter, req *http.Request) {
	var bind struct {
		Brand       string `json:"brand"`
		Name        string `json:"name"`
		Description string `json:"description"`
		ImageURL    string `json:"image,omitempty"`
	}

	// Get POST request binds
	api.Check(api.Bind(req, &bind))
	item := &model.StoreItem{
		ID:          uuid.NewV4().String(),
		Brand:       bind.Brand,
		Name:        bind.Name,
		Description: bind.Description,
		ImageURL:    bind.ImageURL,
	}
	log.Printf("Add new item, ID: %s, brand: %s, name: %s, description: %s, image: %s",
		item.ID, item.Brand, item.Name, item.Description, item.ImageURL)
	h.storeItems[item.ID] = item
	// Return 201 response with item ID in JSON
	h.JSONStatus(rw, 201, map[string]string{"id": item.ID})
}