diff options
Diffstat (limited to '')
-rw-r--r-- | course_groups.go | 22 | ||||
-rw-r--r-- | course_types.go | 10 | ||||
-rw-r--r-- | courses.go | 4 | ||||
-rw-r--r-- | endpoint_export.go | 2 | ||||
-rw-r--r-- | endpoint_index.go | 8 | ||||
-rw-r--r-- | endpoint_newcourses.go | 11 | ||||
-rw-r--r-- | misc_utils.go | 8 | ||||
-rw-r--r-- | ws_connection.go | 2 |
8 files changed, 37 insertions, 30 deletions
diff --git a/course_groups.go b/course_groups.go index f81fcfa..598fb02 100644 --- a/course_groups.go +++ b/course_groups.go @@ -25,25 +25,23 @@ import ( "fmt" ) -type userCourseGroupsT map[courseGroupT]struct{} +type userCourseGroupsT map[string]struct{} -type courseGroupT string - -func checkCourseGroup(cg courseGroupT) bool { +func checkCourseGroup(cg string) bool { _, ok := courseGroups[cg] return ok } const ( - mw1 courseGroupT = "MW1" - mw2 courseGroupT = "MW2" - mw3 courseGroupT = "MW3" - tt1 courseGroupT = "TT1" - tt2 courseGroupT = "TT2" - tt3 courseGroupT = "TT3" + mw1 string = "MW1" + mw2 string = "MW2" + mw3 string = "MW3" + tt1 string = "TT1" + tt2 string = "TT2" + tt3 string = "TT3" ) -var courseGroups = map[courseGroupT]string{ +var courseGroups = map[string]string{ mw1: "Monday/Wednesday CCA1", mw2: "Monday/Wednesday CCA2", mw3: "Monday/Wednesday CCA3", @@ -87,7 +85,7 @@ func populateUserCourseGroups( err, ) } - var thisGroupName courseGroupT + var thisGroupName string _course, ok := courses.Load(thisCourseID) if !ok { return fmt.Errorf( diff --git a/course_types.go b/course_types.go index f82678c..2087735 100644 --- a/course_types.go +++ b/course_types.go @@ -20,19 +20,17 @@ package main -type courseTypeT string - const ( - sport courseTypeT = "Sport" - nonSport courseTypeT = "Non-sport" + sport string = "Sport" + nonSport string = "Non-sport" ) -var courseTypes = map[courseTypeT]struct{}{ +var courseTypes = map[string]struct{}{ sport: {}, nonSport: {}, } -func checkCourseType(ct courseTypeT) bool { +func checkCourseType(ct string) bool { _, ok := courseTypes[ct] return ok } @@ -43,8 +43,8 @@ type courseT struct { ID int Max uint32 Title string - Type courseTypeT - Group courseGroupT + Type string + Group string Teacher string Location string Usems sync.Map /* string, *usemT */ diff --git a/endpoint_export.go b/endpoint_export.go index 5118181..3f55a00 100644 --- a/endpoint_export.go +++ b/endpoint_export.go @@ -151,7 +151,7 @@ func handleExport(w http.ResponseWriter, req *http.Request) { currentStudentID, currentDepartment, course.Title, - string(course.Group), + course.Group, }, ) } diff --git a/endpoint_index.go b/endpoint_index.go index 57ab313..a6aa441 100644 --- a/endpoint_index.go +++ b/endpoint_index.go @@ -65,11 +65,11 @@ func handleIndex(w http.ResponseWriter, req *http.Request) { /* TODO: The below should be completed on-update. */ type groupT struct { - Handle courseGroupT + Handle string Name string Courses *map[int]*courseT } - _groups := make(map[courseGroupT]groupT) + _groups := make(map[string]groupT) for k, v := range courseGroups { _coursemap := make(map[int]*courseT) _groups[k] = groupT{ @@ -98,7 +98,7 @@ func handleIndex(w http.ResponseWriter, req *http.Request) { struct { Name string State uint32 - Groups *map[courseGroupT]groupT + Groups *map[string]groupT }{ username, state, @@ -135,7 +135,7 @@ func handleIndex(w http.ResponseWriter, req *http.Request) { struct { Name string Department string - Groups *map[courseGroupT]groupT + Groups *map[string]groupT }{ username, department, diff --git a/endpoint_newcourses.go b/endpoint_newcourses.go index bf8f570..d00c3e9 100644 --- a/endpoint_newcourses.go +++ b/endpoint_newcourses.go @@ -27,6 +27,7 @@ import ( "fmt" "io" "net/http" + "strings" "sync/atomic" "github.com/jackc/pgx/v5" @@ -243,26 +244,28 @@ func handleNewCourses(w http.ResponseWriter, req *http.Request) { ) return false } - if !checkCourseType(courseTypeT(line[typeIndex])) { + if !checkCourseType(line[typeIndex]) { wstr( w, http.StatusBadRequest, fmt.Sprintf( - "Line %d has invalid course type \"%s\"", + "Line %d has invalid course type \"%s\"\nAllowed course types: %s", lineNumber, line[typeIndex], + strings.Join(getKeysOfMap(courseTypes), ", "), ), ) return false } - if !checkCourseGroup(courseGroupT(line[groupIndex])) { + if !checkCourseGroup(line[groupIndex]) { wstr( w, http.StatusBadRequest, fmt.Sprintf( - "Line %d has invalid course group \"%s\"", + "Line %d has invalid course group \"%s\"\nAllowed course groups: %s", lineNumber, line[groupIndex], + strings.Join(getKeysOfMap(courseGroups), ", "), ), ) return false diff --git a/misc_utils.go b/misc_utils.go index d541118..94b8522 100644 --- a/misc_utils.go +++ b/misc_utils.go @@ -51,3 +51,11 @@ func randomString(sz int) (string, error) { } return base64.RawURLEncoding.EncodeToString(r), nil } + +func getKeysOfMap[K comparable, V any](i map[K]V) []K { + o := make([]K, 0, len(i)) + for k := range i { + o = append(o, k) + } + return o +} diff --git a/ws_connection.go b/ws_connection.go index 48c2b3a..e3cc6d2 100644 --- a/ws_connection.go +++ b/ws_connection.go @@ -146,7 +146,7 @@ func handleConn( * userCourseGroups stores whether the user has already chosen a course * in the courseGroup. */ - var userCourseGroups userCourseGroupsT = make(map[courseGroupT]struct{}) + var userCourseGroups userCourseGroupsT = make(map[string]struct{}) err := populateUserCourseGroups(newCtx, &userCourseGroups, userID) if err != nil { return reportError( |