Skip to content

Instantly share code, notes, and snippets.

@longfellowone
Created May 10, 2019 00:50
Show Gist options
  • Save longfellowone/cc02fb397d408adc944b74dfd1d0048b to your computer and use it in GitHub Desktop.
Save longfellowone/cc02fb397d408adc944b74dfd1d0048b to your computer and use it in GitHub Desktop.
func (r *mutationResolver) UpdateUser(ctx context.Context, input UpdateUser) (User, error) {
var fields = bson.M{}
var user User
update := false
if input.First != nil && *input.First != "" {
fields["first"] = *input.First
update = true
}
if input.Last != nil && *input.Last != "" {
fields["last"] = *input.Last
update = true
}
if input.Email != nil && *input.Email != "" {
fields["email"] = *input.Email
update = true
}
if !update {
return User{}, errors.New("no fields present for updating data")
}
err := r.users.UpdateId(bson.ObjectIdHex(input.ID), fields)
if err != nil {
return User{}, err
}
err = r.users.Find(bson.M{"_id": bson.ObjectIdHex(input.ID)}).One(&user)
if err != nil {
return User{}, err
}
user.ID = bson.ObjectId(user.ID).Hex()
return user, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment