|
|
@ -81,12 +81,12 @@ func SaveUserToken(ctx context.Context, app *core.App, spotifyID string, token *
|
|
|
|
|
|
|
|
|
|
|
|
coll := app.MongoClient.Database("listy").Collection("users")
|
|
|
|
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) {
|
|
|
|
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %w", err)
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err == mongo.ErrNoDocuments {
|
|
|
|
if user == nil {
|
|
|
|
doc := models.User{Token: *token, SpotifyID: spotifyID}
|
|
|
|
doc := models.User{Token: *token, SpotifyID: spotifyID}
|
|
|
|
_, err = coll.InsertOne(context.TODO(), doc)
|
|
|
|
_, 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)
|
|
|
|
return nil, fmt.Errorf("couldn't write user %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} 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 {
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't write user %w", err)
|
|
|
|
return nil, fmt.Errorf("couldn't write user %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result.Token = *token
|
|
|
|
user.Token = *token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|