add with http code

This commit is contained in:
mr 2024-07-19 11:27:58 +02:00
parent 650168f39c
commit d28662d70b
16 changed files with 96 additions and 94 deletions

View File

@ -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)}
targetDBCollection := CollectionMap[collection_name]
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)
if err != nil {
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)}
targetDBCollection := CollectionMap[collection_name]
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))
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
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]
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)
if err != nil {
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)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res = targetDBCollection.FindOne(MngoCtx, filter)
res := targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err = res.Err()
return nil, err
err := res.Err()
return nil, 404, err
}
return res, nil
return res, 200, nil
}

View File

@ -26,7 +26,8 @@ func (d LibDataEnum) EnumIndex() int {
type LibData struct {
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) {
@ -40,24 +41,24 @@ func GetLogger() zerolog.Logger {
}
func LoadOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
return LibData{DataResource: d, Err: err}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
return LibData{DataResource: d, Code: code, Err: err.Error()}
}
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
model := models.Model(collection.EnumIndex())
set = model.Deserialize(set).Serialize()
d, err := model.GetAccessor().UpdateOne(set, id)
return LibData{DataResource: d, Err: err}
d, code, err := model.GetAccessor().UpdateOne(set, id)
return LibData{DataResource: d, Code: code, Err: err.Error()}
}
func DeleteOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
return LibData{DataResource: d, Err: err}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
return LibData{DataResource: d, Code: code, Err: err.Error()}
}
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
model := models.Model(collection.EnumIndex())
d, err := model.GetAccessor().StoreOne(model.Deserialize(object))
return LibData{DataResource: d, Err: err}
d, code, err := model.GetAccessor().StoreOne(model.Deserialize(object))
return LibData{DataResource: d, Code: code, Err: err.Error()}
}

View File

@ -9,26 +9,26 @@ type DataMongoAccessor struct {
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)
}
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)
}
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)
}
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&data)
return &data, nil
return &data, 200, nil
}

View File

@ -22,7 +22,7 @@ func TestStoreOneData(t *testing.T) {
}
dma := DataMongoAccessor{}
id, _ := dma.StoreOne(&d)
id, _, _ := dma.StoreOne(&d)
assert.NotEmpty(t, id)
}
@ -40,6 +40,6 @@ func TestLoadOneDate(t *testing.T) {
}
dma := DataMongoAccessor{}
new_d, _ := dma.StoreOne(&d)
new_d, _, _ := dma.StoreOne(&d)
assert.Equal(t, d, new_d)
}

View File

@ -9,28 +9,28 @@ type DatacenterMongoAccessor struct {
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)
}
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)
}
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)
}
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&datacenter)
return &datacenter, nil
return &datacenter, 200, nil
}

View File

@ -22,7 +22,7 @@ func TestStoreOneDatacenter(t *testing.T) {
}
dcma := DatacenterMongoAccessor{}
id, _ := dcma.StoreOne(&dc)
id, _, _ := dcma.StoreOne(&dc)
assert.NotEmpty(t, id)
}
@ -40,7 +40,7 @@ func TestLoadOneDatacenter(t *testing.T) {
}
dcma := DatacenterMongoAccessor{}
new_dc, _ := dcma.StoreOne(&dc)
new_dc, _, _ := dcma.StoreOne(&dc)
assert.Equal(t, dc, new_dc)
}

View File

@ -9,29 +9,29 @@ type ProcessingMongoAccessor struct {
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)
}
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)
}
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)
}
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&processing)
return &processing, nil
return &processing, 200, nil
}

View File

@ -22,7 +22,7 @@ func TestStoreOneProcessing(t *testing.T) {
}
sma := ProcessingMongoAccessor{}
id, _ := sma.StoreOne(&p)
id, _, _ := sma.StoreOne(&p)
assert.NotEmpty(t, id)
}
@ -40,6 +40,6 @@ func TestLoadOneProcessing(t *testing.T) {
}
sma := ProcessingMongoAccessor{}
new_s, _ := sma.StoreOne(&p)
new_s, _, _ := sma.StoreOne(&p)
assert.Equal(t, p, new_s)
}

View File

@ -9,29 +9,29 @@ type StorageMongoAccessor struct {
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)
}
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)
}
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)
}
func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&storage)
return &storage, nil
return &storage, 200, nil
}

View File

@ -22,7 +22,7 @@ func TestStoreOneStorage(t *testing.T) {
}
sma := StorageMongoAccessor{}
id, _ := sma.StoreOne(&s)
id, _, _ := sma.StoreOne(&s)
assert.NotEmpty(t, id)
}
@ -40,7 +40,7 @@ func TestLoadOneStorage(t *testing.T) {
}
sma := StorageMongoAccessor{}
new_s, _ := sma.StoreOne(&s)
new_s, _, _ := sma.StoreOne(&s)
assert.Equal(t, s, new_s)
}

View File

@ -9,25 +9,25 @@ type WorkflowResourceMongoAccessor struct {
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)
}
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)
}
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)
}
func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, nil
return &workflow, 200, nil
}

View File

@ -21,7 +21,7 @@ func TestStoreOneWorkflow(t *testing.T) {
}
wma := WorkflowResourceMongoAccessor{}
id, _ := wma.StoreOne(&w)
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
@ -38,6 +38,6 @@ func TestLoadOneWorkflow(t *testing.T) {
}
wma := WorkflowResourceMongoAccessor{}
new_w, _ := wma.StoreOne(&w)
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}

View File

@ -33,39 +33,40 @@ func (dma *AbstractAccessor) SetLogger(t DataType) {
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()
err := validate.Struct(data)
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
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 {
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)
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
res, err := accessor.LoadOne(id)
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
res, code, err := accessor.LoadOne(id)
if err != nil {
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 {
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) {
id, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, int, error) {
id, code, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}

View File

@ -11,8 +11,8 @@ type DBObject interface {
type Accessor interface {
SetLogger(t DataType)
GetType() string
LoadOne(id string) (DBObject, error)
DeleteOne(id string) (DBObject, error)
StoreOne(data DBObject) (DBObject, error)
UpdateOne(set map[string]interface{}, id string) (DBObject, error)
LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error)
StoreOne(data DBObject) (DBObject, int, error)
UpdateOne(set map[string]interface{}, id string) (DBObject, int, error)
}

View File

@ -9,25 +9,25 @@ type WorkflowMongoAccessor struct {
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)
}
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)
}
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)
}
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
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 {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, nil
return &workflow, 200, nil
}

View File

@ -13,7 +13,7 @@ func TestStoreOneWorkflow(t *testing.T) {
}
wma := WorkflowMongoAccessor{}
id, _ := wma.StoreOne(&w)
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
@ -24,6 +24,6 @@ func TestLoadOneWorkflow(t *testing.T) {
}
wma := WorkflowMongoAccessor{}
new_w, _ := wma.StoreOne(&w)
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}