account page: fixed platform reordering bugs

This commit is contained in:
2026-01-28 02:14:27 -07:00
parent a50c0ef18d
commit 8f38f141e5
8 changed files with 141 additions and 72 deletions
+19 -9
View File
@@ -12,10 +12,15 @@ import (
)
// TODO: simpify, if possible (single query ideal)
func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int64, platform Platform, orderIndex int) error {
func (db *Store) SetOrderOfPlatformOnAccountPage(
ctx context.Context,
acctID int64,
platform Platform,
orderIndex int,
) (orderedPlatforms []Platform, prevOrderIndex int, err error) {
tx, err := db.db.Begin(ctx)
if err != nil {
return fmt.Errorf("failed to start transaction: %w", err)
return nil, 0, fmt.Errorf("failed to start transaction: %w", err)
}
defer tx.Rollback(ctx)
@@ -39,7 +44,7 @@ func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int
},
)
if err != nil {
return fmt.Errorf("failed to perform query to look up existing indexes: %w", err)
return nil, 0, fmt.Errorf("failed to perform query to look up existing indexes: %w", err)
}
indexes, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
@@ -47,7 +52,7 @@ func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int
Order_index int
}])
if err != nil {
return fmt.Errorf("failed to scan rows for query to look up existing indexes: %w", err)
return nil, 0, fmt.Errorf("failed to scan rows for query to look up existing indexes: %w", err)
}
// compute the implied indexes,
@@ -74,9 +79,13 @@ func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int
}
}
prevOrderIndex := indexPerPlatform[platform]
prevOrderIndex = indexPerPlatform[platform]
orderedPlatforms = make([]Platform, len(allPlatforms))
if indexPerPlatform[platform] == orderIndex {
return nil
for i := range allPlatforms {
orderedPlatforms[i] = platformPerIndex[i]
}
return orderedPlatforms, prevOrderIndex, nil
}
if increased := orderIndex > prevOrderIndex; increased {
@@ -110,6 +119,7 @@ func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int
valuesLines[i] = fmt.Sprintf("(@account_id, @platform_%d, @order_index_%d::smallint)", i, i)
args[fmt.Sprintf("platform_%d", i)] = p
args[fmt.Sprintf("order_index_%d", i)] = indexPerPlatform[p]
orderedPlatforms[i] = platformPerIndex[i]
}
_, err = tx.Exec(
@@ -148,14 +158,14 @@ func (db *Store) SetOrderOfPlatformOnAccountPage(ctx context.Context, acctID int
args,
)
if err != nil {
return fmt.Errorf("failed to perform query to delete old indexes and insert new indexes: %w", err)
return nil, 0, fmt.Errorf("failed to perform query to delete old indexes and insert new indexes: %w", err)
}
if tx.Commit(ctx); err != nil {
return fmt.Errorf("failed to commit txn: %w", err)
return nil, 0, fmt.Errorf("failed to commit txn: %w", err)
}
return nil
return orderedPlatforms, prevOrderIndex, nil
}
func (db *Store) GetOrderOfPlatformsOnAccountPage(ctx context.Context, acctID int64) ([]Platform, error) {