Exemplo n.º 1
0
func (p *InMemoryDataStore) CreateUserAccount(user *dm.User) (*dm.User, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		user.Id = p.generateIdForAccount(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME)
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_ID
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
	}
	if err := user.BeforeCreate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterCreate(); err != nil {
		return user, err
	}
	return user, nil
}
Exemplo n.º 2
0
func (p *InMemoryDataStore) UpdateUserAccount(user *dm.User) (*dm.User, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		return user, dm.ERR_MUST_SPECIFY_ID
	}
	oldUserI, _ := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id)
	var oldUser *dm.User = nil
	if oldUserI != nil {
		oldUser = oldUserI.(*dm.User)
	}
	if oldUser == nil || oldUser.Username != user.Username {
		if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
		}
	}
	if oldUser == nil || oldUser.Email != user.Email {
		if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
		}
	}
	if err := user.BeforeUpdate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
	if oldUser != nil {
		if oldUser.Username != user.Username {
			p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, oldUser.Username)
		}
		if oldUser.Email != user.Email {
			p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, oldUser.Email)
		}
	}
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterUpdate(); err != nil {
		return user, err
	}
	return user, nil
}