-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_blocks.go
72 lines (63 loc) · 1.91 KB
/
map_blocks.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
package mapblocks
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/schema"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/matthewdale/manualsmap.com/encoders"
"github.com/matthewdale/manualsmap.com/services"
)
type getMapBlocksRequest struct {
MinLatitude decimal.Decimal `schema:"min_latitude"`
MinLongitude decimal.Decimal `schema:"min_longitude"`
MaxLatitude decimal.Decimal `schema:"max_latitude"`
MaxLongitude decimal.Decimal `schema:"max_longitude"`
}
type mapBlock struct {
ID int `json:"id"`
Latitude decimal.Decimal `json:"latitude"`
Longitude decimal.Decimal `json:"longitude"`
}
type getMapBlocksResponse struct {
MapBlocks []mapBlock `json:"mapBlocks"`
}
func getEndpoint(persistence services.Persistence) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
r := request.(getMapBlocksRequest)
mapBlocks, err := persistence.GetMapBlocks(
r.MinLatitude,
r.MinLongitude,
r.MaxLatitude,
r.MaxLongitude)
if err != nil {
return nil, encoders.NewJSONError(
errors.WithMessage(err, "error getting map block"),
http.StatusInternalServerError)
}
responseBlocks := make([]mapBlock, 0, len(mapBlocks))
for _, block := range mapBlocks {
responseBlocks = append(responseBlocks, mapBlock{
ID: block.ID,
Latitude: block.Latitude,
Longitude: block.Longitude,
})
}
return getMapBlocksResponse{MapBlocks: responseBlocks}, nil
}
}
func getDecode(_ context.Context, r *http.Request) (interface{}, error) {
var req getMapBlocksRequest
decoder := schema.NewDecoder()
decoder.Decode(&req, r.URL.Query())
return req, nil
}
func GetHandler(persistence services.Persistence) http.Handler {
return httptransport.NewServer(
getEndpoint(persistence),
getDecode,
encoders.JSONResponseEncoder,
)
}