insert uuid

This commit is contained in:
mr 2024-07-22 14:02:48 +02:00
parent f7dfbe91dd
commit 9f17532646
3 changed files with 7 additions and 17 deletions

View File

@ -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)

View File

@ -1,6 +0,0 @@
package mongo
// func TestMongoInit(T *testing.T){
// MongoInit()
// fmt.Printf("It worked !")
// }

View File

@ -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