add with http code
This commit is contained in:
parent
650168f39c
commit
d28662d70b
@ -131,7 +131,7 @@ func (m *MongoDB) createCollection(collection_name string, new_collection *mongo
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, error) {
|
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
|
||||||
filter := bson.M{"_id": GetObjIDFromString(id)}
|
filter := bson.M{"_id": GetObjIDFromString(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}})
|
||||||
@ -141,12 +141,12 @@ func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, error) {
|
|||||||
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
|
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
||||||
return 0, err
|
return 0, 404, err
|
||||||
}
|
}
|
||||||
return result.DeletedCount, nil
|
return result.DeletedCount, 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, 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": GetObjIDFromString(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)
|
||||||
@ -154,13 +154,13 @@ func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_na
|
|||||||
|
|
||||||
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
|
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
|
||||||
return "", err
|
return "", 404, err
|
||||||
}
|
}
|
||||||
return result.UpsertedID.(primitive.ObjectID).Hex(), nil
|
return result.UpsertedID.(primitive.ObjectID).Hex(), 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, error) {
|
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, int, error) {
|
||||||
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)
|
||||||
@ -169,24 +169,24 @@ func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, err
|
|||||||
result, err := targetDBCollection.InsertOne(MngoCtx, obj)
|
result, err := targetDBCollection.InsertOne(MngoCtx, obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
|
||||||
return "", err
|
return "", 409, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.InsertedID.(primitive.ObjectID).Hex(), nil
|
return result.InsertedID.(primitive.ObjectID).Hex(), 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoDB) LoadOne(id string, collection_name string) (res *mongo.SingleResult, err error) {
|
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
|
||||||
filter := bson.M{"_id": GetObjIDFromString(id)}
|
filter := bson.M{"_id": GetObjIDFromString(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()
|
||||||
|
|
||||||
res = targetDBCollection.FindOne(MngoCtx, filter)
|
res := targetDBCollection.FindOne(MngoCtx, filter)
|
||||||
if res.Err() != nil {
|
if res.Err() != nil {
|
||||||
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
|
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
|
||||||
err = res.Err()
|
err := res.Err()
|
||||||
return nil, err
|
return nil, 404, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,8 @@ func (d LibDataEnum) EnumIndex() int {
|
|||||||
|
|
||||||
type LibData struct {
|
type LibData struct {
|
||||||
DataResource utils.DBObject `bson:"data" json:"data"`
|
DataResource utils.DBObject `bson:"data" json:"data"`
|
||||||
Err error `bson:"error" json:"error"`
|
Code int `bson:"code" json:"code"`
|
||||||
|
Err string `bson:"error" json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(appName string) {
|
func Init(appName string) {
|
||||||
@ -40,24 +41,24 @@ func GetLogger() zerolog.Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadOne(collection LibDataEnum, id string) LibData {
|
func LoadOne(collection LibDataEnum, id string) LibData {
|
||||||
d, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
|
||||||
return LibData{DataResource: d, Err: err}
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
|
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
|
||||||
model := models.Model(collection.EnumIndex())
|
model := models.Model(collection.EnumIndex())
|
||||||
set = model.Deserialize(set).Serialize()
|
set = model.Deserialize(set).Serialize()
|
||||||
d, err := model.GetAccessor().UpdateOne(set, id)
|
d, code, err := model.GetAccessor().UpdateOne(set, id)
|
||||||
return LibData{DataResource: d, Err: err}
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteOne(collection LibDataEnum, id string) LibData {
|
func DeleteOne(collection LibDataEnum, id string) LibData {
|
||||||
d, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
|
||||||
return LibData{DataResource: d, Err: err}
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
||||||
model := models.Model(collection.EnumIndex())
|
model := models.Model(collection.EnumIndex())
|
||||||
d, err := model.GetAccessor().StoreOne(model.Deserialize(object))
|
d, code, err := model.GetAccessor().StoreOne(model.Deserialize(object))
|
||||||
return LibData{DataResource: d, Err: err}
|
return LibData{DataResource: d, Code: code, Err: err.Error()}
|
||||||
}
|
}
|
||||||
|
@ -9,26 +9,26 @@ type DataMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *DataMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (dma *DataMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return dma.GenericDeleteOne(id, dma)
|
return dma.GenericDeleteOne(id, dma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return dma.GenericUpdateOne(set, id, dma)
|
return dma.GenericUpdateOne(set, id, dma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return dma.GenericStoreOne(data, dma)
|
return dma.GenericStoreOne(data, dma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var data DataResource
|
var data DataResource
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, dma.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dma.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
res_mongo.Decode(&data)
|
res_mongo.Decode(&data)
|
||||||
|
|
||||||
return &data, nil
|
return &data, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestStoreOneData(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dma := DataMongoAccessor{}
|
dma := DataMongoAccessor{}
|
||||||
id, _ := dma.StoreOne(&d)
|
id, _, _ := dma.StoreOne(&d)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -40,6 +40,6 @@ func TestLoadOneDate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dma := DataMongoAccessor{}
|
dma := DataMongoAccessor{}
|
||||||
new_d, _ := dma.StoreOne(&d)
|
new_d, _, _ := dma.StoreOne(&d)
|
||||||
assert.Equal(t, d, new_d)
|
assert.Equal(t, d, new_d)
|
||||||
}
|
}
|
||||||
|
@ -9,28 +9,28 @@ type DatacenterMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dca *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (dca *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return dca.GenericDeleteOne(id, dca)
|
return dca.GenericDeleteOne(id, dca)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dca *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (dca *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return dca.GenericUpdateOne(set, id, dca)
|
return dca.GenericUpdateOne(set, id, dca)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return dca.GenericStoreOne(data, dca)
|
return dca.GenericStoreOne(data, dca)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var datacenter DatacenterResource
|
var datacenter DatacenterResource
|
||||||
|
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, dca.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dca.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res_mongo.Decode(&datacenter)
|
res_mongo.Decode(&datacenter)
|
||||||
|
|
||||||
return &datacenter, nil
|
return &datacenter, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestStoreOneDatacenter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dcma := DatacenterMongoAccessor{}
|
dcma := DatacenterMongoAccessor{}
|
||||||
id, _ := dcma.StoreOne(&dc)
|
id, _, _ := dcma.StoreOne(&dc)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ func TestLoadOneDatacenter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dcma := DatacenterMongoAccessor{}
|
dcma := DatacenterMongoAccessor{}
|
||||||
new_dc, _ := dcma.StoreOne(&dc)
|
new_dc, _, _ := dcma.StoreOne(&dc)
|
||||||
|
|
||||||
assert.Equal(t, dc, new_dc)
|
assert.Equal(t, dc, new_dc)
|
||||||
}
|
}
|
||||||
|
@ -9,29 +9,29 @@ type ProcessingMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (pma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return pma.GenericDeleteOne(id, pma)
|
return pma.GenericDeleteOne(id, pma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (pma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return pma.GenericUpdateOne(set, id, pma)
|
return pma.GenericUpdateOne(set, id, pma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return pma.GenericStoreOne(data, pma)
|
return pma.GenericStoreOne(data, pma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
|
||||||
var processing ProcessingResource
|
var processing ProcessingResource
|
||||||
|
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, pma.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res_mongo.Decode(&processing)
|
res_mongo.Decode(&processing)
|
||||||
|
|
||||||
return &processing, nil
|
return &processing, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestStoreOneProcessing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sma := ProcessingMongoAccessor{}
|
sma := ProcessingMongoAccessor{}
|
||||||
id, _ := sma.StoreOne(&p)
|
id, _, _ := sma.StoreOne(&p)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -40,6 +40,6 @@ func TestLoadOneProcessing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sma := ProcessingMongoAccessor{}
|
sma := ProcessingMongoAccessor{}
|
||||||
new_s, _ := sma.StoreOne(&p)
|
new_s, _, _ := sma.StoreOne(&p)
|
||||||
assert.Equal(t, p, new_s)
|
assert.Equal(t, p, new_s)
|
||||||
}
|
}
|
||||||
|
@ -9,29 +9,29 @@ type StorageMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (sma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return sma.GenericDeleteOne(id, sma)
|
return sma.GenericDeleteOne(id, sma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (sma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return sma.GenericUpdateOne(set, id, sma)
|
return sma.GenericUpdateOne(set, id, sma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return sma.GenericStoreOne(data, sma)
|
return sma.GenericStoreOne(data, sma)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
|
||||||
var storage StorageResource
|
var storage StorageResource
|
||||||
|
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, sma.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res_mongo.Decode(&storage)
|
res_mongo.Decode(&storage)
|
||||||
|
|
||||||
return &storage, nil
|
return &storage, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestStoreOneStorage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sma := StorageMongoAccessor{}
|
sma := StorageMongoAccessor{}
|
||||||
id, _ := sma.StoreOne(&s)
|
id, _, _ := sma.StoreOne(&s)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ func TestLoadOneStorage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sma := StorageMongoAccessor{}
|
sma := StorageMongoAccessor{}
|
||||||
new_s, _ := sma.StoreOne(&s)
|
new_s, _, _ := sma.StoreOne(&s)
|
||||||
|
|
||||||
assert.Equal(t, s, new_s)
|
assert.Equal(t, s, new_s)
|
||||||
}
|
}
|
||||||
|
@ -9,25 +9,25 @@ type WorkflowResourceMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (wfa *WorkflowResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericDeleteOne(id, wfa)
|
return wfa.GenericDeleteOne(id, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowResourceMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (wfa *WorkflowResourceMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericUpdateOne(set, id, wfa)
|
return wfa.GenericUpdateOne(set, id, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (wfa *WorkflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return wfa.GenericStoreOne(data, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var workflow WorkflowResource
|
var workflow WorkflowResource
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
res_mongo.Decode(&workflow)
|
res_mongo.Decode(&workflow)
|
||||||
return &workflow, nil
|
return &workflow, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func TestStoreOneWorkflow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wma := WorkflowResourceMongoAccessor{}
|
wma := WorkflowResourceMongoAccessor{}
|
||||||
id, _ := wma.StoreOne(&w)
|
id, _, _ := wma.StoreOne(&w)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -38,6 +38,6 @@ func TestLoadOneWorkflow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wma := WorkflowResourceMongoAccessor{}
|
wma := WorkflowResourceMongoAccessor{}
|
||||||
new_w, _ := wma.StoreOne(&w)
|
new_w, _, _ := wma.StoreOne(&w)
|
||||||
assert.Equal(t, w, new_w)
|
assert.Equal(t, w, new_w)
|
||||||
}
|
}
|
||||||
|
@ -33,39 +33,40 @@ func (dma *AbstractAccessor) SetLogger(t DataType) {
|
|||||||
dma.Type = t.String()
|
dma.Type = t.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, error) {
|
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
|
||||||
data.GenerateID()
|
data.GenerateID()
|
||||||
err := validate.Struct(data)
|
err := validate.Struct(data)
|
||||||
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, 422, err
|
||||||
}
|
}
|
||||||
id, err := mongo.MONGOService.StoreOne(data, wfa.GetType())
|
id, code, err := mongo.MONGOService.StoreOne(data, wfa.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
return accessor.LoadOne(id)
|
return accessor.LoadOne(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
|
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
|
||||||
res, err := accessor.LoadOne(id)
|
res, code, err := accessor.LoadOne(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
_, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
|
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, 200, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, error) {
|
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, int, error) {
|
||||||
id, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
|
id, code, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||||
|
return nil, code, err
|
||||||
}
|
}
|
||||||
return accessor.LoadOne(id)
|
return accessor.LoadOne(id)
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ type DBObject interface {
|
|||||||
type Accessor interface {
|
type Accessor interface {
|
||||||
SetLogger(t DataType)
|
SetLogger(t DataType)
|
||||||
GetType() string
|
GetType() string
|
||||||
LoadOne(id string) (DBObject, error)
|
LoadOne(id string) (DBObject, int, error)
|
||||||
DeleteOne(id string) (DBObject, error)
|
DeleteOne(id string) (DBObject, int, error)
|
||||||
StoreOne(data DBObject) (DBObject, error)
|
StoreOne(data DBObject) (DBObject, int, error)
|
||||||
UpdateOne(set map[string]interface{}, id string) (DBObject, error)
|
UpdateOne(set map[string]interface{}, id string) (DBObject, int, error)
|
||||||
}
|
}
|
||||||
|
@ -9,25 +9,25 @@ type WorkflowMongoAccessor struct {
|
|||||||
utils.AbstractAccessor
|
utils.AbstractAccessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
|
func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericDeleteOne(id, wfa)
|
return wfa.GenericDeleteOne(id, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
|
func (wfa *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericUpdateOne(set, id, wfa)
|
return wfa.GenericUpdateOne(set, id, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
|
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
return wfa.GenericStoreOne(data, wfa)
|
return wfa.GenericStoreOne(data, wfa)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
|
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
var workflow Workflow
|
var workflow Workflow
|
||||||
res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||||
return nil, err
|
return nil, code, err
|
||||||
}
|
}
|
||||||
res_mongo.Decode(&workflow)
|
res_mongo.Decode(&workflow)
|
||||||
return &workflow, nil
|
return &workflow, 200, nil
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ func TestStoreOneWorkflow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wma := WorkflowMongoAccessor{}
|
wma := WorkflowMongoAccessor{}
|
||||||
id, _ := wma.StoreOne(&w)
|
id, _, _ := wma.StoreOne(&w)
|
||||||
|
|
||||||
assert.NotEmpty(t, id)
|
assert.NotEmpty(t, id)
|
||||||
}
|
}
|
||||||
@ -24,6 +24,6 @@ func TestLoadOneWorkflow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wma := WorkflowMongoAccessor{}
|
wma := WorkflowMongoAccessor{}
|
||||||
new_w, _ := wma.StoreOne(&w)
|
new_w, _, _ := wma.StoreOne(&w)
|
||||||
assert.Equal(t, w, new_w)
|
assert.Equal(t, w, new_w)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user