-
Notifications
You must be signed in to change notification settings - Fork 143
/
mock_complaints.go
179 lines (154 loc) · 4.28 KB
/
mock_complaints.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package mailgun
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/go-chi/chi/v5"
)
func (ms *mockServer) addComplaintsRoutes(r chi.Router) {
r.Get("/{domain}/complaints", ms.listComplaints)
r.Get("/{domain}/complaints/{address}", ms.getComplaint)
r.Delete("/{domain}/complaints/{address}", ms.deleteComplaint)
r.Post("/{domain}/complaints", ms.createComplaint)
ms.complaints = append(ms.complaints, Complaint{
CreatedAt: RFC2822Time(time.Now()),
Address: "[email protected]",
})
ms.complaints = append(ms.complaints, Complaint{
CreatedAt: RFC2822Time(time.Now()),
Address: "[email protected]",
})
}
func (ms *mockServer) listComplaints(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var idx []string
for _, t := range ms.complaints {
idx = append(idx, t.Address)
}
limit := stringToInt(r.FormValue("limit"))
if limit == 0 {
limit = 100
}
page := r.FormValue("page")
var pivot string
if len(page) != 0 {
pivot = r.FormValue("p")
if pivot == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"message\": \"Invalid parameter: pivot \"}"))
return
}
}
start, end := pageOffsets(idx, page, pivot, limit)
var nextAddress, prevAddress string
var results []Complaint
if start != end {
results = ms.complaints[start:end]
nextAddress = results[len(results)-1].Address
prevAddress = results[0].Address
} else {
results = []Complaint{}
nextAddress = pivot
prevAddress = pivot
}
toJSON(w, complaintsResponse{
Paging: Paging{
First: getPageURL(r, url.Values{
"page": []string{"first"},
}),
Last: getPageURL(r, url.Values{
"page": []string{"last"},
}),
Next: getPageURL(r, url.Values{
"page": []string{"next"},
"p": []string{nextAddress},
}),
Previous: getPageURL(r, url.Values{
"page": []string{"prev"},
"p": []string{prevAddress},
}),
},
Items: results,
})
}
func (ms *mockServer) getComplaint(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for _, complaint := range ms.complaints {
if complaint.Address == chi.URLParam(r, "address") {
toJSON(w, complaint)
return
}
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{\"message\": \"Address not found in complaints table\"}"))
}
func (ms *mockServer) createComplaint(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var complaints []Complaint
if r.Header.Get("Content-Type") == "application/json" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("{\"message\": \"Can't read request body\"}"))
return
}
err = json.Unmarshal(body, &complaints)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("{\"message\": \"Invalid json: %s\"}", err.Error())))
return
}
} else {
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
address := r.FormValue("address")
if len(address) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"message\": \"Invalid format for parameter address: \"}"))
return
}
complaints = append(complaints, Complaint{Address: address, CreatedAt: RFC2822Time(time.Now())})
}
for _, complaint := range complaints {
var addressExist bool
for _, existingComplaint := range ms.complaints {
if existingComplaint.Address == complaint.Address {
addressExist = true
}
}
if !addressExist {
complaint.CreatedAt = RFC2822Time(time.Now())
ms.complaints = append(ms.complaints, complaint)
}
}
toJSON(w, map[string]interface{}{
"message": "Address has been added to the complaints table",
"address": fmt.Sprint(complaints),
})
}
func (ms *mockServer) deleteComplaint(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for i, complaint := range ms.complaints {
if complaint.Address == chi.URLParam(r, "address") {
ms.complaints = append(ms.complaints[:i], ms.complaints[i+1:len(ms.complaints)]...)
toJSON(w, map[string]interface{}{
"message": "Complaint has been removed",
})
return
}
}
toJSON(w, map[string]interface{}{
"message": "Address not found in complaints table",
})
return
}