-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
97 lines (70 loc) · 2.15 KB
/
tasks.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
87
88
89
90
91
92
93
94
95
96
97
package v1
import (
"fmt"
"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) AddTaskToCourse(w http.ResponseWriter, r *http.Request, courseID string) {
cmd, ok := unmarshalAddTaskCommand(w, r, courseID)
if !ok {
return
}
taskNumber, err := h.app.Commands.AddTask.Handle(r.Context(), cmd)
if err == nil {
w.Header().Set("Content-Location", fmt.Sprintf("/courses/%s/tasks/%d", courseID, taskNumber))
w.WriteHeader(http.StatusCreated)
return
}
if errors.Is(err, app.ErrCourseDoesntExist) {
httperr.NotFound("course-not-found", err, w, r)
return
}
if course.IsInvalidTaskParametersError(err) {
httperr.UnprocessableEntity("invalid-task-parameters", 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) GetCourseTasks(w http.ResponseWriter, r *http.Request, courseID string, params GetCourseTasksParams) {
qry, ok := unmarshalAllTasksQuery(w, r, courseID, params)
if !ok {
return
}
tasks, err := h.app.Queries.AllTasks.Handle(r.Context(), qry)
if err == nil {
marshalGeneralTasks(w, r, tasks)
return
}
if errors.Is(err, app.ErrCourseDoesntExist) {
httperr.NotFound("course-not-found", err, w, r)
return
}
httperr.InternalServerError("unexpected-error", err, w, r)
}
func (h handler) GetCourseTask(w http.ResponseWriter, r *http.Request, courseID string, taskNumber int) {
qry, ok := unmarshalSpecificTaskQuery(w, r, courseID, taskNumber)
if !ok {
return
}
task, err := h.app.Queries.SpecificTask.Handle(r.Context(), qry)
if err == nil {
marshalSpecificTask(w, r, task)
return
}
if errors.Is(err, app.ErrCourseDoesntExist) {
httperr.NotFound("course-not-found", err, w, r)
return
}
if errors.Is(err, app.ErrTaskDoesntExist) {
httperr.NotFound("course-task-not-found", err, w, r)
return
}
httperr.InternalServerError("unexpected-error", err, w, r)
}