resource as resource named

This commit is contained in:
mr
2024-07-19 10:54:58 +02:00
parent 218714683b
commit 2a9a784ec1
28 changed files with 381 additions and 284 deletions

View File

@@ -7,14 +7,14 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type Data struct {
type DataResource struct {
resources.AbstractResource
Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type
DataType string `json:"datatype" required:"true" bson:"datatype"`
Example string `json:"example" bson:"example" required:"true" validate:"required" description:"base64 encoded data"`
}
func (dma *Data) Deserialize(j map[string]interface{}) utils.DBObject {
func (dma *DataResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
@@ -23,7 +23,7 @@ func (dma *Data) Deserialize(j map[string]interface{}) utils.DBObject {
return dma
}
func (dma *Data) Serialize() map[string]interface{} {
func (dma *DataResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
@@ -33,12 +33,8 @@ func (dma *Data) Serialize() map[string]interface{} {
return m
}
func (d *Data) GetType() resources.ResourceType {
return resources.DATA
}
func (d *Data) GetAccessor() utils.Accessor {
func (d *DataResource) GetAccessor() utils.Accessor {
data := &DataMongoAccessor{}
data.SetLogger(resources.DATA)
data.SetLogger(utils.DATACENTER_RESOURCE)
return data
}

View File

@@ -18,20 +18,11 @@ func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (
}
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data)
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Data), dma.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return dma.LoadOne(id)
return dma.GenericStoreOne(data, dma)
}
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var data Data
var data DataResource
res_mongo, err := mongo.MONGOService.LoadOne(id, dma.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@@ -4,20 +4,20 @@ import (
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneData(t *testing.T) {
d := Data{DataType: "jpeg", Example: "123456",
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
@@ -28,15 +28,14 @@ func TestStoreOneData(t *testing.T) {
}
func TestLoadOneDate(t *testing.T) {
d := Data{DataType: "jpeg", Example: "123456",
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}