-
Notifications
You must be signed in to change notification settings - Fork 29
/
meta_data_handlers.go
115 lines (88 loc) · 2.28 KB
/
meta_data_handlers.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
package main
import (
"net/http"
"strconv"
"github.com/RedHatInsights/sources-api-go/dao"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
)
// function that defines how we get the dao - default implementation below.
var getMetaDataDao func(c echo.Context) (dao.MetaDataDao, error)
func getMetaDataDaoWithoutTenant(c echo.Context) (dao.MetaDataDao, error) {
return dao.GetMetaDataDao(), nil
}
func MetaDataList(c echo.Context) error {
metaDataDB, err := getMetaDataDao(c)
if err != nil {
return err
}
filters, err := getFilters(c)
if err != nil {
return err
}
limit, offset, err := getLimitAndOffset(c)
if err != nil {
return err
}
var (
metaDatas []m.MetaData
count int64
)
metaDatas, count, err = metaDataDB.List(limit, offset, filters)
if err != nil {
return err
}
out := make([]interface{}, len(metaDatas))
for i := 0; i < len(metaDatas); i++ {
out[i] = metaDatas[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func ApplicationTypeListMetaData(c echo.Context) error {
metaDataDB, err := getMetaDataDao(c)
if err != nil {
return err
}
filters, err := getFilters(c)
if err != nil {
return err
}
limit, offset, err := getLimitAndOffset(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("application_type_id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
var (
metaDatas []m.MetaData
count int64
)
metaDatas, count, err = metaDataDB.SubCollectionList(m.ApplicationType{Id: id}, limit, offset, filters)
if err != nil {
return err
}
out := make([]interface{}, len(metaDatas))
for i := 0; i < len(metaDatas); i++ {
out[i] = metaDatas[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func MetaDataGet(c echo.Context) error {
metaDataDB, err := getMetaDataDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
c.Logger().Infof("Getting MetaData ID %v", id)
app, err := metaDataDB.GetById(&id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, app.ToResponse())
}