Skip to content

Commit

Permalink
update resource change
Browse files Browse the repository at this point in the history
  • Loading branch information
futugyou committed Jun 25, 2024
1 parent 1031745 commit 02f3f55
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions infr-project/application/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,27 @@ func (s *ResourceService) UpdateResource(id string, aux models.UpdateResourceReq
var aggregate *resource.Resource
var aggregates = make([]resource.Resource, 0)
if source.Data != aux.Data {
aggregate, err = source.ChangeData(aux.Data)
if aggregate, err = source.ChangeData(aux.Data); err != nil {
return err
}
aggregates = append(aggregates, *aggregate)
}

if source.Name != aux.Name && err == nil {
if source.Name != aux.Name {
// TODO: check name in db
aggregate, err = source.ChangeName(aux.Name)
if aggregate, err = source.ChangeName(aux.Name); err != nil {
return err
}
aggregates = append(aggregates, *aggregate)
}

if !extensions.StringArrayCompare(aux.Tags, source.Tags) && err == nil {
aggregate, err = source.ChangeTags(aux.Tags)
if !extensions.StringArrayCompare(aux.Tags, source.Tags) {
if aggregate, err = source.ChangeTags(aux.Tags); err != nil {
return err
}
aggregates = append(aggregates, *aggregate)
}

if err != nil {
return err
}

if aggregate == nil || oldVersion == aggregate.Version {
return fmt.Errorf("the data in the resource has not changed")
}
Expand All @@ -83,6 +85,7 @@ func (s *ResourceService) UpdateResource(id string, aux models.UpdateResourceReq
for i := 0; i < len(aggregates); i++ {
aggs = append(aggs, &aggregates[i])
}

return s.service.withUnitOfWork(context.Background(), func(ctx context.Context) error {
return s.service.SaveSnapshotAndEvent2(ctx, aggs)
})
Expand Down

0 comments on commit 02f3f55

Please sign in to comment.