Example #1
0
// NewService creates a new store Service which handles websites, groups and stores.
// A Service can only act on a certain scope (MAGE_RUN_TYPE) and scope ID (MAGE_RUN_CODE).
// Default scope.Scope is always the scope.WebsiteID constant.
// This function is mainly used when booting the app to set the environment configuration
// Also all other calls to any method receiver with nil arguments depends on the internal
// appStore which reflects the default store ID.
func NewService(so scope.Option, storage Storager, opts ...ServiceOption) (*Service, error) {
	scopeID := so.Scope()
	if scopeID == scope.DefaultID {
		scopeID = scope.WebsiteID
	}

	s := &Service{
		cr:           config.DefaultManager,
		boundToScope: scopeID,
		storage:      storage,
		mu:           sync.RWMutex{},
		websiteMap:   make(map[uint64]*Website),
		groupMap:     make(map[uint64]*Group),
		storeMap:     make(map[uint64]*Store),
	}
	for _, opt := range opts {
		if opt != nil {
			opt(s)
		}
	}

	var err error
	s.appStore, err = s.findDefaultStoreByScope(s.boundToScope, so)
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.Service.Init", "err", err, "ScopeOption", so)
		}
		return nil, errgo.Mask(err)
	}

	return s, nil
}
Example #2
0
// RequestedStore see interface description Reader.RequestedStore
func (sm *Service) RequestedStore(so scope.Option) (activeStore *Store, err error) {

	activeStore, err = sm.findDefaultStoreByScope(so.Scope(), so)
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("store.Service.RequestedStore.FindDefaultStoreByScope", "err", err, "so", so)
		}
		return nil, err
	}

	//	activeStore, err = sm.newActiveStore(activeStore) // this is the active store from a request.
	// todo rethink here if we really need a newActiveStore
	// newActiveStore creates a new Store, Website and Group pointers !!!
	//	if activeStore == nil || err != nil {
	//		// store is not active so ignore
	//		return nil, err
	//	}

	if false == activeStore.Data.IsActive {
		return nil, ErrStoreNotActive
	}

	allowStoreChange := false
	switch sm.boundToScope {
	case scope.StoreID:
		allowStoreChange = true
		break
	case scope.GroupID:
		allowStoreChange = activeStore.Data.GroupID == sm.appStore.Data.GroupID
		break
	case scope.WebsiteID:
		allowStoreChange = activeStore.Data.WebsiteID == sm.appStore.Data.WebsiteID
		break
	}

	if allowStoreChange {
		return activeStore, nil
	}
	return nil, ErrStoreChangeNotAllowed
}
Example #3
0
func testStoreCodeFrom(t *testing.T, i int, haveErr, wantErr error, haveScope scope.Option, wantScope scope.Scope, wantCode string, wantID int64) {
	if wantErr != nil {
		assert.EqualError(t, haveErr, wantErr.Error(), "Index: %d", i)

	}
	switch sos := haveScope.Scope(); sos {
	case scope.StoreID:
		assert.Exactly(t, wantID, haveScope.Store.StoreID(), "Index: %d", i)
	case scope.GroupID:
		assert.Exactly(t, wantID, haveScope.Group.GroupID(), "Index: %d", i)
	case scope.WebsiteID:
		assert.Exactly(t, wantID, haveScope.Website.WebsiteID(), "Index: %d", i)
	case scope.DefaultID:
		assert.Nil(t, haveScope.Store, "Index: %d", i)
		assert.Nil(t, haveScope.Group, "Index: %d", i)
		assert.Nil(t, haveScope.Website, "Index: %d", i)
	default:
		t.Fatalf("Unknown scope: %d", sos)
	}
	assert.Exactly(t, wantScope, haveScope.Scope(), "Index: %d", i)
	assert.Exactly(t, wantCode, haveScope.StoreCode(), "Index: %d", i)
}
Example #4
0
func TestApplyDefault(t *testing.T) {
	so := scope.Option{}
	assert.NotNil(t, so)
	assert.Exactly(t, scope.DefaultID, so.Scope())
}