fix: 🐛 use nil check instead of error check
continuous-integration/drone/push Build is passing Details

main
RaviAnand Mohabir 1 year ago
parent 5b8b05feef
commit eddca837e6

@ -81,12 +81,12 @@ func SaveUserToken(ctx context.Context, app *core.App, spotifyID string, token *
coll := app.MongoClient.Database("listy").Collection("users")
result, err := GetUser(ctx, app, spotifyID)
user, err := GetUser(ctx, app, spotifyID)
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
return nil, fmt.Errorf("couldn't fetch user %w", err)
}
if err == mongo.ErrNoDocuments {
if user == nil {
doc := models.User{Token: *token, SpotifyID: spotifyID}
_, err = coll.InsertOne(context.TODO(), doc)
@ -94,14 +94,14 @@ func SaveUserToken(ctx context.Context, app *core.App, spotifyID string, token *
return nil, fmt.Errorf("couldn't write user %w", err)
}
} else {
_, err := coll.UpdateByID(ctx, result.ID, bson.D{{"$set", bson.D{{"token", *token}}}})
_, err := coll.UpdateByID(ctx, user.ID, bson.D{{"$set", bson.D{{"token", *token}}}})
if err != nil {
return nil, fmt.Errorf("couldn't write user %w", err)
}
result.Token = *token
user.Token = *token
}
return result, nil
return user, nil
}

Loading…
Cancel
Save