@@ -77,32 +77,24 @@ func (i CreateUserGroupInput) validateUserGroupInput() error {
77
77
}
78
78
79
79
func AddUsersToGroup (tx * database.Transactable , userSlugs []string , groupID int64 ) error {
80
- if len (userSlugs ) > 0 {
81
- questionMarks := "("
82
-
83
- interfaceSlice := make ([]interface {}, len (userSlugs )+ 1 )
84
- for i , v := range userSlugs {
85
- questionMarks += "?, "
86
- interfaceSlice [i ] = v
87
- }
80
+ if len (userSlugs ) == 0 {
81
+ return nil
82
+ }
88
83
89
- questionMarks = strings .TrimSuffix (questionMarks , ", " )
90
- questionMarks += ")"
84
+ peopleToAdd := sq .Select ("id" , strconv .FormatInt (groupID , 10 )).
85
+ From ("users" ).
86
+ Where (sq.Eq {"slug" : userSlugs })
91
87
92
- sqlStatement := fmt . Sprintf ( `INSERT IGNORE INTO group_user_map(user_id, group_id)
93
- SELECT users.id, user_groups.id
94
- FROM users, user_groups
95
- WHERE users.slug in %s and user_groups.id = ?;` , questionMarks )
88
+ insertQuery := sq .
89
+ Insert ( "group_user_map" ).
90
+ Options ( "Ignore" ).
91
+ Columns ( "user_id" , "group_id" ). Select ( peopleToAdd )
96
92
97
- interfaceSlice [len (userSlugs )] = groupID
98
- err := tx .Exec (sq .Expr (sqlStatement , interfaceSlice ... ))
93
+ err := tx .Exec (insertQuery )
99
94
100
- if err != nil {
101
- return backend .WrapError ("Unable to add users to group" , backend .DatabaseErr (err ))
102
- }
103
- return nil
95
+ if err != nil {
96
+ return backend .WrapError ("Unable to add users to group" , backend .DatabaseErr (err ))
104
97
}
105
-
106
98
return nil
107
99
}
108
100
0 commit comments