-
Notifications
You must be signed in to change notification settings - Fork 3
/
endpoint_export_choices.go
138 lines (131 loc) · 3.07 KB
/
endpoint_export_choices.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
/*
* Staff page
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"encoding/csv"
"net/http"
"strings"
)
func handleExportChoices(
w http.ResponseWriter,
req *http.Request,
) (string, int, error) {
_, _, department, err := getUserInfoFromRequest(req)
if err != nil {
return "", -1, err
}
if department != staffDepartment {
return "", http.StatusForbidden, errStaffOnly
}
type userCacheT struct {
Name string
StudentID string
Department string
}
userCacheMap := make(map[string]userCacheT)
rows, err := db.Query(req.Context(), "SELECT userid, courseid FROM choices")
if err != nil {
return "", -1, wrapError(errUnexpectedDBError, err)
}
output := make([][]string, 0)
for {
if !rows.Next() {
err := rows.Err()
if err != nil {
return "", -1, wrapError(errUnexpectedDBError, err)
}
break
}
var currentUserID,
currentUserName,
currentStudentID,
currentDepartment string
var currentCourseID int
err := rows.Scan(¤tUserID, ¤tCourseID)
if err != nil {
return "", -1, wrapError(errUnexpectedDBError, err)
}
currentUserCache, ok := userCacheMap[currentUserID]
if ok {
currentUserName = currentUserCache.Name
currentDepartment = currentUserCache.Department
currentStudentID = currentUserCache.StudentID
} else {
var currentUserEmail string
err := db.QueryRow(
req.Context(),
"SELECT name, email, department FROM users WHERE id = $1",
currentUserID,
).Scan(
¤tUserName,
¤tUserEmail,
¤tDepartment,
)
if err != nil {
return "", -1, wrapError(errUnexpectedDBError, err)
}
before, _, found := strings.Cut(currentUserEmail, "@")
if found {
currentStudentID, _ = strings.CutPrefix(before, "s")
} else {
currentStudentID = currentUserEmail
}
userCacheMap[currentUserID] = userCacheT{
Name: currentUserName,
StudentID: currentStudentID,
Department: currentDepartment,
}
}
_course, ok := courses.Load(currentCourseID)
if !ok {
return "", -1, wrapAny(errNoSuchCourse, currentCourseID)
}
course, ok := _course.(*courseT)
if !ok {
return "", -1, errType
}
if course == nil {
return "", -1, wrapAny(errNoSuchCourse, currentCourseID)
}
output = append(
output,
[]string{
currentUserName,
currentStudentID,
currentDepartment,
course.Title,
course.Group,
course.SectionID,
course.CourseID,
},
)
}
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", "attachment;filename=cca_choices.csv")
csvWriter := csv.NewWriter(w)
err = csvWriter.Write([]string{
"Student Name",
"Student ID",
"Grade/Year",
"Group/Activity",
"Container",
"Section ID",
"Course ID",
})
if err != nil {
return "", -1, wrapError(errHTTPWrite, err)
}
err = csvWriter.WriteAll(output)
if err != nil {
return "", -1, wrapError(errHTTPWrite, err)
}
csvWriter.Flush()
if csvWriter.Error() != nil {
return "", -1, wrapError(errHTTPWrite, err)
}
return "", -1, nil
}