/
application_type_dao.go
184 lines (147 loc) · 5.57 KB
/
application_type_dao.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dao
import (
"fmt"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
)
// GetApplicationTypeDao is a function definition that can be replaced in runtime in case some other DAO provider is
// needed.
var GetApplicationTypeDao func(*int64) ApplicationTypeDao
// getDefaultApplicationAuthenticationDao gets the default DAO implementation which will have the given tenant ID.
func getDefaultApplicationTypeDao(tenantId *int64) ApplicationTypeDao {
return &applicationTypeDaoImpl{
TenantID: tenantId,
}
}
// init sets the default DAO implementation so that other packages can request it easily.
func init() {
GetApplicationTypeDao = getDefaultApplicationTypeDao
}
type applicationTypeDaoImpl struct {
TenantID *int64
}
func (at *applicationTypeDaoImpl) SubCollectionList(primaryCollection interface{}, limit, offset int, filters []util.Filter) ([]m.ApplicationType, int64, error) {
// allocating a slice of application types, initial length of
// 0, size of limit (since we will not be returning more than that)
applicationTypes := make([]m.ApplicationType, 0, limit)
relationObject, err := m.NewRelationObject(primaryCollection, *at.TenantID, DB.Debug())
if err != nil {
return nil, 0, err
}
query := relationObject.HasMany(&m.ApplicationType{}, DB.Debug())
query, err = applyFilters(query, filters)
if err != nil {
return nil, 0, util.NewErrBadRequest(err.Error())
}
// getting the total count (filters included) for pagination
count := int64(0)
query.Model(&m.ApplicationType{}).Count(&count)
// limiting + running the actual query.
result := query.Limit(limit).Offset(offset).Find(&applicationTypes)
if result.Error != nil {
return nil, 0, util.NewErrBadRequest(result.Error)
}
return applicationTypes, count, nil
}
func (at *applicationTypeDaoImpl) List(limit, offset int, filters []util.Filter) ([]m.ApplicationType, int64, error) {
// allocating a slice of application types, initial length of
// 0, size of limit (since we will not be returning more than that)
appTypes := make([]m.ApplicationType, 0, limit)
query := DB.Debug().Model(&m.ApplicationType{})
query, err := applyFilters(query, filters)
if err != nil {
return nil, 0, util.NewErrBadRequest(err)
}
// getting the total count (filters included) for pagination
count := int64(0)
query.Count(&count)
// limiting + running the actual query.
result := query.Limit(limit).Offset(offset).Find(&appTypes)
if result.Error != nil {
return nil, 0, util.NewErrBadRequest(result.Error)
}
return appTypes, count, nil
}
func (at *applicationTypeDaoImpl) GetById(id *int64) (*m.ApplicationType, error) {
var appType m.ApplicationType
err := DB.Debug().
Model(&m.ApplicationType{}).
Where("id = ?", *id).
First(&appType).
Error
if err != nil {
return nil, util.NewErrNotFound("application type")
}
return &appType, nil
}
func (at *applicationTypeDaoImpl) GetByName(name string) (*m.ApplicationType, error) {
appTypes := make([]m.ApplicationType, 0)
result := DB.Debug().
Where("name LIKE ?", "%"+name+"%").
Find(&appTypes)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected > int64(1) {
return nil, util.NewErrBadRequest("Found more than one of the same application type name")
} else if result.RowsAffected == int64(0) {
return nil, util.NewErrNotFound("application type")
}
return &appTypes[0], nil
}
func (at *applicationTypeDaoImpl) Create(_ *m.ApplicationType) error {
panic("not needed (yet) due to seeding.")
}
func (at *applicationTypeDaoImpl) Update(_ *m.ApplicationType) error {
panic("not needed (yet) due to seeding.")
}
func (at *applicationTypeDaoImpl) Delete(_ *int64) error {
panic("not needed (yet) due to seeding.")
}
func (at *applicationTypeDaoImpl) ApplicationTypeCompatibleWithSource(typeId, sourceId int64) error {
// Looks up the source ID and then compare's the source-type's name with the
// application type's supported source types
var source m.Source
err := DB.Debug().
Model(&m.Source{}).
Where("id = ?", sourceId).
Preload("SourceType").
Find(&source).
Error
if err != nil {
return fmt.Errorf("source not found")
}
return at.ApplicationTypeCompatibleWithSourceType(typeId, source.SourceType.Id)
}
func (at *applicationTypeDaoImpl) ApplicationTypeCompatibleWithSourceType(appTypeId, sourceTypeId int64) error {
// searching for the application type that has the source type's name in its
// supported source types column.
//
// initially i tried to use the
// datatypes.JsonQuery("application_types.supported_source_types") but that
// doesn't work when we're specifying something joined in, in this case
// "source_types.name"
result := DB.Debug().
Select("application_types.*").
Joins("LEFT JOIN source_types ON source_types.id = ?", sourceTypeId).
Where("application_types.id = ?", appTypeId).
Where("application_types.supported_source_types::jsonb ? source_types.name").
First(&m.ApplicationType{})
return result.Error
}
func (at *applicationTypeDaoImpl) GetSuperKeyResultType(applicationTypeId int64, authType string) (string, error) {
resultType := ""
// selecting the authtype's supported authentication types for superkey via
// jsonb query
//
// the short story is that we're pulling the `authType` key out of the
// supportedAuthenticationTypes which is an array and then plucking index 0
result := DB.Debug().
Model(&m.ApplicationType{Id: applicationTypeId}).
Select("application_types.supported_authentication_types::json -> ? ->> 0", authType).
Scan(&resultType)
if result.Error != nil {
return "", result.Error
}
return resultType, result.Error
}