-
-
Save longfellowone/cc02fb397d408adc944b74dfd1d0048b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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