-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixtures.go
42 lines (33 loc) · 947 Bytes
/
fixtures.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
package httphandler
import (
"net/http"
"strings"
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/httpjson"
)
func (s Service) fixturesHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := strings.Trim(r.URL.Path, "/")
if len(query) == 0 {
httperror.NotFound(r.Context(), w)
return
}
urlParts := strings.Split(query, "/")
if len(urlParts) == 1 {
fixturesList, err := s.mailerService.ListFixtures(urlParts[0])
if httperror.HandleError(r.Context(), w, err) {
return
}
httpjson.WriteArray(r.Context(), w, http.StatusOK, fixturesList)
return
}
if len(urlParts) == 2 {
content, err := s.mailerService.GetFixture(urlParts[0], urlParts[1])
if httperror.HandleError(r.Context(), w, err) {
return
}
httpjson.Write(r.Context(), w, http.StatusOK, content)
}
httperror.NotFound(r.Context(), w)
})
}