-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathep_stu_api_myselections.go
More file actions
74 lines (70 loc) · 2.37 KB
/
ep_stu_api_myselections.go
File metadata and controls
74 lines (70 loc) · 2.37 KB
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
package main
import (
"encoding/json"
"log/slog"
"net/http"
"git.sr.ht/~runxiyu/cca/db"
)
func (app *App) handleStuAPIMySelections(w http.ResponseWriter, r *http.Request, sui *UserInfoStudent) {
app.logRequestStart(r, "handleStuAPIMySelections", slog.Int64("student_id", sui.ID))
get := func() bool {
selections, err := app.queries.GetSelectionsByStudent(r.Context(), sui.ID)
if err != nil {
app.apiError(r, w, http.StatusInternalServerError, err.Error(), slog.Int64("student_id", sui.ID))
return true
}
app.writeJSON(r, w, http.StatusOK, selections, slog.Int64("student_id", sui.ID))
return false
}
switch r.Method {
case http.MethodGet:
if get() {
return
}
case http.MethodDelete:
var s string
err := json.NewDecoder(r.Body).Decode(&s)
if err != nil {
app.apiError(r, w, http.StatusBadRequest, err, slog.String("operation", "delete_selection"), slog.Int64("student_id", sui.ID))
return
}
err = app.queries.DeleteChoiceByStudentAndCourse(r.Context(),
db.DeleteChoiceByStudentAndCourseParams{
PStudentID: sui.ID,
PCourseID: s,
},
)
if err != nil {
app.apiError(r, w, http.StatusInternalServerError, err.Error(), slog.String("operation", "delete_selection"), slog.Int64("student_id", sui.ID), slog.String("course_id", s))
return
}
app.logInfo(r, logMsgStudentSelectionsDelete, slog.Int64("student_id", sui.ID), slog.String("operation", "delete_selection"), slog.String("course_id", s))
app.broadcastCourseCounts(r, []string{s})
if get() {
return
}
case http.MethodPut:
var s string
err := json.NewDecoder(r.Body).Decode(&s)
if err != nil {
app.apiError(r, w, http.StatusBadRequest, err, slog.String("operation", "new_selection"), slog.Int64("student_id", sui.ID))
return
}
err = app.queries.NewSelection(r.Context(), db.NewSelectionParams{
PStudentID: sui.ID,
PCourseID: s,
PSelectionType: "normal",
})
if err != nil {
app.apiError(r, w, http.StatusInternalServerError, err.Error(), slog.String("operation", "new_selection"), slog.Int64("student_id", sui.ID), slog.String("course_id", s))
return
}
app.logInfo(r, logMsgStudentSelectionsCreate, slog.Int64("student_id", sui.ID), slog.String("operation", "new_selection"), slog.String("course_id", s))
app.broadcastCourseCounts(r, []string{s})
if get() {
return
}
default:
app.apiError(r, w, http.StatusMethodNotAllowed, nil)
}
}