-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapplication_type_handlers.go
128 lines (100 loc) · 2.9 KB
/
application_type_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
116
117
118
119
120
121
122
123
124
125
126
127
128
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"
echoUtils "github.com/RedHatInsights/sources-api-go/util/echo"
"github.com/labstack/echo/v4"
)
// function that defines how we get the dao - default implementation below.
var getApplicationTypeDao func(c echo.Context) (dao.ApplicationTypeDao, error)
func getApplicationTypeDaoWithTenant(c echo.Context) (dao.ApplicationTypeDao, error) {
tenantId, err := echoUtils.GetTenantFromEchoContext(c)
if err != nil {
return nil, err
}
if tenantId == 0 {
return dao.GetApplicationTypeDao(nil), nil
} else {
return dao.GetApplicationTypeDao(&tenantId), nil
}
}
func SourceListApplicationTypes(c echo.Context) error {
applicationTypeDB, err := getApplicationTypeDao(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 (
appTypes []m.ApplicationType
count int64
)
id, err := strconv.ParseInt(c.Param("source_id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
appTypes, count, err = applicationTypeDB.SubCollectionList(m.Source{ID: id}, limit, offset, filters)
if err != nil {
return err
}
// converting the objects to the interface type so the collection response can process it
// allocating the length of our collection (so it doesn't have to resize)
out := make([]interface{}, len(appTypes))
for i := 0; i < len(appTypes); i++ {
out[i] = appTypes[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func ApplicationTypeList(c echo.Context) error {
applicationTypeDB, err := getApplicationTypeDao(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 (
appTypes []m.ApplicationType
count int64
)
appTypes, count, err = applicationTypeDB.List(limit, offset, filters)
if err != nil {
return err
}
// converting the objects to the interface type so the collection response can process it
// allocating the length of our collection (so it doesn't have to resize)
out := make([]interface{}, len(appTypes))
for i := 0; i < len(appTypes); i++ {
out[i] = appTypes[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func ApplicationTypeGet(c echo.Context) error {
applicationTypeDB, err := getApplicationTypeDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
appType, err := applicationTypeDB.GetById(&id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, appType.ToResponse())
}