Skip to content

Commit

Permalink
refactoring update to use already available info
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeschuurmans committed Aug 2, 2024
1 parent 0ae5d70 commit b243e22
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
44 changes: 16 additions & 28 deletions pkg/api/v1/router_server_sku.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,13 @@ func (r *Router) updateServerSkuTransaction(ctx context.Context, sku *models.Ser
func (r *Router) updateServerSkuAuxDevices(ctx context.Context, tx *sql.Tx, sku *models.ServerSku, oldSku *models.ServerSku) error {
var oldAuxDevices []*models.ServerSkuAuxDevice
var auxDevices []*models.ServerSkuAuxDevice
var isOldAuxDevices []bool

if oldSku.R != nil {
oldAuxDevices = oldSku.R.SkuServerSkuAuxDevices
}

if sku.R != nil {
auxDevices = sku.R.SkuServerSkuAuxDevices
isOldAuxDevices = make([]bool, len(sku.R.SkuServerSkuAuxDevices))
}

// Find aux devices no longer present and remove them
Expand All @@ -317,7 +315,6 @@ func (r *Router) updateServerSkuAuxDevices(ctx context.Context, tx *sql.Tx, sku
auxDevices[i].Model == oldAuxDevice.Model &&
auxDevices[i].ID == "" {
auxDevices[i].ID = oldAuxDevice.ID
isOldAuxDevices[i] = true
auxDeviceFound = true
break
}
Expand All @@ -332,12 +329,12 @@ func (r *Router) updateServerSkuAuxDevices(ctx context.Context, tx *sql.Tx, sku
}

// Upsert aux devices
for i, auxDevice := range auxDevices {
for _, auxDevice := range auxDevices {
var err error
if isOldAuxDevices[i] {
_, err = auxDevice.Update(ctx, tx, boil.Infer())
} else {
if auxDevice.ID == "" {
err = auxDevice.Insert(ctx, tx, boil.Infer())
} else {
_, err = auxDevice.Update(ctx, tx, boil.Infer())
}
if err != nil {
return err
Expand All @@ -350,15 +347,13 @@ func (r *Router) updateServerSkuAuxDevices(ctx context.Context, tx *sql.Tx, sku
func (r *Router) updateServerSkuDisks(ctx context.Context, tx *sql.Tx, sku *models.ServerSku, oldSku *models.ServerSku) error {
var oldDisks []*models.ServerSkuDisk
var disks []*models.ServerSkuDisk
var isOldDisks []bool

if oldSku.R != nil {
oldDisks = oldSku.R.SkuServerSkuDisks
}

if sku.R != nil {
disks = sku.R.SkuServerSkuDisks
isOldDisks = make([]bool, len(sku.R.SkuServerSkuDisks))
}

// Find disks no longer present and remove them
Expand All @@ -369,7 +364,6 @@ func (r *Router) updateServerSkuDisks(ctx context.Context, tx *sql.Tx, sku *mode
disks[i].Model == oldDisk.Model &&
disks[i].ID == "" {
disks[i].ID = oldDisk.ID
isOldDisks[i] = true
diskFound = true
break
}
Expand All @@ -384,12 +378,12 @@ func (r *Router) updateServerSkuDisks(ctx context.Context, tx *sql.Tx, sku *mode
}

// Upsert disks
for i, disk := range disks {
for _, disk := range disks {
var err error
if isOldDisks[i] {
_, err = disk.Update(ctx, tx, boil.Infer())
} else {
if disk.ID == "" {
err = disk.Insert(ctx, tx, boil.Infer())
} else {
_, err = disk.Update(ctx, tx, boil.Infer())
}
if err != nil {
return err
Expand All @@ -402,15 +396,13 @@ func (r *Router) updateServerSkuDisks(ctx context.Context, tx *sql.Tx, sku *mode
func (r *Router) updateServerSkuMemories(ctx context.Context, tx *sql.Tx, sku *models.ServerSku, oldSku *models.ServerSku) error {
var oldMemorys []*models.ServerSkuMemory
var memories []*models.ServerSkuMemory
var isOldMemories []bool

if oldSku.R != nil {
oldMemorys = oldSku.R.SkuServerSkuMemories
}

if sku.R != nil {
memories = sku.R.SkuServerSkuMemories
isOldMemories = make([]bool, len(sku.R.SkuServerSkuMemories))
}

// Find memories no longer present and remove them
Expand All @@ -421,7 +413,6 @@ func (r *Router) updateServerSkuMemories(ctx context.Context, tx *sql.Tx, sku *m
memories[i].Model == oldMemory.Model &&
memories[i].ID == "" {
memories[i].ID = oldMemory.ID
isOldMemories[i] = true
memoryFound = true
break
}
Expand All @@ -436,12 +427,12 @@ func (r *Router) updateServerSkuMemories(ctx context.Context, tx *sql.Tx, sku *m
}

// Upsert memories
for i, memory := range memories {
for _, memory := range memories {
var err error
if isOldMemories[i] {
_, err = memory.Update(ctx, tx, boil.Infer())
} else {
if memory.ID == "" {
err = memory.Insert(ctx, tx, boil.Infer())
} else {
_, err = memory.Update(ctx, tx, boil.Infer())
}
if err != nil {
return err
Expand All @@ -454,15 +445,13 @@ func (r *Router) updateServerSkuMemories(ctx context.Context, tx *sql.Tx, sku *m
func (r *Router) updateServerSkuNics(ctx context.Context, tx *sql.Tx, sku *models.ServerSku, oldSku *models.ServerSku) error {
var oldNics []*models.ServerSkuNic
var nics []*models.ServerSkuNic
var isOldNics []bool

if oldSku.R != nil {
oldNics = oldSku.R.SkuServerSkuNics
}

if sku.R != nil {
nics = sku.R.SkuServerSkuNics
isOldNics = make([]bool, len(sku.R.SkuServerSkuNics))
}

// Find nics no longer present and remove them
Expand All @@ -473,7 +462,6 @@ func (r *Router) updateServerSkuNics(ctx context.Context, tx *sql.Tx, sku *model
nics[i].Model == oldNic.Model &&
nics[i].ID == "" {
nics[i].ID = oldNic.ID
isOldNics[i] = true
nicFound = true
break
}
Expand All @@ -488,12 +476,12 @@ func (r *Router) updateServerSkuNics(ctx context.Context, tx *sql.Tx, sku *model
}

// Upsert nics
for i, nic := range nics {
for _, nic := range nics {
var err error
if isOldNics[i] {
_, err = nic.Update(ctx, tx, boil.Infer())
} else {
if nic.ID == "" {
err = nic.Insert(ctx, tx, boil.Infer())
} else {
_, err = nic.Update(ctx, tx, boil.Infer())
}
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/router_server_sku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestIntegrationServerSkuUpdate(t *testing.T) {

sku := *resp.Record.(*fleetdbapi.ServerSku)

assert.Equal(t, ServerSkuTemp, sku)
assertServerSkuEqual(t, &ServerSkuTemp, &sku)
}
})
}
Expand Down

0 comments on commit b243e22

Please sign in to comment.