Skip to content

Commit

Permalink
chore: update version to include MongoDB transaction functions (#101)
Browse files Browse the repository at this point in the history
* update version to include MongoDB transaction functions

Signed-off-by: Patricia Reinoso <[email protected]>

* add PUT operation with context

Signed-off-by: Patricia Reinoso <[email protected]>

---------

Signed-off-by: Patricia Reinoso <[email protected]>
  • Loading branch information
patriciareinoso authored Jan 16, 2025
1 parent 09af132 commit 995e97d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.9-dev
1.2.9
5 changes: 5 additions & 0 deletions mongoapi/dbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type DBInterface interface {
RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error)
RestfulAPIPutOneTimeout(collName string, filter bson.M, putData map[string]interface{}, timeout int32, timeField string) bool
RestfulAPIPutOne(collName string, filter bson.M, putData map[string]interface{}) (bool, error)
RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error)
RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) (bool, error)
RestfulAPIPutMany(collName string, filterArray []primitive.M, putDataArray []map[string]interface{}) error
RestfulAPIDeleteOne(collName string, filter bson.M) error
Expand Down Expand Up @@ -87,6 +88,10 @@ func (db *MongoDBClient) RestfulAPIPutOne(collName string, filter bson.M, putDat
return db.MongoClient.RestfulAPIPutOne(collName, filter, putData)
}

func (db *MongoDBClient) RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error) {
return db.MongoClient.RestfulAPIPutOneWithContext(collName, filter, putData, context)
}

func (db *MongoDBClient) RestfulAPIPutOneNotUpdate(collName string, filter bson.M, putData map[string]interface{}) (bool, error) {
return db.MongoClient.RestfulAPIPutOneNotUpdate(collName, filter, putData)
}
Expand Down
9 changes: 7 additions & 2 deletions mongoapi/mongoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,25 @@ func (c *MongoClient) RestfulAPIGetMany(collName string, filter bson.M) ([]map[s

// if no error happened, return true means data existed and false means data not existed
func (c *MongoClient) RestfulAPIPutOne(collName string, filter bson.M, putData map[string]interface{}) (bool, error) {
return c.RestfulAPIPutOneWithContext(collName, filter, putData, context.TODO())
}

// if no error happened, return true means data existed and false means data not existed
func (c *MongoClient) RestfulAPIPutOneWithContext(collName string, filter bson.M, putData map[string]interface{}, context context.Context) (bool, error) {
collection := c.Client.Database(c.dbName).Collection(collName)
existed, err := checkDataExisted(collection, filter)
if err != nil {
return false, fmt.Errorf("RestfulAPIPutOne err: %+v", err)
}

if existed {
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": putData}); err != nil {
if _, err := collection.UpdateOne(context, filter, bson.M{"$set": putData}); err != nil {
return false, fmt.Errorf("RestfulAPIPutOne UpdateOne err: %+v", err)
}
return true, nil
}

if _, err := collection.InsertOne(context.TODO(), putData); err != nil {
if _, err := collection.InsertOne(context, putData); err != nil {
return false, fmt.Errorf("RestfulAPIPutOne InsertOne err: %+v", err)
}
return false, nil
Expand Down

0 comments on commit 995e97d

Please sign in to comment.