Skip to content

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Aug 5, 2020
1 parent 698656a commit e8a93a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
12 changes: 9 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# MongoDB components for Pip.Services in Golang Changelog

## <a name="1.0.2"></a> 1.0.2 (2020-07-12)
## <a name="1.0.4"></a> 1.0.4 (2020-08-05)

### Features
* Moved some CRUD operations from IdentifiableMongoDbPersistence to MongoDbPersistence
* Rafactoring code

## <a name="1.0.3"></a> 1.0.3 (2020-08-04)

### Features
* Fix returns data in GetPageByFilter method
* Fix returns data in GetPageByFilter method

## <a name="1.0.2"></a> 1.0.2 (2020-07-12)

### Features
* Moved some CRUD operations from IdentifiableMongoDbPersistence to MongoDbPersistence

41 changes: 11 additions & 30 deletions persistence/IdentifiableMongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *IdentifiableMongoDbPersistence) GetListByIds(correlationId string, ids
// - callback callback function that receives data item or error.
func (c *IdentifiableMongoDbPersistence) GetOneById(correlationId string, id interface{}) (item interface{}, err error) {
filter := bson.M{"_id": id}
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
foRes := c.Collection.FindOne(c.Connection.Ctx, filter)
ferr := foRes.Decode(docPointer.Interface())
if ferr != nil {
Expand All @@ -190,7 +190,7 @@ func (c *IdentifiableMongoDbPersistence) GetOneById(correlationId string, id int
c.Logger.Trace(correlationId, "Retrieved from %s by id = %s", c.CollectionName, id)
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand Down Expand Up @@ -256,7 +256,7 @@ func (c *IdentifiableMongoDbPersistence) Set(correlationId string, item interfac
return nil, frRes.Err()
}
c.Logger.Trace(correlationId, "Set in %s with id = %s", c.CollectionName, id)
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
err = frRes.Decode(docPointer.Interface())
if err != nil {
if err == mongo.ErrNoDocuments {
Expand All @@ -266,7 +266,7 @@ func (c *IdentifiableMongoDbPersistence) Set(correlationId string, item interfac
}
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand Down Expand Up @@ -295,7 +295,7 @@ func (c *IdentifiableMongoDbPersistence) Update(correlationId string, item inter
return nil, fuRes.Err()
}
c.Logger.Trace(correlationId, "Updated in %s with id = %s", c.CollectionName, id)
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
err = fuRes.Decode(docPointer.Interface())
if err != nil {
if err == mongo.ErrNoDocuments {
Expand All @@ -305,7 +305,7 @@ func (c *IdentifiableMongoDbPersistence) Update(correlationId string, item inter
}
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand Down Expand Up @@ -337,7 +337,7 @@ func (c *IdentifiableMongoDbPersistence) UpdatePartially(correlationId string, i
return nil, fuRes.Err()
}
c.Logger.Trace(correlationId, "Updated partially in %s with id = %s", c.Collection, id)
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
err = fuRes.Decode(docPointer.Interface())
if err != nil {
if err == mongo.ErrNoDocuments {
Expand All @@ -347,7 +347,7 @@ func (c *IdentifiableMongoDbPersistence) UpdatePartially(correlationId string, i
}
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand All @@ -366,7 +366,7 @@ func (c *IdentifiableMongoDbPersistence) DeleteById(correlationId string, id int
return nil, fdRes.Err()
}
c.Logger.Trace(correlationId, "Deleted from %s with id = %s", c.CollectionName, id)
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
err = fdRes.Decode(docPointer.Interface())
if err != nil {
if err == mongo.ErrNoDocuments {
Expand All @@ -376,7 +376,7 @@ func (c *IdentifiableMongoDbPersistence) DeleteById(correlationId string, id int
}
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand All @@ -393,23 +393,4 @@ func (c *IdentifiableMongoDbPersistence) DeleteByIds(correlationId string, ids [
"_id": bson.M{"$in": ids},
}
return c.DeleteByFilter(correlationId, filter)
}

// 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
}
}
33 changes: 25 additions & 8 deletions persistence/MongoDbPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,14 @@ func (c *MongoDbPersistence) GetPageByFilter(correlationId string, filter interf
return page, ferr
}
for cursor.Next(c.Connection.Ctx) {
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
curErr := cursor.Decode(docPointer.Interface())
if curErr != nil {
continue
}
// item := docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item := c.GetConvResult(docPointer, c.Prototype)
item := c.ConvertResultToPublic(docPointer, c.Prototype)
items = append(items, item)
}
if items != nil {
Expand Down Expand Up @@ -534,15 +534,15 @@ func (c *MongoDbPersistence) GetListByFilter(correlationId string, filter interf
}

for cursor.Next(c.Connection.Ctx) {
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
curErr := cursor.Decode(docPointer.Interface())
if curErr != nil {
continue
}

// item := docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item := c.GetConvResult(docPointer, c.Prototype)
item := c.ConvertResultToPublic(docPointer, c.Prototype)

items = append(items, item)
}
Expand Down Expand Up @@ -583,14 +583,14 @@ func (c *MongoDbPersistence) GetOneRandom(correlationId string, filter interface
if fndErr != nil {
return nil, fndErr
}
docPointer := c.GetProtoPtr()
docPointer := c.NewObjectByPrototype()
err = cursor.Decode(docPointer.Interface())
if err != nil {
return nil, err
}
// item = docPointer.Elem().Interface()
// c.ConvertToPublic(&item)
item = c.GetConvResult(docPointer, c.Prototype)
item = c.ConvertResultToPublic(docPointer, c.Prototype)
return item, nil
}

Expand Down Expand Up @@ -646,21 +646,38 @@ func (c *MongoDbPersistence) DeleteByFilter(correlationId string, filter interfa
return nil
}

// 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 *MongoDbPersistence) 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
}




// service function for return pointer on new prototype object for unmarshaling
func (c *MongoDbPersistence) GetProtoPtr() reflect.Value {
func (c *MongoDbPersistence) NewObjectByPrototype() reflect.Value {
proto := c.Prototype
if proto.Kind() == reflect.Ptr {
proto = proto.Elem()
}
return reflect.New(proto)
}

func (c *MongoDbPersistence) GetConvResult(docPointer reflect.Value, proto reflect.Type) interface{} {
func (c *MongoDbPersistence) ConvertResultToPublic(docPointer reflect.Value, proto reflect.Type) interface{} {
item := docPointer.Elem().Interface()
c.ConvertToPublic(&item)
if proto.Kind() == reflect.Ptr {
Expand Down

0 comments on commit e8a93a9

Please sign in to comment.