@@ -4,14 +4,19 @@ import (
4
4
"encoding/json"
5
5
"errors"
6
6
"gorm.io/gorm"
7
+ "strconv"
8
+ "wejh-go/app/config"
7
9
"wejh-go/app/models"
8
10
"wejh-go/config/database"
9
11
)
10
12
11
- func AddThemePermission (themeID int , reqStudentIDs []string ) ([]string , error ) {
13
+ func AddThemePermission (themeID int , reqStudentIDs []string , themeType string ) ([]string , error ) {
14
+ if themeType == "all" {
15
+ return nil , nil
16
+ }
17
+
12
18
var studentIDs []string
13
19
var invalidStudentIDs []string
14
-
15
20
if len (reqStudentIDs ) > 0 {
16
21
var existingUsers []models.User
17
22
err := database .DB .Select ("student_id" ).Where ("student_id IN ?" , reqStudentIDs ).Find (& existingUsers ).Error
@@ -32,14 +37,10 @@ func AddThemePermission(themeID int, reqStudentIDs []string) ([]string, error) {
32
37
}
33
38
}
34
39
} else {
35
- var users []models.User
36
- err := database .DB .Select ("student_id" ).Find (& users ).Error
37
- if err != nil {
38
- return nil , err
39
- }
40
- for _ , user := range users {
41
- studentIDs = append (studentIDs , user .StudentID )
42
- }
40
+ return nil , errors .New ("reqStudentIDs is invalid" )
41
+ }
42
+ if len (studentIDs ) == 0 {
43
+ return invalidStudentIDs , nil
43
44
}
44
45
45
46
var permissions []models.ThemePermission
@@ -106,6 +107,18 @@ func UpdateCurrentTheme(id int, studentID string) error {
106
107
return err
107
108
}
108
109
110
+ var allThemes []models.Theme
111
+ err = database .DB .Where ("type = ?" , "all" ).Find (& allThemes ).Error
112
+ if err != nil {
113
+ return err
114
+ }
115
+
116
+ for _ , theme := range allThemes {
117
+ if ! containThemeID (themePermissionData .ThemeIDs , theme .ID ) {
118
+ themePermissionData .ThemeIDs = append (themePermissionData .ThemeIDs , theme .ID )
119
+ }
120
+ }
121
+
109
122
if ! containThemeID (themePermissionData .ThemeIDs , id ) {
110
123
return errors .New ("the theme ID is not in the user's permission list" )
111
124
}
@@ -133,11 +146,25 @@ func GetThemeNameByID(themePermission models.ThemePermission) ([]string, error)
133
146
if err != nil {
134
147
return nil , err
135
148
}
149
+
136
150
var themes []models.Theme
137
151
err = database .DB .Where ("id IN ?" , themePermissionData .ThemeIDs ).Find (& themes ).Error
138
152
if err != nil {
139
153
return nil , err
140
154
}
155
+
156
+ var allThemes []models.Theme
157
+ err = database .DB .Where ("type = ?" , "all" ).Find (& allThemes ).Error
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+
162
+ for _ , allTheme := range allThemes {
163
+ if ! containThemeID (themePermissionData .ThemeIDs , allTheme .ID ) {
164
+ themes = append (themes , allTheme )
165
+ }
166
+ }
167
+
141
168
var themeNames []string
142
169
for _ , theme := range themes {
143
170
themeNames = append (themeNames , theme .Name )
@@ -151,11 +178,25 @@ func GetThemesByID(themePermission models.ThemePermission) ([]models.Theme, erro
151
178
if err != nil {
152
179
return nil , err
153
180
}
181
+
154
182
var themes []models.Theme
155
183
err = database .DB .Where ("id IN ?" , themePermissionData .ThemeIDs ).Find (& themes ).Error
156
184
if err != nil {
157
185
return nil , err
158
186
}
187
+
188
+ var allThemes []models.Theme
189
+ err = database .DB .Where ("type = ?" , "all" ).Find (& allThemes ).Error
190
+ if err != nil {
191
+ return nil , err
192
+ }
193
+
194
+ for _ , allTheme := range allThemes {
195
+ if ! containThemeID (themePermissionData .ThemeIDs , allTheme .ID ) {
196
+ themes = append (themes , allTheme )
197
+ }
198
+ }
199
+
159
200
return themes , nil
160
201
}
161
202
@@ -164,29 +205,32 @@ func AddDefaultThemePermission(studentID string) error {
164
205
err := database .DB .Where ("student_id = ?" , studentID ).First (& existingPermission ).Error
165
206
if err != nil {
166
207
if err == gorm .ErrRecordNotFound {
167
- var themes []models.Theme
168
- err := database .DB .Where ("type = ?" , "all" ).Find (& themes ).Error
169
- if err != nil {
170
- return err
171
- }
172
- if len (themes ) == 0 {
173
- return errors .New ("no themes found with type 'all'" )
174
- }
175
-
176
- var themeIDs []int
177
- for _ , theme := range themes {
178
- themeIDs = append (themeIDs , theme .ID )
208
+ themePermissionData := models.ThemePermissionData {
209
+ ThemeIDs : []int {},
179
210
}
180
- var themePermissionData models.ThemePermissionData
181
- themePermissionData .ThemeIDs = themeIDs
182
211
permission , err := json .Marshal (themePermissionData )
183
212
if err != nil {
184
213
return err
185
214
}
186
215
216
+ var defaultThemeID int
217
+ defaultThemeIDStr := config .GetDefaultThemeKey ()
218
+ if defaultThemeIDStr != "" {
219
+ defaultThemeID , err = strconv .Atoi (defaultThemeIDStr )
220
+ if err != nil {
221
+ return err
222
+ }
223
+ } else {
224
+ var theme models.Theme
225
+ if err := database .DB .Model (models.Theme {}).Where ("type = ?" , "all" ).First (& theme ).Error ; err != nil {
226
+ return err
227
+ }
228
+ defaultThemeID = theme .ID
229
+ }
230
+
187
231
newPermission := models.ThemePermission {
188
232
StudentID : studentID ,
189
- CurrentThemeID : themeIDs [ 0 ] ,
233
+ CurrentThemeID : defaultThemeID ,
190
234
ThemePermission : string (permission ),
191
235
}
192
236
0 commit comments