-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcourse_get.go
More file actions
111 lines (107 loc) · 3.91 KB
/
course_get.go
File metadata and controls
111 lines (107 loc) · 3.91 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
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
package httpserver
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/lextures/lextures/server/internal/apierr"
"github.com/lextures/lextures/server/internal/repos/course"
"github.com/lextures/lextures/server/internal/repos/coursesections"
"github.com/lextures/lextures/server/internal/repos/enrollment"
)
// courseGetResponse matches Rust `CourseWithViewerResponse` (flattened course + viewer fields).
type courseGetResponse struct {
course.CoursePublic
ViewerEnrollmentRoles []string `json:"viewerEnrollmentRoles"`
ViewerStudentEnrollmentID *string `json:"viewerStudentEnrollmentId,omitempty"`
ViewerSectionCode *string `json:"viewerSectionCode,omitempty"`
ViewerSectionName *string `json:"viewerSectionName,omitempty"`
AnnotationsEnabled bool `json:"annotationsEnabled"`
FeedbackMediaEnabled bool `json:"feedbackMediaEnabled"`
ResubmissionWorkflowEnabled bool `json:"resubmissionWorkflowEnabled"`
}
func (d Deps) handleGetCourse() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
courseCode := chi.URLParam(r, "course_code")
if courseCode == "" {
apierr.WriteJSON(w, http.StatusBadRequest, apierr.CodeInvalidInput, "Missing course code.")
return
}
userID, ok := d.meUserID(w, r)
if !ok {
return
}
hasAccess, err := enrollment.UserHasAccess(r.Context(), d.Pool, courseCode, userID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to verify course access.")
return
}
if !hasAccess {
apierr.WriteJSON(w, http.StatusNotFound, apierr.CodeNotFound, "Course not found.")
return
}
crow, err := course.GetPublicByCourseCode(r.Context(), d.Pool, courseCode)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load course.")
return
}
if crow == nil {
apierr.WriteJSON(w, http.StatusNotFound, apierr.CodeNotFound, "Course not found.")
return
}
cid, err := uuid.Parse(crow.ID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Invalid course id.")
return
}
roles, err := enrollment.UserRolesInCourse(r.Context(), d.Pool, courseCode, userID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load enrollment roles.")
return
}
if roles == nil {
roles = []string{}
}
stuEid, err := enrollment.GetStudentEnrollmentID(r.Context(), d.Pool, cid, userID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load enrollment.")
return
}
var stuStr *string
if stuEid != nil {
s := stuEid.String()
stuStr = &s
}
var viewerSecCode, viewerSecName *string
if crow.SectionsEnabled {
secID, err := enrollment.GetStudentSectionID(r.Context(), d.Pool, cid, userID)
if err == nil && secID != nil {
sec, err := coursesections.GetByID(r.Context(), d.Pool, cid, *secID)
if err == nil && sec != nil {
c := sec.SectionCode
viewerSecCode = &c
if sec.Name != nil && *sec.Name != "" {
viewerSecName = sec.Name
}
}
}
}
resp := courseGetResponse{
CoursePublic: *crow,
ViewerEnrollmentRoles: roles,
ViewerStudentEnrollmentID: stuStr,
ViewerSectionCode: viewerSecCode,
ViewerSectionName: viewerSecName,
AnnotationsEnabled: d.effectiveConfig().AnnotationEnabled,
FeedbackMediaEnabled: d.effectiveConfig().FeedbackMediaEnabled,
ResubmissionWorkflowEnabled: d.effectiveConfig().ResubmissionWorkflowEnabled,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(resp)
}
}