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
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ type WikiRepository interface {
|
||||
GetContentCountByWikiID(ctx context.Context, wikiID pgtype.UUID) (int64, error)
|
||||
GetContentByID(ctx context.Context, id pgtype.UUID) (*models.WikiContentEntity, error)
|
||||
GetContentByIDs(ctx context.Context, ids []string) ([]*models.WikiContentEntity, error)
|
||||
GetWikiIDsByEntityIDs(ctx context.Context, entityIDs []string) (map[string][]string, error)
|
||||
WithTx(tx pgx.Tx) WikiRepository
|
||||
}
|
||||
|
||||
@@ -638,3 +639,62 @@ func (r *wikiRepository) GetContentByID(ctx context.Context, id pgtype.UUID) (*m
|
||||
func (r *wikiRepository) GetContentByIDs(ctx context.Context, ids []string) ([]*models.WikiContentEntity, error) {
|
||||
return r.getContentByIDsWithFallback(ctx, ids)
|
||||
}
|
||||
|
||||
func (r *wikiRepository) GetWikiIDsByEntityIDs(ctx context.Context, entityIDs []string) (map[string][]string, error) {
|
||||
if len(entityIDs) == 0 {
|
||||
return make(map[string][]string), nil
|
||||
}
|
||||
|
||||
keys := make([]string, len(entityIDs))
|
||||
for i, id := range entityIDs {
|
||||
keys[i] = fmt.Sprintf("entity_wikis:entity:%s", id)
|
||||
}
|
||||
|
||||
raws := r.c.MGet(ctx, keys...)
|
||||
result := make(map[string][]string)
|
||||
var missingEntityIDs []string
|
||||
var missingPgIDs []pgtype.UUID
|
||||
|
||||
for i, b := range raws {
|
||||
if len(b) > 0 {
|
||||
var wikiIDs []string
|
||||
if err := json.Unmarshal(b, &wikiIDs); err == nil {
|
||||
result[entityIDs[i]] = wikiIDs
|
||||
continue
|
||||
}
|
||||
}
|
||||
missingEntityIDs = append(missingEntityIDs, entityIDs[i])
|
||||
pgID, err := convert.StringToUUID(entityIDs[i])
|
||||
if err == nil {
|
||||
missingPgIDs = append(missingPgIDs, pgID)
|
||||
}
|
||||
}
|
||||
|
||||
if len(missingPgIDs) > 0 {
|
||||
rows, err := r.q.GetWikiIDsByEntityIDs(ctx, missingPgIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbMap := make(map[string][]string)
|
||||
for _, id := range missingEntityIDs {
|
||||
dbMap[id] = []string{}
|
||||
}
|
||||
for _, row := range rows {
|
||||
eID := convert.UUIDToString(row.EntityID)
|
||||
wID := convert.UUIDToString(row.WikiID)
|
||||
dbMap[eID] = append(dbMap[eID], wID)
|
||||
}
|
||||
|
||||
missingToCache := make(map[string]any)
|
||||
for eID, wIDs := range dbMap {
|
||||
result[eID] = wIDs
|
||||
missingToCache[fmt.Sprintf("entity_wikis:entity:%s", eID)] = wIDs
|
||||
}
|
||||
if len(missingToCache) > 0 {
|
||||
_ = r.c.MSet(ctx, missingToCache, constants.NormalCacheDuration)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user