organised in packages

This commit is contained in:
pb 2024-07-17 17:48:20 +02:00
parent 02b0a2a595
commit 2684703da5
4 changed files with 109 additions and 0 deletions

23
data/data.go Normal file
View File

@ -0,0 +1,23 @@
package data
import (
oclib "oc-lib"
)
type Data struct {
oclib.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 (d *Data) GetType() oclib.ResourceType{
return oclib.DATA
}

View File

@ -0,0 +1,38 @@
package data
import (
logger "oc-lib/logs"
mongo "oc-lib/mongo"
)
type DataMongoAccessor struct{
}
func (dma *DataMongoAccessor) StoreOne(data Data) string {
id, err := mongo.StoreOne(data,"data")
if err != nil{
l := logger.CreateLogger("oclib","")
l.Error().Msg("Could not store " + data.Name + " to db. Error: " + err.Error())
return ""
}
return id
}
func (dma *DataMongoAccessor) LoadOne(id string) Data {
var data Data
res_mongo, err := mongo.LoadOne(id,"data")
if err != nil{
l := logger.CreateLogger("oclib","")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return Data{}
}
res_mongo.Decode(&data)
return data
}

47
data/data_test.go Normal file
View File

@ -0,0 +1,47 @@
package data
import (
oclib "oc-lib"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStoreOneData(t *testing.T){
d := Data{DataType: "jpeg", Example: "123456",
AbstractResource: oclib.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo : "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := DataMongoAccessor{}
id := dma.StoreOne(d)
assert.NotEmpty(t, id)
}
func TestLoadOneDate(t *testing.T){
d := Data{DataType: "jpeg", Example: "123456",
AbstractResource: oclib.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo : "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := DataMongoAccessor{}
id := dma.StoreOne(d)
new_d := dma.LoadOne(id)
assert.Equal(t,d, new_d)
}

View File

@ -0,0 +1 @@
package data