-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
158 lines (150 loc) · 4.66 KB
/
handlers.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// createTaskHandler will attempt to create and distribute a task to an agent.
func createTaskHandler(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodPost:
taskPayload, err := createPayload(request.Body)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to decode payload %s", err.Error()), http.StatusInternalServerError)
return
}
err = taskPayload.requiredFields()
if err != nil {
formatError(writer, fmt.Sprintf("Required field missing %s", err.Error()), http.StatusBadRequest)
return
}
err = taskPayload.validateSkills(destributerDb)
if err != nil {
formatError(writer, fmt.Sprintf("Invalid skill %s", err.Error()), http.StatusBadRequest)
return
}
err = taskPayload.validatePriority(destributerDb)
if err != nil {
formatError(writer, fmt.Sprintf("Invalid priority %s", err.Error()), http.StatusBadRequest)
return
}
t := &task{
db: destributerDb,
}
err = t.assignTask(*taskPayload)
if err != nil {
formatError(writer, fmt.Sprintf("%s", err.Error()), http.StatusInsufficientStorage)
return
}
success := struct {
Success bool `json:"success"`
Task task `json:"task"`
}{
Success: true,
Task: *t,
}
resp, err := json.Marshal(success)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to encode response %s", err.Error()), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(resp)
default:
http.Error(writer, fmt.Sprintf("Method is not supported %s", request.Method), http.StatusMethodNotAllowed)
}
}
// statusTaskHandler will return the current status of the task.
func statusTaskHandler(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodGet:
routes := strings.Split(request.URL.String(), "/")
taskID := routes[len(routes)-1]
if taskID == "" {
formatError(writer, "Task Id must be included in the URL", http.StatusBadRequest)
return
}
t := &task{
db: destributerDb,
}
err := t.retrieve(taskID)
if err != nil {
formatError(writer, fmt.Sprintf("Task %s is not present", taskID), http.StatusBadRequest)
return
}
success := struct {
Success bool `json:"success"`
Task task `json:"task"`
}{
Success: true,
Task: *t,
}
resp, err := json.Marshal(success)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to encode response %s", err.Error()), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(resp)
default:
http.Error(writer, fmt.Sprintf("Method is not supported %s", request.Method), http.StatusMethodNotAllowed)
}
}
// completeTaskHandler sets the task as completed.
func completeTaskHandler(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodGet:
routes := strings.Split(request.URL.String(), "/")
taskID := routes[len(routes)-1]
if taskID == "" {
formatError(writer, "Task Id must be included in the URL", http.StatusBadRequest)
return
}
err := updateTaskStatus(destributerDb, taskID, "Complete")
if err != nil {
formatError(writer, fmt.Sprintf("Task %s is not present", taskID), http.StatusBadRequest)
return
}
success := struct {
Success bool `json:"success"`
}{
Success: true,
}
resp, err := json.Marshal(success)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to encode response %s", err.Error()), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(resp)
default:
http.Error(writer, fmt.Sprintf("Method is not supported %s", request.Method), http.StatusMethodNotAllowed)
}
}
// listAgentHandler will list the agents and what they are currently working on
func listAgentHandler(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodGet:
ats, err := retrieveAgentTasks(destributerDb)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to encode response %s", err.Error()), http.StatusInternalServerError)
}
success := struct {
Success bool `json:"success"`
AgentTasks []agentTasks `json:"agent_tasks"`
}{
Success: true,
AgentTasks: ats,
}
resp, err := json.Marshal(success)
if err != nil {
formatError(writer, fmt.Sprintf("Unable to encode response %s", err.Error()), http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(resp)
default:
http.Error(writer, fmt.Sprintf("Method is not supported %s", request.Method), http.StatusMethodNotAllowed)
}
}