/
source_type_dao.go
92 lines (73 loc) · 2.49 KB
/
source_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
package dao
import (
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
)
// GetSourceTypeDao is a function definition that can be replaced in runtime in case some other DAO provider is
// needed.
var GetSourceTypeDao func() SourceTypeDao
// getDefaultRhcConnectionDao gets the default DAO implementation which will have the given tenant ID.
func getDefaultSourceTypeDao() SourceTypeDao {
return &sourceTypeDaoImpl{}
}
// init sets the default DAO implementation so that other packages can request it easily.
func init() {
GetSourceTypeDao = getDefaultSourceTypeDao
}
type sourceTypeDaoImpl struct {
}
func (st *sourceTypeDaoImpl) List(limit, offset int, filters []util.Filter) ([]m.SourceType, int64, error) {
// allocating a slice of source types, initial length of
// 0, size of limit (since we will not be returning more than that)
sourceTypes := make([]m.SourceType, 0, limit)
query := DB.Debug().Model(&m.SourceType{})
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(&sourceTypes)
if result.Error != nil {
return nil, 0, util.NewErrBadRequest(result.Error)
}
return sourceTypes, count, nil
}
func (st *sourceTypeDaoImpl) GetById(id *int64) (*m.SourceType, error) {
var sourceType m.SourceType
err := DB.Debug().
Model(&m.SourceType{}).
Where("id = ?", *id).
First(&sourceType).
Error
if err != nil {
return nil, util.NewErrNotFound("source type")
}
return &sourceType, nil
}
func (st *sourceTypeDaoImpl) GetByName(name string) (*m.SourceType, error) {
sourceTypes := make([]m.SourceType, 0)
result := DB.Debug().
Where("name LIKE ?", "%"+name+"%").
Find(&sourceTypes)
if result.Error != nil {
return nil, result.Error
}
if result.RowsAffected > int64(1) {
return nil, util.NewErrBadRequest("Found more than one of the same source type name")
} else if result.RowsAffected == int64(0) {
return nil, util.NewErrNotFound("source type")
}
return &sourceTypes[0], nil
}
func (st *sourceTypeDaoImpl) Create(_ *m.SourceType) error {
panic("not needed (yet) due to seeding.")
}
func (st *sourceTypeDaoImpl) Update(_ *m.SourceType) error {
panic("not needed (yet) due to seeding.")
}
func (st *sourceTypeDaoImpl) Delete(_ *int64) error {
panic("not needed (yet) due to seeding.")
}