|
|
|
@ -23,12 +23,12 @@ func GetUsers(ctx context.Context, app *core.App) ([]*models.User, error) {
|
|
|
|
|
|
|
|
|
|
cursor, err := coll.Find(context.TODO(), filter, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch users %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch users %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var results []*models.User
|
|
|
|
|
if err = cursor.All(ctx, &results); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch users %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch users %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
@ -43,7 +43,7 @@ func GetUser(ctx context.Context, app *core.App, spotifyID string) (*models.User
|
|
|
|
|
var result models.User
|
|
|
|
|
err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
@ -62,7 +62,7 @@ func GetSpotifyClient(ctx context.Context, app *core.App, user *models.User) (*s
|
|
|
|
|
_, err := coll.UpdateByID(ctx, user.ID, bson.D{{"$set", bson.D{{"token", *newToken}}}})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't update token %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't update token %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Token = *newToken
|
|
|
|
@ -79,7 +79,7 @@ func SaveUserToken(ctx context.Context, app *core.App, spotifyID string, token *
|
|
|
|
|
|
|
|
|
|
result, err := GetUser(ctx, app, spotifyID)
|
|
|
|
|
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't fetch user %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err == mongo.ErrNoDocuments {
|
|
|
|
@ -87,13 +87,13 @@ func SaveUserToken(ctx context.Context, app *core.App, spotifyID string, token *
|
|
|
|
|
_, err = coll.InsertOne(context.TODO(), doc)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't write user %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't write user %w", err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
_, err := coll.UpdateByID(ctx, result.ID, bson.D{{"$set", bson.D{{"token", *token}}}})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't write user %v", err)
|
|
|
|
|
return nil, fmt.Errorf("couldn't write user %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Token = *token
|
|
|
|
|