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

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

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

	vendor.VendorKey = key

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

	reply.ID = vendor.ID
	reply.VendorName = vendor.VendorName
	reply.VendorDescription = vendor.VendorDescription
	reply.VendorKey = vendor.VendorKey
	reply.CreatedAt = vendor.CreatedAt
	reply.UpdatedAt = vendor.UpdatedAt

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

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

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

	vendor.VendorKey = key

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

	reply = vendor
	return nil
}
Ejemplo n.º 3
0
func setVendor(target *models.Vendor, src *models.Vendor) {
	target.ID = src.ID
	target.VendorName = src.VendorName
	target.VendorDescription = src.VendorDescription
	target.VendorKey = src.VendorKey
	target.CreatedAt = src.CreatedAt
	target.UpdatedAt = src.UpdatedAt
}
Ejemplo n.º 4
0
// SaveVendor will create a vendor if the ID field is not initialized
// if ID field is initialized, it will update the conresponding vendor.
func (r *Registry) SaveVendor(vendor *models.Vendor, reply *models.Vendor) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	if vendor.ID == 0 {
		// if ID field is not initialized, will generate key first
		err = db.Save(vendor).Error
		if err != nil {
			return err
		}

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

		vendor.VendorKey = key
	}

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

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

	setVendor(reply, vendor)

	return nil
}