-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcourse_archived.go
More file actions
62 lines (55 loc) · 1.92 KB
/
course_archived.go
File metadata and controls
62 lines (55 loc) · 1.92 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
package httpserver
import (
"encoding/json"
"net/http"
"github.com/lextures/lextures/server/internal/apierr"
"github.com/lextures/lextures/server/internal/repos/course"
"github.com/lextures/lextures/server/internal/courseroles"
)
type patchCourseArchivedBody struct {
Archived bool `json:"archived"`
}
// handlePatchCourseArchived is PATCH /api/v1/courses/{course_code}/archived.
func (d Deps) handlePatchCourseArchived() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if r.Method != http.MethodPatch {
w.Header().Set("Allow", http.MethodPatch+","+http.MethodOptions)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
courseCode, viewer, ok := d.requireCourseAccess(w, r)
if !ok {
return
}
canEdit, err := courseroles.UserHasPermission(r.Context(), d.Pool, viewer, "course:"+courseCode+":item:create")
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to verify permissions.")
return
}
if !canEdit {
apierr.WriteJSON(w, http.StatusForbidden, apierr.CodeForbidden, "You do not have permission for this action.")
return
}
var req patchCourseArchivedBody
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
apierr.WriteJSON(w, http.StatusBadRequest, apierr.CodeInvalidInput, "Invalid JSON body.")
return
}
out, err := course.SetArchived(r.Context(), d.Pool, courseCode, req.Archived)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to update course.")
return
}
if out == nil {
apierr.WriteJSON(w, http.StatusNotFound, apierr.CodeNotFound, "Course not found.")
return
}
d.notifyCourses(viewer)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(out)
}
}