-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathendpoint_state.go
48 lines (43 loc) · 1.15 KB
/
endpoint_state.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
/*
* Let staff update state
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"net/http"
"strconv"
)
func handleState(w http.ResponseWriter, req *http.Request) (string, int, error) {
_, _, department, err := getUserInfoFromRequest(req)
if err != nil {
return "", http.StatusUnauthorized, err
}
if department != staffDepartment {
return "", http.StatusForbidden, errStaffOnly
}
yeargroupParams := req.URL.Query()["yeargroup"]
if len(yeargroupParams) == 0 {
return "", http.StatusBadRequest, errNoSuchYearGroup
}
targetParams := req.URL.Query()["target"]
if len(targetParams) != 1 {
return "", http.StatusBadRequest, errInvalidState
}
newState, err := strconv.ParseUint(targetParams[0], 10, 32)
if err != nil {
return "", http.StatusBadRequest, wrapError(errInvalidState, err)
}
for _, yeargroup := range yeargroupParams {
err = setState(req.Context(), yeargroup, uint32(newState))
if err != nil {
return "", http.StatusBadRequest, wrapError(
errCannotSetState,
err,
)
}
}
http.Redirect(w, req, "/", http.StatusSeeOther)
return "", -1, nil
}