snow/person/attribute.go

58 lines
1.1 KiB
Go
Raw Normal View History

2023-11-01 00:05:17 +00:00
package person
import (
"reflect"
2023-11-01 00:05:17 +00:00
"github.com/ectrc/snow/aid"
2023-11-01 00:05:17 +00:00
"github.com/ectrc/snow/storage"
"github.com/google/uuid"
2023-11-01 00:05:17 +00:00
)
type Attribute struct {
ID string
ProfileID string
Key string
ValueJSON string
2023-11-01 00:05:17 +00:00
Type string
}
func NewAttribute(key string, value interface{}) *Attribute {
2023-11-01 00:05:17 +00:00
return &Attribute{
ID: uuid.New().String(),
Key: key,
ValueJSON: aid.JSONStringify(value),
Type: reflect.TypeOf(value).String(),
2023-11-01 00:05:17 +00:00
}
}
func FromDatabaseAttribute(db *storage.DB_PAttribute) *Attribute {
return &Attribute{
ID: db.ID,
ProfileID: db.ProfileID,
Key: db.Key,
ValueJSON: db.ValueJSON,
Type: db.Type,
2023-11-01 00:05:17 +00:00
}
}
func (a *Attribute) ToDatabase(profileId string) *storage.DB_PAttribute {
return &storage.DB_PAttribute{
ID: a.ID,
2023-11-01 00:05:17 +00:00
ProfileID: profileId,
Key: a.Key,
ValueJSON: a.ValueJSON,
Type: a.Type,
2023-11-01 00:05:17 +00:00
}
}
func (a *Attribute) Delete() {
storage.Repo.DeleteAttribute(a.ID)
}
func (a *Attribute) Save() {
if a.ProfileID == "" {
aid.Print("error saving attribute", a.Key, "profile id is empty")
return
}
storage.Repo.SaveAttribute(a.ToDatabase(a.ProfileID))
2023-11-01 00:05:17 +00:00
}