-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsource_type_handlers.go
71 lines (54 loc) · 1.58 KB
/
source_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
package main
import (
"net/http"
"strconv"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
)
// function that defines how we get the dao - default implementation below.
var getSourceTypeDao func(c echo.Context) (dao.SourceTypeDao, error)
func getSourceTypeDaoWithoutTenant(_ echo.Context) (dao.SourceTypeDao, error) {
// we do not need tenancy for source type.
return dao.GetSourceTypeDao(), nil
}
func SourceTypeList(c echo.Context) error {
sourceTypeDB, err := getSourceTypeDao(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
}
sourceTypes, count, err := sourceTypeDB.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(sourceTypes))
for i := 0; i < len(sourceTypes); i++ {
out[i] = sourceTypes[i].ToResponse()
}
return c.JSON(http.StatusOK, util.CollectionResponse(out, c.Request(), int(count), limit, offset))
}
func SourceTypeGet(c echo.Context) error {
SourceTypeDB, err := getSourceTypeDao(c)
if err != nil {
return err
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return util.NewErrBadRequest(err)
}
sourceType, err := SourceTypeDB.GetById(&id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, sourceType.ToResponse())
}