feat(api): enhance community retrieval with flexible ID handling
Updated the GetCommunity function to accept both UUIDs and community titles for improved flexibility in community retrieval. Added error handling for invalid ID formats and adjusted related functions to ensure consistent behavior across memory and PostgreSQL storage. This change enhances the API's usability by allowing more intuitive community lookups.
This commit is contained in:
@@ -585,20 +585,36 @@ func (m *Memory) ListCommunities(tenantID string) ([]*Community, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) GetCommunity(tenantID, id string) (*Community, error) {
|
||||
func (m *Memory) GetCommunity(tenantID, idOrKey string) (*Community, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
c, ok := m.communities[id]
|
||||
if !ok || c.TenantID != tenantID {
|
||||
key := strings.TrimSpace(idOrKey)
|
||||
if key == "" {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return c, nil
|
||||
if c, ok := m.communities[key]; ok && c.TenantID == tenantID {
|
||||
return c, nil
|
||||
}
|
||||
for _, c := range m.communities {
|
||||
if c.TenantID != tenantID {
|
||||
continue
|
||||
}
|
||||
if c.Community == key || c.Title == key {
|
||||
return c, nil
|
||||
}
|
||||
if strings.TrimSpace(c.Title) != "" && c.Community+" · "+c.Title == key {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
func (m *Memory) ListCommunityPrefixes(tenantID, communityID, cursor string, limit int) ([]PrefixRow, string, bool, error) {
|
||||
if _, err := m.GetCommunity(tenantID, communityID); err != nil {
|
||||
commRow, err := m.GetCommunity(tenantID, communityID)
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
resolvedID := commRow.ID
|
||||
if limit <= 0 {
|
||||
limit = 500
|
||||
}
|
||||
@@ -626,10 +642,9 @@ func (m *Memory) ListCommunityPrefixes(tenantID, communityID, cursor string, lim
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
var all []PrefixRow
|
||||
comm := communityID
|
||||
for _, rev := range latestByModule {
|
||||
for _, pr := range m.revPrefixes[rev.ID] {
|
||||
if pr.CommunityID == nil || *pr.CommunityID != communityID {
|
||||
if pr.CommunityID == nil || *pr.CommunityID != resolvedID {
|
||||
continue
|
||||
}
|
||||
pfx := strings.TrimSpace(pr.Prefix)
|
||||
@@ -640,7 +655,7 @@ func (m *Memory) ListCommunityPrefixes(tenantID, communityID, cursor string, lim
|
||||
continue
|
||||
}
|
||||
seen[pfx] = struct{}{}
|
||||
all = append(all, PrefixRow{Prefix: pfx, CommunityID: &comm, Source: pr.Source})
|
||||
all = append(all, PrefixRow{Prefix: pfx, CommunityID: &resolvedID, Source: pr.Source})
|
||||
}
|
||||
}
|
||||
sort.Slice(all, func(i, j int) bool { return all[i].Prefix < all[j].Prefix })
|
||||
|
||||
Reference in New Issue
Block a user