-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadconnectors.go
52 lines (41 loc) · 1.18 KB
/
readconnectors.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
package api
import (
"context"
"encoding/json"
"net/http"
"github.com/formancehq/payments/internal/app/models"
"github.com/formancehq/go-libs/sharedapi"
)
type readConnectorsRepository interface {
ListConnectors(ctx context.Context) ([]*models.Connector, error)
}
type readConnectorsResponseElement struct {
Provider models.ConnectorProvider `json:"provider" bson:"provider"`
Enabled bool `json:"enabled" bson:"enabled"`
// TODO: remove disabled field when frontend switches to using enabled
Disabled bool `json:"disabled" bson:"disabled"`
}
func readConnectorsHandler(repo readConnectorsRepository) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := repo.ListConnectors(r.Context())
if err != nil {
handleError(w, r, err)
return
}
data := make([]readConnectorsResponseElement, len(res))
for i := range res {
data[i] = readConnectorsResponseElement{
Provider: res[i].Provider,
Enabled: res[i].Enabled,
Disabled: !res[i].Enabled,
}
}
err = json.NewEncoder(w).Encode(
sharedapi.BaseResponse[[]readConnectorsResponseElement]{
Data: &data,
})
if err != nil {
panic(err)
}
}
}