Skip to content

Commit

Permalink
Add getCountByFilter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed May 19, 2020
1 parent b6510e8 commit b57f0ad
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
19 changes: 19 additions & 0 deletions persistence/IdentifiableMongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,22 @@ func (c *IdentifiableMongoDbPersistence) GetConvResult(docPointer reflect.Value,
}
return item
}

// GetCountByFilter is gets a count of data items retrieved by a given filter.
// This method shall be called by a func (c *IdentifiableMongoDbPersistence) GetCountByFilter method from child type that
// receives FilterParams and converts them into a filter function.
// Parameters:
// - correlationId string
// (optional) transaction id to Trace execution through call chain.
// - filter interface{}
// Returns count int, err error
// a data count or error, if they are occured
func (c *IdentifiableMongoDbPersistence) GetCountByFilter(correlationId string, filter interface{}) (count int64, err error) {

// Configure options
var options mngoptions.CountOptions
count = 0
count, err = c.Collection.CountDocuments(c.Connection.Ctx, filter, &options)
c.Logger.Trace(correlationId, "Find %d items in %s", count, c.CollectionName)
return count, err
}
18 changes: 16 additions & 2 deletions test/persistence/DummyMapMongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

// extends IdentifiableMongoDbPersistence<DummyMap, string>
// implements IDummyMapPersistence {
type DummyMapMongoDbPersistence struct {
mngpersist.IdentifiableMongoDbPersistence
}
Expand Down Expand Up @@ -116,3 +114,19 @@ func (c *DummyMapMongoDbPersistence) GetPageByFilter(correlationId string, filte
dataPage := NewMapPage(&dataLen, data)
return dataPage, err
}

func (c *DummyMapMongoDbPersistence) 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 != "" {
filterObj = bson.M{"key": *key}
} else {
filterObj = bson.M{}
}
return c.IdentifiableMongoDbPersistence.GetCountByFilter(correlationId, filterObj)
}
16 changes: 16 additions & 0 deletions test/persistence/DummyMongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ func (c *DummyMongoDbPersistence) GetPageByFilter(correlationId string, filter *
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 != "" {
filterObj = bson.M{"key": *key}
} else {
filterObj = bson.M{}
}
return c.IdentifiableMongoDbPersistence.GetCountByFilter(correlationId, filterObj)
}
16 changes: 16 additions & 0 deletions test/persistence/DummyRefMongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ func (c *DummyRefMongoDbPersistence) GetPageByFilter(correlationId string, filte
page = NewDummyRefPage(&dataLen, data)
return page, err
}

func (c *DummyRefMongoDbPersistence) 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 != "" {
filterObj = bson.M{"key": *key}
} else {
filterObj = bson.M{}
}
return c.IdentifiableMongoDbPersistence.GetCountByFilter(correlationId, filterObj)
}
1 change: 1 addition & 0 deletions test/persistence/IDummyMapPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type IDummyMapPersistence interface {
UpdatePartially(correlationId string, id string, data *cdata.AnyValueMap) (item map[string]interface{}, err error)
DeleteById(correlationId string, id string) (item map[string]interface{}, err error)
DeleteByIds(correlationId string, ids []string) (err error)
GetCountByFilter(correlationId string, filter *cdata.FilterParams) (count int64, err error)
}
1 change: 1 addition & 0 deletions test/persistence/IDummyPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type IDummyPersistence interface {
UpdatePartially(correlationId string, id string, data *cdata.AnyValueMap) (item Dummy, err error)
DeleteById(correlationId string, id string) (item Dummy, err error)
DeleteByIds(correlationId string, ids []string) (err error)
GetCountByFilter(correlationId string, filter *cdata.FilterParams) (count int64, err error)
}
1 change: 1 addition & 0 deletions test/persistence/IDummyRefPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type IDummyRefPersistence interface {
UpdatePartially(correlationId string, id string, data *cdata.AnyValueMap) (item *Dummy, err error)
DeleteById(correlationId string, id string) (item *Dummy, err error)
DeleteByIds(correlationId string, ids []string) (err error)
GetCountByFilter(correlationId string, filter *cdata.FilterParams) (count int64, err error)
}

0 comments on commit b57f0ad

Please sign in to comment.