forked from benthosdev/benthos-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_handler.go
59 lines (51 loc) · 1.51 KB
/
list_handler.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
package service
import (
"context"
"encoding/json"
"github.com/nats-io/nats.go/micro"
"github.com/rs/zerolog/log"
"github.com/wombatwisdom/wombat-builder/internal/store"
"github.com/wombatwisdom/wombat-builder/public/model"
)
type (
BuildListRequest struct {
Query string `json:"query" jsonschema_description:"The query to search for"`
}
BuildListResponse struct {
Builds []model.Build `json:"builds" jsonschema_description:"The list of builds"`
}
)
func (r *BuildListRequest) Validate() error {
return nil
}
func getBuildListHandler(s *store.Store) micro.HandlerFunc {
return func(request micro.Request) {
var req BuildListRequest
if err := json.Unmarshal(request.Data(), &req); err != nil {
_ = request.Error("BAD_REQUEST", "failed to parse request", []byte(err.Error()))
return
}
if err := req.Validate(); err != nil {
_ = request.Error("BAD_REQUEST", "invalid request", []byte(err.Error()))
return
}
ids, err := s.BuildsIndex.Search(req.Query)
if err != nil {
_ = request.Error("BACKBONE_ERROR", "failed to search for builds", []byte(err.Error()))
return
}
var result []model.Build
for _, id := range ids {
build, err := s.Builds.Get(context.Background(), id)
if err != nil {
log.Warn().Err(err).Msgf("failed to get build %s", id)
continue
}
result = append(result, *build)
}
if err := request.RespondJSON(BuildListResponse{Builds: result}); err != nil {
_ = request.Error("INTERNAL_ERROR", "failed to respond", []byte(err.Error()))
return
}
}
}