Skip to content

Commit

Permalink
fix: remove treecluster coords and region if no trees are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed Oct 17, 2024
1 parent 1fe6e8c commit ac7a9a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
21 changes: 19 additions & 2 deletions internal/service/domain/treecluster/geo_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,24 @@ func (s *GeoClusterLocator) UpdateCluster(ctx context.Context, clusterID *int32)
return err
}

treeIDs := utils.Map(cluster.Trees, func(t *entities.Tree) int32 {
if len(cluster.Trees) == 0 {
return s.removeClusterCoords(ctx, *clusterID)
}

return s.setClusterCoords(ctx, *clusterID, cluster.Trees)
}

func (s *GeoClusterLocator) removeClusterCoords(ctx context.Context, clusterID int32) error {
_, err := s.clusterRepo.Update(ctx, clusterID,
treecluster.WithLatitude(nil),
treecluster.WithLongitude(nil),
treecluster.WithRegion(nil),
)
return err
}

func (s *GeoClusterLocator) setClusterCoords(ctx context.Context, clusterID int32, trees []*entities.Tree) error {
treeIDs := utils.Map(trees, func(t *entities.Tree) int32 {
return t.ID
})

Expand All @@ -49,7 +66,7 @@ func (s *GeoClusterLocator) UpdateCluster(ctx context.Context, clusterID *int32)
return err
}

_, err = s.clusterRepo.Update(ctx, *clusterID,
_, err = s.clusterRepo.Update(ctx, clusterID,
treecluster.WithLatitude(&lat),
treecluster.WithLongitude(&long),
treecluster.WithRegion(region),
Expand Down
2 changes: 0 additions & 2 deletions internal/storage/postgres/region/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import (
func (r *RegionRepository) GetAll(ctx context.Context) ([]*entities.Region, error) {
rows, err := r.store.GetAllRegions(ctx)
if err != nil {
fmt.Printf("error: %v\n", err)
return nil, err
}
fmt.Printf("rows: %v\n", rows)

return r.mapper.FromSqlList(rows), nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/postgres/treecluster/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func (r *TreeClusterRepository) updateEntity(ctx context.Context, tc *entities.T
Name: tc.Name,
}

if err := r.store.UnlinkTreeClusterID(ctx, &tc.ID); err != nil {
return err
}

if len(tc.Trees) > 0 {
treeIDs := utils.Map(tc.Trees, func(t *entities.Tree) int32 {
return t.ID
})

if err := r.store.UnlinkTreeClusterID(ctx, &tc.ID); err != nil {
return err
}

if err := r.LinkTreesToCluster(ctx, tc.ID, treeIDs); err != nil {
return err
}
Expand Down

0 comments on commit ac7a9a7

Please sign in to comment.