-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.go
265 lines (229 loc) · 7.11 KB
/
queries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package editor
import (
"database/sql"
"sort"
"github.com/benoitkugler/maths-online/server/src/sql/teacher"
"github.com/benoitkugler/maths-online/server/src/utils"
)
type OptionalIdExercice struct {
Valid bool
ID IdExercice
}
func (id IdExercice) AsOptional() OptionalIdExercice {
return OptionalIdExercice{ID: id, Valid: true}
}
type OptionalIdQuestiongroup struct {
ID IdQuestiongroup
Valid bool
}
func (id IdQuestiongroup) AsOptional() OptionalIdQuestiongroup {
return OptionalIdQuestiongroup{ID: id, Valid: true}
}
// selectQuestiongroupsByTag returns the question groups matching the given tag.
// It is meant to avoid loading the whole tags table.
func selectQuestiongroupsByTag(db DB, tag TagSection) (Questiongroups, error) {
rs, err := db.Query(`SELECT questiongroups.*
FROM questiongroups
JOIN questiongroup_tags ON questiongroups.id = questiongroup_tags.IdQuestiongroup
WHERE questiongroup_tags.tag = $1 AND questiongroup_tags.section = $2`, tag.Tag, tag.Section)
if err != nil {
return nil, err
}
return ScanQuestiongroups(rs)
}
// selectExercicegroupsByTag returns the exercice groups matching the given tag.
// It is meant to avoid loading the whole tags table.
func selectExercicegroupsByTag(db DB, tag TagSection) (Exercicegroups, error) {
rs, err := db.Query(`SELECT exercicegroups.*
FROM exercicegroups
JOIN exercicegroup_tags ON exercicegroups.id = exercicegroup_tags.IdExercicegroup
WHERE exercicegroup_tags.tag = $1 AND exercicegroup_tags.section = $2`, tag.Tag, tag.Section)
if err != nil {
return nil, err
}
return ScanExercicegroups(rs)
}
// IsVisibleBy returns `true` if the question is public or
// owned by `userID`
func (qu Questiongroup) IsVisibleBy(userID teacher.IdTeacher) bool {
return qu.Public || qu.IdTeacher == userID
}
// RestrictVisible remove the questions not visible by `userID`
func (qus Questiongroups) RestrictVisible(userID teacher.IdTeacher) {
for id, qu := range qus {
if !qu.IsVisibleBy(userID) {
delete(qus, id)
}
}
}
// RestrictNeedExercice remove the questions marked as requiring an
// exercice
func (qus Questions) RestrictNeedExercice() {
for id, question := range qus {
if question.NeedExercice.Valid {
delete(qus, id)
}
}
}
func (qus Questions) ByGroup() map[IdQuestiongroup][]Question {
out := make(map[IdQuestiongroup][]Question)
for _, question := range qus {
idGroup := question.IdGroup.ID
out[idGroup] = append(out[idGroup], question)
}
return out
}
// UpdateQuestiongroupTags sets the tags of [id], normalizing and validating [tags]
// It does NOT commit or rollback.
func UpdateQuestiongroupTags(tx *sql.Tx, tags Tags, id IdQuestiongroup) error {
tags, err := tags.normalize()
if err != nil {
return err
}
_, err = DeleteQuestiongroupTagsByIdQuestiongroups(tx, id)
if err != nil {
return utils.SQLError(err)
}
err = InsertManyQuestiongroupTags(tx, tags.asQuestionLinks(id)...)
if err != nil {
return utils.SQLError(err)
}
return nil
}
// UpdateExercicegroupTags enforces proper IdExercicegroup, mutating `tags`.
// It does NOT commit or rollback.
func UpdateExercicegroupTags(tx *sql.Tx, tags Tags, id IdExercicegroup) error {
tags, err := tags.normalize()
if err != nil {
return err
}
_, err = DeleteExercicegroupTagsByIdExercicegroups(tx, id)
if err != nil {
return utils.SQLError(err)
}
err = InsertManyExercicegroupTags(tx, tags.asExerciceLinks(id)...)
if err != nil {
return utils.SQLError(err)
}
return nil
}
// UpdateExerciceQuestionList set the questions for the given exercice,
// overiding `IdExercice` and `index` fields of the list items.
// Do NOT commit or rollback
func UpdateExerciceQuestionList(tx *sql.Tx, idExercice IdExercice, l ExerciceQuestions) error {
// enforce fields
for i := range l {
l[i].Index = i
l[i].IdExercice = idExercice
}
_, err := DeleteExerciceQuestionsByIdExercices(tx, idExercice)
if err != nil {
return utils.SQLError(err)
}
err = InsertManyExerciceQuestions(tx, l...)
if err != nil {
return utils.SQLError(err)
}
return nil
}
// IsVisibleBy returns `true` if the exercice group is public or
// owned by `userID`
func (qu Exercicegroup) IsVisibleBy(userID teacher.IdTeacher) bool {
return qu.Public || qu.IdTeacher == userID
}
// RestrictVisible remove the questions not visible by `userID`
func (qus Exercicegroups) RestrictVisible(userID teacher.IdTeacher) {
for id, ex := range qus {
if !ex.IsVisibleBy(userID) {
delete(qus, id)
}
}
}
func (qus Exercices) ByGroup() map[IdExercicegroup][]Exercice {
out := make(map[IdExercicegroup][]Exercice)
for _, exercice := range qus {
idGroup := exercice.IdGroup
out[idGroup] = append(out[idGroup], exercice)
}
return out
}
// SelectQuestiongroupByTags returns the question groups matching the given query,
// and available to `userID`, returning their tags.
// It panics if `pattern` is empty.
func SelectQuestiongroupByTags(db DB, userID teacher.IdTeacher, pattern Tags) (map[IdQuestiongroup]QuestiongroupTags, error) {
firstSelection, err := selectQuestiongroupsByTag(db, pattern[0])
if err != nil {
return nil, err
}
quTags, err := SelectQuestiongroupTagsByIdQuestiongroups(db, firstSelection.IDs()...)
if err != nil {
return nil, err
}
var (
selectedIDs []IdQuestiongroup
tagsByGroup = quTags.ByIdQuestiongroup()
)
// remove questions not matching all the tags
for idGroup, cr := range tagsByGroup {
hasAll := cr.Tags().Crible().HasAll(pattern)
if !hasAll {
delete(tagsByGroup, idGroup)
} else {
selectedIDs = append(selectedIDs, idGroup)
}
}
groups, err := SelectQuestiongroups(db, selectedIDs...)
if err != nil {
return nil, utils.SQLError(err)
}
// restrict to user visible groups
for _, group := range groups {
if !group.IsVisibleBy(userID) {
delete(tagsByGroup, group.Id)
}
}
return tagsByGroup, nil
}
// SelectExercicegroupByTags returns the question groups matching the given query,
// and available to `userID`, returning their tags.
// It panics if `pattern` is empty.
func SelectExercicegroupByTags(db DB, userID teacher.IdTeacher, pattern Tags) (map[IdExercicegroup]ExercicegroupTags, error) {
firstSelection, err := selectExercicegroupsByTag(db, pattern[0])
if err != nil {
return nil, err
}
quTags, err := SelectExercicegroupTagsByIdExercicegroups(db, firstSelection.IDs()...)
if err != nil {
return nil, err
}
var (
selectedIDs []IdExercicegroup
tagsByGroup = quTags.ByIdExercicegroup()
)
// remove exercices not matching all the tags
for idGroup, cr := range tagsByGroup {
hasAll := cr.Tags().Crible().HasAll(pattern)
if !hasAll {
delete(tagsByGroup, idGroup)
} else {
selectedIDs = append(selectedIDs, idGroup)
}
}
groups, err := SelectExercicegroups(db, selectedIDs...)
if err != nil {
return nil, utils.SQLError(err)
}
// restrict to user visible groups
for _, group := range groups {
if !group.IsVisibleBy(userID) {
delete(tagsByGroup, group.Id)
}
}
return tagsByGroup, nil
}
// EnsureOrder must be call on the questions of one exercice,
// to make sure the order in the slice is consistent with the one
// indicated by `Index`
func (l ExerciceQuestions) EnsureOrder() {
sort.Slice(l, func(i, j int) bool { return l[i].Index < l[j].Index })
}