This commit is contained in:
mr 2024-07-22 14:46:49 +02:00
parent b1c3c056df
commit 7a49f6b957

View File

@ -10,7 +10,6 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
@ -132,7 +131,7 @@ func (m *MongoDB) createCollection(collection_name string, new_collection *mongo
} }
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) { func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)} filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}}) opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@ -147,17 +146,17 @@ func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, erro
} }
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, int, error) { func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)} filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true)) _, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
if err != nil { if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error()) m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err return "", 404, err
} }
return result.UpsertedID.(primitive.ObjectID).Hex(), 200, nil return id, 200, nil
} }
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) { func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
@ -180,7 +179,7 @@ func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (
} }
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) { func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
filter := bson.M{"_id": GetObjIDFromString(id)} filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name] targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)