-
Notifications
You must be signed in to change notification settings - Fork 0
/
collaborators.go
86 lines (62 loc) · 1.87 KB
/
collaborators.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
package v1
import (
"net/http"
"github.com/pkg/errors"
"github.com/authena-ru/courses-organization/internal/app"
"github.com/authena-ru/courses-organization/internal/domain/course"
"github.com/authena-ru/courses-organization/pkg/httperr"
)
func (h handler) GetAllCourseCollaborators(w http.ResponseWriter, _ *http.Request, _ string) {
w.WriteHeader(http.StatusNotImplemented)
}
func (h handler) AddCollaboratorToCourse(w http.ResponseWriter, r *http.Request, courseID string) {
cmd, ok := unmarshalAddCollaboratorCommand(w, r, courseID)
if !ok {
return
}
err := h.app.Commands.AddCollaborator.Handle(r.Context(), cmd)
if err == nil {
w.WriteHeader(http.StatusNoContent)
return
}
if errors.Is(err, app.ErrCourseDoesntExist) {
httperr.NotFound("course-not-found", err, w, r)
return
}
if errors.Is(err, app.ErrTeacherDoesntExist) {
httperr.UnprocessableEntity("teacher-not-found", err, w, r)
return
}
if course.IsAcademicCantEditCourseError(err) {
httperr.Forbidden("academic-cant-edit-course", err, w, r)
return
}
httperr.InternalServerError("unexpected-error", err, w, r)
}
func (h handler) RemoveCollaboratorFromCourse(
w http.ResponseWriter, r *http.Request,
courseID, teacherID string,
) {
cmd, ok := unmarshalRemoveCollaboratorCommand(w, r, courseID, teacherID)
if !ok {
return
}
err := h.app.Commands.RemoveCollaborator.Handle(r.Context(), cmd)
if err == nil {
w.WriteHeader(http.StatusNoContent)
return
}
if errors.Is(err, app.ErrCourseDoesntExist) {
httperr.NotFound("course-not-found", err, w, r)
return
}
if errors.Is(err, course.ErrCourseHasNoSuchCollaborator) {
httperr.NotFound("course-collaborator-not-found", err, w, r)
return
}
if course.IsAcademicCantEditCourseError(err) {
httperr.Forbidden("academic-cant-edit-course", err, w, r)
return
}
httperr.InternalServerError("unexpected-error", err, w, r)
}