Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getGroupByName method #1481

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type AccountManager interface {
UpdatePeerSSHKey(peerID string, sshKey string) error
GetUsersFromAccount(accountID, userID string) ([]*UserInfo, error)
GetGroup(accountId, groupID string) (*Group, error)
GetGroupByName(groupName, accountID string) (*Group, error)
SaveGroup(accountID, userID string, group *Group) error
DeleteGroup(accountId, userId, groupID string) error
ListGroups(accountId string) ([]*Group, error)
Expand Down
33 changes: 33 additions & 0 deletions management/server/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,39 @@ func (am *DefaultAccountManager) GetGroup(accountID, groupID string) (*Group, er
return nil, status.Errorf(status.NotFound, "group with ID %s not found", groupID)
}

// GetGroupByName
pascal-fischer marked this conversation as resolved.
Show resolved Hide resolved
func (am *DefaultAccountManager) GetGroupByName(groupName, accountID string) (*Group, error) {
unlock := am.Store.AcquireAccountLock(accountID)
defer unlock()

account, err := am.Store.GetAccount(accountID)
if err != nil {
return nil, err
}

matchingGroups := make([]*Group, 0)
for _, group := range account.Groups {
if group.Name == groupName {
matchingGroups = append(matchingGroups, group)
}
}

if len(matchingGroups) == 0 {
return nil, status.Errorf(status.NotFound, "group with name %s not found", groupName)
}

maxPeers := -1
var groupWithMostPeers *Group
for i, group := range matchingGroups {
if len(group.Peers) > maxPeers {
maxPeers = len(group.Peers)
groupWithMostPeers = matchingGroups[i]
}
}

return groupWithMostPeers, nil
}

// SaveGroup object of the peers
func (am *DefaultAccountManager) SaveGroup(accountID, userID string, newGroup *Group) error {
unlock := am.Store.AcquireAccountLock(accountID)
Expand Down
10 changes: 10 additions & 0 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

// Contains checks if a slice of strings contains a specific string.
func Contains(slice []string, str string) bool {
pascal-fischer marked this conversation as resolved.
Show resolved Hide resolved
for _, item := range slice {
if item == str {
return true
}
}
return false
}
Loading