diff --git a/dbs/mongo/mongo.go b/dbs/mongo/mongo.go index c4d16ff..479d005 100644 --- a/dbs/mongo/mongo.go +++ b/dbs/mongo/mongo.go @@ -2,7 +2,6 @@ package mongo import ( "context" - "encoding/json" "errors" "time" @@ -161,14 +160,11 @@ func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_na return result.UpsertedID.(primitive.ObjectID).Hex(), 200, nil } -func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, int, error) { - var doc interface{} - b, err := json.Marshal(obj) - if err != nil { - m.Logger.Error().Msg("Couldn't insert resource: " + err.Error()) - return "", 422, err - } - bson.UnmarshalExtJSON(b, false, &doc) +func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) { + var doc map[string]interface{} + b, _ := bson.Marshal(obj) + bson.Unmarshal(b, &doc) + doc["_id"] = id targetDBCollection := CollectionMap[collection_name] MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second) diff --git a/dbs/mongo/mongo_test.go b/dbs/mongo/mongo_test.go deleted file mode 100644 index 4ac00e0..0000000 --- a/dbs/mongo/mongo_test.go +++ /dev/null @@ -1,6 +0,0 @@ -package mongo - -// func TestMongoInit(T *testing.T){ -// MongoInit() -// fmt.Printf("It worked !") -// } diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index 81ebebf..225078f 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -11,7 +11,7 @@ import ( var validate = validator.New(validator.WithRequiredStructEnabled()) type AbstractObject struct { - UUID string `json:"id" required:"true" bson:"_id" validate:"required"` + UUID string `json:"id" required:"true" bson:"id" validate:"required"` Name string `json:"name" required:"true" bson:"name" validate:"required"` } @@ -43,7 +43,7 @@ func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) ( if err != nil { return nil, 422, err } - id, code, err := mongo.MONGOService.StoreOne(data, wfa.GetType()) + id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType()) if err != nil { wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) return nil, code, err