diff --git a/persistence/IdentifiableMongoDbPersistence.go b/persistence/IdentifiableMongoDbPersistence.go index 04b726e..c69bf31 100644 --- a/persistence/IdentifiableMongoDbPersistence.go +++ b/persistence/IdentifiableMongoDbPersistence.go @@ -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 +} diff --git a/test/persistence/DummyMapMongoDbPersistence.go b/test/persistence/DummyMapMongoDbPersistence.go index 01925a4..8a699ce 100644 --- a/test/persistence/DummyMapMongoDbPersistence.go +++ b/test/persistence/DummyMapMongoDbPersistence.go @@ -8,8 +8,6 @@ import ( "go.mongodb.org/mongo-driver/bson" ) -// extends IdentifiableMongoDbPersistence -// implements IDummyMapPersistence { type DummyMapMongoDbPersistence struct { mngpersist.IdentifiableMongoDbPersistence } @@ -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) +} diff --git a/test/persistence/DummyMongoDbPersistence.go b/test/persistence/DummyMongoDbPersistence.go index 3f840a2..035972f 100644 --- a/test/persistence/DummyMongoDbPersistence.go +++ b/test/persistence/DummyMongoDbPersistence.go @@ -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) +} diff --git a/test/persistence/DummyRefMongoDbPersistence.go b/test/persistence/DummyRefMongoDbPersistence.go index ae3e310..3948ffb 100644 --- a/test/persistence/DummyRefMongoDbPersistence.go +++ b/test/persistence/DummyRefMongoDbPersistence.go @@ -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) +} diff --git a/test/persistence/IDummyMapPersistence.go b/test/persistence/IDummyMapPersistence.go index a83d99c..8f881e3 100644 --- a/test/persistence/IDummyMapPersistence.go +++ b/test/persistence/IDummyMapPersistence.go @@ -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) } diff --git a/test/persistence/IDummyPersistence.go b/test/persistence/IDummyPersistence.go index 3441f9f..15942f7 100644 --- a/test/persistence/IDummyPersistence.go +++ b/test/persistence/IDummyPersistence.go @@ -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) } diff --git a/test/persistence/IDummyRefPersistence.go b/test/persistence/IDummyRefPersistence.go index 93073dc..ec282f0 100644 --- a/test/persistence/IDummyRefPersistence.go +++ b/test/persistence/IDummyRefPersistence.go @@ -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) }