-
Notifications
You must be signed in to change notification settings - Fork 3
/
course_types_groups.go
169 lines (154 loc) · 3.06 KB
/
course_types_groups.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
/*
* Course types and groups
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"context"
"fmt"
)
/* Course types, e.g. Sport */
const (
sport string = "Sport"
nonSport string = "Non-sport"
)
var courseTypes = map[string]struct{}{
sport: {},
nonSport: {},
}
func checkCourseType(ct string) bool {
_, ok := courseTypes[ct]
return ok
}
type userCourseTypesT map[string]int
func getCourseTypeMinimumForYearGroup(yearGroup, courseType string) (int, error) {
switch yearGroup {
case "Y9":
switch courseType {
case sport:
return config.Req.Y9.Sport, nil
case nonSport:
return config.Req.Y9.NonSport, nil
default:
return 0, errInvalidCourseType
}
case "Y10":
switch courseType {
case sport:
return config.Req.Y10.Sport, nil
case nonSport:
return config.Req.Y10.NonSport, nil
default:
return 0, errInvalidCourseType
}
case "Y11":
switch courseType {
case sport:
return config.Req.Y11.Sport, nil
case nonSport:
return config.Req.Y11.NonSport, nil
default:
return 0, errInvalidCourseType
}
case "Y12":
switch courseType {
case sport:
return config.Req.Y12.Sport, nil
case nonSport:
return config.Req.Y12.NonSport, nil
default:
return 0, errInvalidCourseType
}
default:
return 0, errNoSuchYearGroup
}
}
/* Course groups, e.g. MW1 */
type userCourseGroupsT map[string]struct{}
func checkCourseGroup(cg string) bool {
_, ok := courseGroups[cg]
return ok
}
const (
mw1 string = "MW1"
mw2 string = "MW2"
mw3 string = "MW3"
tt1 string = "TT1"
tt2 string = "TT2"
tt3 string = "TT3"
)
var courseGroups = map[string]string{
mw1: "Monday/Wednesday CCA1",
mw2: "Monday/Wednesday CCA2",
mw3: "Monday/Wednesday CCA3",
tt1: "Tuesday/Thursday CCA1",
tt2: "Tuesday/Thursday CCA2",
tt3: "Tuesday/Thursday CCA3",
}
/* Populate both */
func populateUserCourseTypesAndGroups(
ctx context.Context,
userCourseTypes *userCourseTypesT,
userCourseGroups *userCourseGroupsT,
userID string,
) error {
rows, err := db.Query(
ctx,
"SELECT courseid FROM choices WHERE userid = $1",
userID,
)
if err != nil {
return wrapError(
errUnexpectedDBError,
err,
)
}
for {
if !rows.Next() {
err := rows.Err()
if err != nil {
return wrapError(
errUnexpectedDBError,
err,
)
}
break
}
var thisCourseID int
err := rows.Scan(&thisCourseID)
if err != nil {
return wrapError(
errUnexpectedDBError,
err,
)
}
var thisGroupName, thisTypeName string
_course, ok := courses.Load(thisCourseID)
if !ok {
return fmt.Errorf(
"%w: %d",
errNoSuchCourse,
thisCourseID,
)
}
course, ok := _course.(*courseT)
if !ok {
return errType
}
thisGroupName = course.Group
thisTypeName = course.Type
if _, ok := (*userCourseGroups)[thisGroupName]; ok {
return fmt.Errorf(
"%w: user %v, group %v",
errMultipleChoicesInOneGroup,
userID,
thisGroupName,
)
}
(*userCourseGroups)[thisGroupName] = struct{}{}
(*userCourseTypes)[thisTypeName]++
}
return nil
}