Example #1
0
// All returns all the users in this collection.
func (u *Users) All() (map[string]User, error) {
	ref := firebase.NewReference(u.fbURL)
	if err := ref.Value(&u.data); err != nil {
		return nil, err
	}
	return u.data, nil
}
Example #2
0
// Update a user given an id and a "User" object.
func (u *Users) Update(id string, v *User) (*User, error) {
	if usr := u.GetByID(id); usr != nil {

		if v.Name != "" && usr.Name != v.Name {
			usr.Name = v.Name
		}

		for i := 0; i < len(v.Nicknames); i++ {
			if !usr.HasNickname(v.Nicknames[i]) {
				usr.Nicknames = append(usr.Nicknames, v.Nicknames[i])
			}

		}

		ref := firebase.NewReference(u.fbURL + id)

		if err := ref.Write(usr); err != nil {
			return nil, err
		}

		return usr, nil
	}

	return nil, nil
}
Example #3
0
// Get returns a user given an id.
func (u *Users) Get(id string) (*User, error) {
	ref := firebase.NewReference(u.fbURL + id).Export(false)
	usr := &User{}
	if err := ref.Value(usr); err != nil {
		return nil, err
	}
	return usr, nil
}
Example #4
0
// Delete a user with the specified name.
func (u *Users) Delete(name string) error {
	if id := u.GetUserID(name); id != "" {
		ref := firebase.NewReference(u.fbURL + id)
		if err := ref.Delete(); err != nil {
			return err
		}
		// also delete from the map
		delete(u.data, id)
	} else {
		return errors.New("user not found")
	}
	return nil
}
Example #5
0
// Add adds a new user to this collection.
func (u *Users) Add(usr *User) error {
	ref := firebase.NewReference(u.fbURL)

	if usr.Name == "" || len(usr.Nicknames) < 1 {
		return errors.New("Invalid user")
	}

	existing, err := u.GetByName(usr.Name)
	if err != nil {
		return err
	}

	if existing != nil {
		return errors.New("existing user")
	}

	if err := ref.Push(usr); err != nil {
		return err
	}

	return nil
}