Refactor ToGroupsInfo to process slices of groups

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga
2025-01-15 00:09:10 +03:00
parent 84aea32118
commit b15ee5c07c
3 changed files with 24 additions and 24 deletions

View File

@@ -82,7 +82,7 @@ func (h *handler) getAllNetworks(w http.ResponseWriter, r *http.Request) {
return
}
groups, err := h.groupsManager.GetAllGroups(r.Context(), accountID, userID)
groups, err := h.groupsManager.GetAllGroupsMap(r.Context(), accountID, userID)
if err != nil {
util.WriteError(r.Context(), err, w)
return
@@ -267,7 +267,7 @@ func (h *handler) collectIDsInNetwork(ctx context.Context, accountID, userID, ne
return nil, nil, 0, fmt.Errorf("failed to get routers in network: %w", err)
}
groups, err := h.groupsManager.GetAllGroups(ctx, accountID, userID)
groups, err := h.groupsManager.GetAllGroupsMap(ctx, accountID, userID)
if err != nil {
return nil, nil, 0, fmt.Errorf("failed to get groups: %w", err)
}

View File

@@ -72,13 +72,8 @@ func (h *Handler) getPeer(ctx context.Context, accountID, peerID, userID string,
}
dnsDomain := h.accountManager.GetDNSDomain()
groupsMap := map[string]*types.Group{}
grps, _ := h.accountManager.GetAllGroups(ctx, accountID, userID)
for _, group := range grps {
groupsMap[group.ID] = group
}
groupsInfo := groups.ToGroupsInfo(groupsMap, peerID)
grps, _ := h.accountManager.GetPeerGroups(ctx, accountID, peerID)
groupsInfo := groups.ToGroupsInfo(grps, peerID)
validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID)
if err != nil {
@@ -128,12 +123,7 @@ func (h *Handler) updatePeer(ctx context.Context, accountID, userID, peerID stri
return
}
groupsMap := map[string]*types.Group{}
for _, group := range peerGroups {
groupsMap[group.ID] = group
}
groupMinimumInfo := groups.ToGroupsInfo(groupsMap, peer.ID)
groupMinimumInfo := groups.ToGroupsInfo(peerGroups, peer.ID)
validPeers, err := h.accountManager.GetValidatedPeers(ctx, accountID)
if err != nil {
@@ -204,11 +194,7 @@ func (h *Handler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
dnsDomain := h.accountManager.GetDNSDomain()
groupsMap := map[string]*types.Group{}
grps, _ := h.accountManager.GetAllGroups(r.Context(), accountID, userID)
for _, group := range grps {
groupsMap[group.ID] = group
}
respBody := make([]*api.PeerBatch, 0, len(peers))
for _, peer := range peers {
@@ -217,7 +203,7 @@ func (h *Handler) GetAllPeers(w http.ResponseWriter, r *http.Request) {
util.WriteError(r.Context(), err, w)
return
}
groupMinimumInfo := groups.ToGroupsInfo(groupsMap, peer.ID)
groupMinimumInfo := groups.ToGroupsInfo(grps, peer.ID)
respBody = append(respBody, toPeerListItemResponse(peerToReturn, groupMinimumInfo, dnsDomain, 0))
}