2023-11-01 00:05:17 +00:00
|
|
|
package person
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-11-01 21:33:25 +00:00
|
|
|
"reflect"
|
2023-11-01 00:05:17 +00:00
|
|
|
|
|
|
|
"github.com/ectrc/snow/storage"
|
2023-11-01 21:33:25 +00:00
|
|
|
"github.com/google/uuid"
|
2023-11-01 00:05:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Attribute struct {
|
2023-11-01 21:33:25 +00:00
|
|
|
ID string
|
2023-11-01 00:05:17 +00:00
|
|
|
Key string
|
|
|
|
Value interface{}
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:33:25 +00:00
|
|
|
func NewAttribute(key string, value interface{}) *Attribute {
|
2023-11-01 00:05:17 +00:00
|
|
|
return &Attribute{
|
2023-11-01 21:33:25 +00:00
|
|
|
ID: uuid.New().String(),
|
2023-11-01 00:05:17 +00:00
|
|
|
Key: key,
|
|
|
|
Value: value,
|
2023-11-01 21:33:25 +00:00
|
|
|
Type: reflect.TypeOf(value).String(),
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
|
|
|
|
var value interface{}
|
|
|
|
err := json.Unmarshal([]byte(db.ValueJSON), &value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Attribute{
|
2023-11-01 21:33:25 +00:00
|
|
|
ID: db.ID,
|
2023-11-01 00:05:17 +00:00
|
|
|
Key: db.Key,
|
|
|
|
Value: value,
|
|
|
|
Type: db.Type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
|
|
|
|
value, err := json.Marshal(a.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &storage.DB_PAttribute{
|
2023-11-01 21:33:25 +00:00
|
|
|
ID: a.ID,
|
2023-11-01 00:05:17 +00:00
|
|
|
ProfileID: profileId,
|
2023-11-01 21:33:25 +00:00
|
|
|
Key: a.Key,
|
2023-11-01 00:05:17 +00:00
|
|
|
ValueJSON: string(value),
|
2023-11-01 21:33:25 +00:00
|
|
|
Type: a.Type,
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
2023-11-01 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) Delete() {
|
|
|
|
storage.Repo.DeleteAttribute(a.ID)
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|