Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchennn committed Aug 8, 2023
1 parent a3b530a commit 7f65053
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions mongoapi/mongoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func SetMongoDB(setdbName string, url string) error {
return nil
}

func findOneAndDecode(collection *mongo.Collection, filter bson.M, argOpt ...int) (map[string]interface{}, error) {
func findOneAndDecode(collection *mongo.Collection, filter bson.M, strength int) (map[string]interface{}, error) {
var result map[string]interface{}
opts := new(options.FindOneOptions)
if len(argOpt) > 0 {
myCollation := &options.Collation{Locale: "en_US", Strength: argOpt[0]}
opts.SetCollation(myCollation)
}

// Strength 2: Case insensitive, 3: Case sensitive (default)
myCollation := &options.Collation{Locale: "en_US", Strength: strength}
opts.SetCollation(myCollation)

if err := collection.FindOne(context.TODO(), filter, opts).Decode(&result); err != nil {
// ErrNoDocuments means that the filter did not match any documents in
Expand All @@ -51,14 +51,10 @@ func findOneAndDecode(collection *mongo.Collection, filter bson.M, argOpt ...int
return result, nil
}

func getOrigData(collection *mongo.Collection, filter bson.M, argOpt ...int) (
func getOrigData(collection *mongo.Collection, filter bson.M, strength int) (
result map[string]interface{}, err error,
) {
if len(argOpt) == 0 {
result, err = findOneAndDecode(collection, filter)
} else {
result, err = findOneAndDecode(collection, filter, argOpt[0])
}
result, err = findOneAndDecode(collection, filter, strength)

if err != nil {
return nil, err
Expand All @@ -71,7 +67,7 @@ func getOrigData(collection *mongo.Collection, filter bson.M, argOpt ...int) (
}

func checkDataExisted(collection *mongo.Collection, filter bson.M) (bool, error) {
result, err := findOneAndDecode(collection, filter)
result, err := findOneAndDecode(collection, filter, 3)
if err != nil {
return false, err
}
Expand All @@ -83,29 +79,69 @@ func checkDataExisted(collection *mongo.Collection, filter bson.M) (bool, error)

func RestfulAPIGetOne(collName string, filter bson.M, argOpt ...int) (result map[string]interface{}, err error) {
collection := Client.Database(dbName).Collection(collName)
if len(argOpt) == 0 {
result, err = getOrigData(collection, filter)
} else {
result, err = getOrigData(collection, filter, argOpt[0])
result, err = getOrigData(collection, filter, 3)
if err != nil {
return nil, fmt.Errorf("RestfulAPIGetOne err: %+v", err)
}

return result, nil
}

func RestfulAPIGetOneWithStrength(collName string, filter bson.M, strength int) (
result map[string]interface{}, err error,
) {
collection := Client.Database(dbName).Collection(collName)
result, err = getOrigData(collection, filter, strength)
if err != nil {
return nil, fmt.Errorf("RestfulAPIGetOne err: %+v", err)
}

return result, nil
}

func RestfulAPIGetMany(collName string, filter bson.M, argOpt ...int) ([]map[string]interface{}, error) {
func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) {
collection := Client.Database(dbName).Collection(collName)

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

opts := new(options.FindOptions)
if len(argOpt) > 0 {
myCollation := &options.Collation{Locale: "en_US", Strength: argOpt[0]}
opts.SetCollation(myCollation)
cur, err := collection.Find(ctx, filter)
if err != nil {
return nil, fmt.Errorf("RestfulAPIGetMany err: %+v", err)
}
defer func(ctx context.Context) {
if err := cur.Close(ctx); err != nil {
return
}
}(ctx)

var resultArray []map[string]interface{}
for cur.Next(ctx) {
var result map[string]interface{}
if err := cur.Decode(&result); err != nil {
return nil, fmt.Errorf("RestfulAPIGetMany err: %+v", err)
}

// Delete "_id" entry which is auto-inserted by MongoDB
delete(result, "_id")
resultArray = append(resultArray, result)
}
if err := cur.Err(); err != nil {
return nil, fmt.Errorf("RestfulAPIGetMany err: %+v", err)
}

return resultArray, nil
}

func RestfulAPIGetManyWithStrength(collName string, filter bson.M, strength int) ([]map[string]interface{}, error) {
collection := Client.Database(dbName).Collection(collName)

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

opts := new(options.FindOptions)
myCollation := &options.Collation{Locale: "en_US", Strength: strength}
opts.SetCollation(myCollation)

cur, err := collection.Find(ctx, filter, opts)
if err != nil {
Expand Down Expand Up @@ -226,7 +262,7 @@ func RestfulAPIDeleteMany(collName string, filter bson.M) error {
func RestfulAPIMergePatch(collName string, filter bson.M, patchData map[string]interface{}) error {
collection := Client.Database(dbName).Collection(collName)

originalData, err := getOrigData(collection, filter)
originalData, err := getOrigData(collection, filter, 3)
if err != nil {
return fmt.Errorf("RestfulAPIMergePatch getOrigData err: %+v", err)
}
Expand Down Expand Up @@ -259,7 +295,7 @@ func RestfulAPIMergePatch(collName string, filter bson.M, patchData map[string]i
func RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error {
collection := Client.Database(dbName).Collection(collName)

originalData, err := getOrigData(collection, filter)
originalData, err := getOrigData(collection, filter, 3)
if err != nil {
return fmt.Errorf("RestfulAPIJSONPatch getOrigData err: %+v", err)
}
Expand Down Expand Up @@ -292,7 +328,7 @@ func RestfulAPIJSONPatch(collName string, filter bson.M, patchJSON []byte) error
func RestfulAPIJSONPatchExtend(collName string, filter bson.M, patchJSON []byte, dataName string) error {
collection := Client.Database(dbName).Collection(collName)

originalDataCover, err := getOrigData(collection, filter)
originalDataCover, err := getOrigData(collection, filter, 3)
if err != nil {
return fmt.Errorf("RestfulAPIJSONPatchExtend getOrigData err: %+v", err)
}
Expand Down

0 comments on commit 7f65053

Please sign in to comment.