feat: implement entity and wiki management services, repositories, and routes with associated controllers
Build and Release / release (push) Successful in 1m34s
Build and Release / release (push) Successful in 1m34s
This commit is contained in:
@@ -27,6 +27,7 @@ type EntityRepository interface {
|
||||
Delete(ctx context.Context, id pgtype.UUID) error
|
||||
DeleteByIDs(ctx context.Context, ids []pgtype.UUID) error
|
||||
GetByProjectID(ctx context.Context, projectID pgtype.UUID) ([]*models.EntityEntity, error)
|
||||
GetEntityIDsByGeometryIDs(ctx context.Context, geometryIDs []string) (map[string][]string, error)
|
||||
WithTx(tx pgx.Tx) EntityRepository
|
||||
}
|
||||
|
||||
@@ -412,3 +413,62 @@ func (r *entityRepository) GetBySlugs(ctx context.Context, slugs []string) ([]*m
|
||||
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *entityRepository) GetEntityIDsByGeometryIDs(ctx context.Context, geometryIDs []string) (map[string][]string, error) {
|
||||
if len(geometryIDs) == 0 {
|
||||
return make(map[string][]string), nil
|
||||
}
|
||||
|
||||
keys := make([]string, len(geometryIDs))
|
||||
for i, id := range geometryIDs {
|
||||
keys[i] = fmt.Sprintf("entity_geometries:geometry:%s", id)
|
||||
}
|
||||
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
result := make(map[string][]string)
|
||||
var missingGeometryIDs []string
|
||||
var missingPgIDs []pgtype.UUID
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var entityIDs []string
|
||||
if err := json.Unmarshal(b, &entityIDs); err == nil {
|
||||
result[geometryIDs[i]] = entityIDs
|
||||
continue
|
||||
}
|
||||
}
|
||||
missingGeometryIDs = append(missingGeometryIDs, geometryIDs[i])
|
||||
pgID, err := convert.StringToUUID(geometryIDs[i])
|
||||
if err == nil {
|
||||
missingPgIDs = append(missingPgIDs, pgID)
|
||||
}
|
||||
}
|
||||
|
||||
if len(missingPgIDs) > 0 {
|
||||
rows, err := r.q.GetEntityIDsByGeometryIDs(ctx, missingPgIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbMap := make(map[string][]string)
|
||||
for _, id := range missingGeometryIDs {
|
||||
dbMap[id] = []string{}
|
||||
}
|
||||
for _, row := range rows {
|
||||
gID := convert.UUIDToString(row.GeometryID)
|
||||
eID := convert.UUIDToString(row.EntityID)
|
||||
dbMap[gID] = append(dbMap[gID], eID)
|
||||
}
|
||||
|
||||
missingToCache := make(map[string]any)
|
||||
for gID, eIDs := range dbMap {
|
||||
result[gID] = eIDs
|
||||
missingToCache[fmt.Sprintf("entity_geometries:geometry:%s", gID)] = eIDs
|
||||
}
|
||||
if len(missingToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, missingToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user