Ejemplo n.º 1
0
// SaveProduct will create a product if the ID field is not initialized
// if ID field is initialized, it will update the conresponding product.
func (r *Registry) SaveProduct(product *models.Product, reply *models.Product) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	err = db.Save(product).Error
	if err != nil {
		return err
	}

	key, err := r.keygen.GenRandomKey(int64(product.ID))
	if err != nil {
		return err
	}

	product.ProductKey = key

	err = db.Save(product).Error
	if err != nil {
		return err
	}

	reply.ID = product.ID
	reply.ProductName = product.ProductName
	reply.ProductDescription = product.ProductDescription
	reply.ProductKey = product.ProductKey
	reply.ProductConfig = product.ProductConfig
	reply.CreatedAt = product.CreatedAt
	reply.UpdatedAt = product.UpdatedAt

	return nil
}
Ejemplo n.º 2
0
// SaveProduct will create a product if the ID field is not initialized
// if ID field is initialized, it will update the conresponding product.
func (r *Registry) SaveProduct(product *models.Product, reply *models.Product) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	err = db.Save(product).Error
	if err != nil {
		return err
	}

	key, err := r.keygen.GenRandomKey(int64(product.ID))
	if err != nil {
		return err
	}

	product.ProductKey = key

	err = db.Save(product).Error
	if err != nil {
		return err
	}

	reply = product
	return nil
}
Ejemplo n.º 3
0
func setProduct(target *models.Product, src *models.Product) {
	target.ID = src.ID
	target.ProductName = src.ProductName
	target.ProductDescription = src.ProductDescription
	target.ProductKey = src.ProductKey
	target.ProductConfig = src.ProductConfig
	target.CreatedAt = src.CreatedAt
	target.UpdatedAt = src.UpdatedAt
}
Ejemplo n.º 4
0
// SaveProduct will create a product if the ID field is not initialized
// if ID field is initialized, it will update the conresponding product.
func (r *Registry) SaveProduct(product *models.Product, reply *models.Product) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	if product.ID == 0 {
		// create product
		err = db.Save(product).Error
		if err != nil {
			return err
		}

		key, err := r.keygen.GenRandomKey(int64(product.ID))
		if err != nil {
			return err
		}

		product.ProductKey = key
	}

	err = db.Save(product).Error
	if err != nil {
		return err
	}

	cache := getCache()
	cacheKey := fmt.Sprintf("Product:%v", product.ID)
	if _, ok := cache.Get(cacheKey); ok {
		cache.Delete(cacheKey)
	}

	setProduct(reply, product)

	return nil
}