Ejemplo n.º 1
0
func (s *ProductService) UpdateProduct(product *model.Product) {
	if product == nil {
		return
	}
	// update product information
	product.Capital = s.getCapital(product.Name)
	if _, err := productdao.UpdateProduct(product); err != nil {
		panic(err.Error())
	}

	// update it's properties
	if product.Colors != nil {
		productdao.UpdateProductProperties(product.Id, "color", product.Colors...)
	}
	if product.Sizes != nil {
		productdao.UpdateProductProperties(product.Id, "size", product.Sizes...)
	}

	// update stock information
	if product.Stocks != nil {
		// TODO change to edit/create/delete;
		inventorydao.ClearProductStock(product.Id) // clear
		product.Stocks.Loop(func(color, size string, stock int) {
			inventorydao.SetProductStock(product.Id, color, size, stock)
		})
	}

	// update suggest
	fmt.Println("^^^^^^ update product, ", product)
	fmt.Println("Productid is: ", product.ProductId)
	suggest.Update(suggest.Product, product.Name, product.Id, product.ProductId)

}
Ejemplo n.º 2
0
//
// CreateProduct create a new Product into database, including it's properties.
//
func (s *ProductService) CreateProduct(product *model.Product) (*model.Product, error) {
	if product == nil {
		panic("Product can't be null!")
	}

	product.Capital = s.getCapital(product.Name)
	newProduct, err := productdao.Create(product)
	if err != nil {
		return nil, err
	}
	// newProduct := dal.CreateProduct(product)
	if product.Colors != nil {
		productdao.UpdateProductProperties(newProduct.Id, "color", product.Colors...)
	}
	if product.Sizes != nil {
		productdao.UpdateProductProperties(newProduct.Id, "size", product.Sizes...)
	}

	// update suggest
	suggest.Add(suggest.Product, newProduct.Name, newProduct.Id, product.ProductId)

	return newProduct, nil
}