2026-04-16 09:04:25 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
//"github.com/aarondl/opt/omit"
|
|
|
|
|
//"github.com/aarondl/opt/omitnull"
|
2026-04-16 10:15:28 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob"
|
2026-04-16 09:04:25 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-04-16 10:15:28 +00:00
|
|
|
"github.com/stephenafamo/scan"
|
2026-04-16 09:04:25 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-16 19:49:18 +00:00
|
|
|
func FeaturesForSite(ctx context.Context, site_id int32) ([]types.Feature, error) {
|
|
|
|
|
features, err := featuresBySiteID(ctx, []int32{site_id})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("features by site ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return features[site_id], nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 09:04:25 +00:00
|
|
|
func featuresBySiteID(ctx context.Context, site_ids []int32) (map[int32][]types.Feature, error) {
|
2026-04-16 10:15:28 +00:00
|
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
|
|
|
|
sm.Columns(
|
|
|
|
|
"feature.id AS id",
|
|
|
|
|
"feature.site_id AS site_id",
|
2026-04-16 20:39:30 +00:00
|
|
|
"COALESCE(feature.location_longitude, 0) AS \"location.longitude\"",
|
|
|
|
|
"COALESCE(feature.location_latitude, 0) AS \"location.latitude\"",
|
2026-04-16 10:15:28 +00:00
|
|
|
"'pool' AS type",
|
|
|
|
|
),
|
|
|
|
|
sm.From("feature"),
|
|
|
|
|
sm.InnerJoin("feature_pool").OnEQ(
|
|
|
|
|
psql.Quote("feature", "id"),
|
|
|
|
|
psql.Quote("feature_pool", "feature_id"),
|
|
|
|
|
),
|
|
|
|
|
sm.Where(
|
2026-04-16 19:49:18 +00:00
|
|
|
models.Features.Columns.SiteID.EQ(psql.Any(site_ids)),
|
2026-04-16 10:15:28 +00:00
|
|
|
),
|
|
|
|
|
), scan.StructMapper[types.Feature]())
|
2026-04-16 09:04:25 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query features: %w", err)
|
|
|
|
|
}
|
|
|
|
|
results := make(map[int32][]types.Feature, len(site_ids))
|
|
|
|
|
for _, site_id := range site_ids {
|
|
|
|
|
results[site_id] = make([]types.Feature, 0)
|
|
|
|
|
}
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
features, ok := results[row.SiteID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("impossible")
|
|
|
|
|
}
|
2026-04-16 20:39:30 +00:00
|
|
|
features = append(features, row)
|
2026-04-16 09:04:25 +00:00
|
|
|
results[row.SiteID] = features
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|