mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-26 22:39:07 +01:00
update get group by name
This commit is contained in:
@@ -1771,9 +1771,6 @@ func (s *SqlStore) GetGroupByID(ctx context.Context, lockStrength LockingStrengt
|
|||||||
// GetGroupByName retrieves a group by name and account ID.
|
// GetGroupByName retrieves a group by name and account ID.
|
||||||
func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStrength, accountID, groupName string) (*types.Group, error) {
|
func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStrength, accountID, groupName string) (*types.Group, error) {
|
||||||
tx := s.db
|
tx := s.db
|
||||||
if lockStrength != LockingStrengthNone {
|
|
||||||
tx = tx.Clauses(clause.Locking{Strength: string(lockStrength)})
|
|
||||||
}
|
|
||||||
|
|
||||||
var group types.Group
|
var group types.Group
|
||||||
|
|
||||||
@@ -1781,16 +1778,14 @@ func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStren
|
|||||||
// we may need to reconsider changing the types.
|
// we may need to reconsider changing the types.
|
||||||
query := tx.Preload(clause.Associations)
|
query := tx.Preload(clause.Associations)
|
||||||
|
|
||||||
switch s.storeEngine {
|
result := query.
|
||||||
case types.PostgresStoreEngine:
|
Model(&types.Group{}).
|
||||||
query = query.Order("json_array_length(peers::json) DESC")
|
Joins("LEFT JOIN group_peers ON group_peers.group_id = groups.id").
|
||||||
case types.MysqlStoreEngine:
|
Where("groups.account_id = ? AND groups.name = ?", accountID, groupName).
|
||||||
query = query.Order("JSON_LENGTH(JSON_EXTRACT(peers, \"$\")) DESC")
|
Group("groups.id").
|
||||||
default:
|
Order("COUNT(group_peers.peer_id) DESC").
|
||||||
query = query.Order("json_array_length(peers) DESC")
|
Limit(1).
|
||||||
}
|
First(&group)
|
||||||
|
|
||||||
result := query.Preload(clause.Associations).First(&group, "account_id = ? AND name = ?", accountID, groupName)
|
|
||||||
if err := result.Error; err != nil {
|
if err := result.Error; err != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
return nil, status.NewGroupNotFoundError(groupName)
|
return nil, status.NewGroupNotFoundError(groupName)
|
||||||
|
|||||||
Reference in New Issue
Block a user