Esempio n. 1
0
func (p *InMemoryDataStore) DeleteConsumerAccount(user *dm.Consumer) (*dm.Consumer, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		return user, dm.ERR_MUST_SPECIFY_ID
	}
	oldUserI, _ := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id)
	var oldUser *dm.Consumer = nil
	if oldUserI != nil {
		oldUser = oldUserI.(*dm.Consumer)
	}
	if err := user.BeforeDelete(); err != nil {
		return user, err
	}
	if oldUser != nil {
		p.delete(_INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id)
		p.delete(_INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, oldUser.ShortName)
		p.removeFromStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_NAME_COLLECTION_NAME, oldUser.Name, user.Id)
		p.removeFromStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_DOMAIN_NAME_COLLECTION_NAME, oldUser.DomainName, user.Id)
	}
	if err := user.AfterDelete(); err != nil {
		return user, err
	}
	return user, nil
}
Esempio n. 2
0
func (p *InMemoryDataStore) CreateConsumerAccount(user *dm.Consumer) (*dm.Consumer, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		user.Id = p.generateIdForAccount(_INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME)
	}
	if _, ok := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_ID
	}
	if _, ok := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, user.ShortName); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_SHORTNAME
	}
	if err := user.BeforeCreate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, user.ShortName, user.Id)
	p.addToStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_NAME_COLLECTION_NAME, user.Name, user.Id, user.Id)
	p.addToStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_DOMAIN_NAME_COLLECTION_NAME, user.DomainName, user.Id, user.Id)
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterCreate(); err != nil {
		return user, err
	}
	return user, nil
}
Esempio n. 3
0
func (p *InMemoryDataStore) UpdateConsumerAccount(user *dm.Consumer) (*dm.Consumer, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		return user, dm.ERR_MUST_SPECIFY_ID
	}
	oldUserI, _ := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id)
	var oldUser *dm.Consumer = nil
	if oldUserI != nil {
		oldUser = oldUserI.(*dm.Consumer)
	}
	if oldUser == nil || oldUser.ShortName != user.ShortName {
		if _, ok := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, user.ShortName); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_SHORTNAME
		}
	}
	if oldUser == nil || oldUser.DomainName != user.DomainName {
		if _, ok := p.retrieve(_INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_DOMAIN_NAME_COLLECTION_NAME, user.DomainName); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_DOMAIN_NAME
		}
	}
	if err := user.BeforeUpdate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_CONSUMER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, user.ShortName, user.Id)
	p.addToStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_NAME_COLLECTION_NAME, user.Name, user.Id, user.Id)
	p.addToStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_DOMAIN_NAME_COLLECTION_NAME, user.DomainName, user.Id, user.Id)
	if oldUser != nil {
		if oldUser.ShortName != user.ShortName {
			p.delete(_INMEMORY_CONSUMER_ACCOUNT_ID_FOR_SHORTNAME_COLLECTION_NAME, oldUser.ShortName)
		}
		if oldUser.Name != user.Name {
			p.removeFromStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_NAME_COLLECTION_NAME, oldUser.Name, user.Id)
		}
		if oldUser.DomainName != user.DomainName {
			p.removeFromStringMapCollection(user.Id, _INMEMORY_CONSUMER_ACCOUNT_IDS_FOR_DOMAIN_NAME_COLLECTION_NAME, oldUser.DomainName, user.Id)
		}
	}
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterUpdate(); err != nil {
		return user, err
	}
	return user, nil
}