-
Notifications
You must be signed in to change notification settings - Fork 0
/
DummyMongoDbPersistence.go
135 lines (115 loc) · 3.83 KB
/
DummyMongoDbPersistence.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
package test_persistence
import (
"reflect"
cdata "github.com/pip-services3-go/pip-services3-commons-go/data"
persist "github.com/pip-services3-go/pip-services3-mongodb-go/persistence"
"go.mongodb.org/mongo-driver/bson"
)
// extends IdentifiableMongoDbPersistence
// implements IDummyPersistence
type DummyMongoDbPersistence struct {
persist.IdentifiableMongoDbPersistence
}
func NewDummyMongoDbPersistence() *DummyMongoDbPersistence {
proto := reflect.TypeOf(Dummy{})
c := &DummyMongoDbPersistence{}
c.IdentifiableMongoDbPersistence = *persist.InheritIdentifiableMongoDbPersistence(c, proto, "dummies")
return c
}
func (c *DummyMongoDbPersistence) Create(correlationId string, item Dummy) (result Dummy, err error) {
value, err := c.IdentifiableMongoDbPersistence.Create(correlationId, item)
if value != nil {
val, _ := value.(Dummy)
result = val
}
return result, err
}
func (c *DummyMongoDbPersistence) GetListByIds(correlationId string, ids []string) (items []Dummy, err error) {
convIds := make([]interface{}, len(ids))
for i, v := range ids {
convIds[i] = v
}
result, err := c.IdentifiableMongoDbPersistence.GetListByIds(correlationId, convIds)
items = make([]Dummy, len(result))
for i, v := range result {
val, _ := v.(Dummy)
items[i] = val
}
return items, err
}
func (c *DummyMongoDbPersistence) GetOneById(correlationId string, id string) (item Dummy, err error) {
result, err := c.IdentifiableMongoDbPersistence.GetOneById(correlationId, id)
if result != nil {
val, _ := result.(Dummy)
item = val
}
return item, err
}
func (c *DummyMongoDbPersistence) Update(correlationId string, item Dummy) (result Dummy, err error) {
value, err := c.IdentifiableMongoDbPersistence.Update(correlationId, item)
if value != nil {
val, _ := value.(Dummy)
result = val
}
return result, err
}
func (c *DummyMongoDbPersistence) UpdatePartially(correlationId string, id string, data *cdata.AnyValueMap) (item Dummy, err error) {
result, err := c.IdentifiableMongoDbPersistence.UpdatePartially(correlationId, id, data)
if result != nil {
val, _ := result.(Dummy)
item = val
}
return item, err
}
func (c *DummyMongoDbPersistence) DeleteById(correlationId string, id string) (item Dummy, err error) {
result, err := c.IdentifiableMongoDbPersistence.DeleteById(correlationId, id)
if result != nil {
val, _ := result.(Dummy)
item = val
}
return item, err
}
func (c *DummyMongoDbPersistence) DeleteByIds(correlationId string, ids []string) (err error) {
convIds := make([]interface{}, len(ids))
for i, v := range ids {
convIds[i] = v
}
return c.IdentifiableMongoDbPersistence.DeleteByIds(correlationId, convIds)
}
func (c *DummyMongoDbPersistence) GetPageByFilter(correlationId string, filter *cdata.FilterParams, paging *cdata.PagingParams) (page *DummyPage, err error) {
if filter == nil {
filter = cdata.NewEmptyFilterParams()
}
key := filter.GetAsNullableString("Key")
var filterObj bson.M
if key != nil && *key != "" {
filterObj = bson.M{"key": *key}
} else {
filterObj = bson.M{}
}
sorting := bson.M{"key": -1}
tempPage, err := c.IdentifiableMongoDbPersistence.GetPageByFilter(correlationId,
filterObj, paging,
sorting, nil)
// Convert to DummyPage
dataLen := int64(len(tempPage.Data)) // For full release tempPage and delete this by GC
data := make([]Dummy, dataLen)
for i, v := range tempPage.Data {
data[i] = v.(Dummy)
}
page = NewDummyPage(&dataLen, data)
return page, err
}
func (c *DummyMongoDbPersistence) GetCountByFilter(correlationId string, filter *cdata.FilterParams) (count int64, err error) {
if filter == nil {
filter = cdata.NewEmptyFilterParams()
}
key := filter.GetAsNullableString("Key")
var filterObj bson.M
if key != nil && *key != "" {
filterObj = bson.M{"key": *key}
} else {
filterObj = bson.M{}
}
return c.IdentifiableMongoDbPersistence.GetCountByFilter(correlationId, filterObj)
}