2023-11-01 00:05:17 +00:00
|
|
|
package person
|
|
|
|
|
|
|
|
import (
|
2023-11-01 21:33:25 +00:00
|
|
|
"reflect"
|
2023-11-01 00:05:17 +00:00
|
|
|
|
2023-11-05 01:58:00 +00:00
|
|
|
"github.com/ectrc/snow/aid"
|
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-05 01:58:00 +00:00
|
|
|
ID string
|
|
|
|
ProfileID string
|
|
|
|
Key string
|
|
|
|
ValueJSON string
|
2023-11-01 00:05:17 +00:00
|
|
|
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-05 01:58:00 +00:00
|
|
|
ID: uuid.New().String(),
|
|
|
|
Key: key,
|
|
|
|
ValueJSON: aid.JSONStringify(value),
|
|
|
|
Type: reflect.TypeOf(value).String(),
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-04 02:05:31 +00:00
|
|
|
func FromDatabaseAttribute(db *storage.DB_Attribute) *Attribute {
|
2023-11-01 00:05:17 +00:00
|
|
|
return &Attribute{
|
2023-11-05 01:58:00 +00:00
|
|
|
ID: db.ID,
|
|
|
|
ProfileID: db.ProfileID,
|
|
|
|
Key: db.Key,
|
|
|
|
ValueJSON: db.ValueJSON,
|
|
|
|
Type: db.Type,
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-04 02:05:31 +00:00
|
|
|
func (a *Attribute) ToDatabase(profileId string) *storage.DB_Attribute {
|
|
|
|
return &storage.DB_Attribute{
|
2023-11-05 01:58:00 +00:00
|
|
|
ID: a.ID,
|
2023-11-01 00:05:17 +00:00
|
|
|
ProfileID: profileId,
|
2023-11-05 01:58:00 +00:00
|
|
|
Key: a.Key,
|
|
|
|
ValueJSON: a.ValueJSON,
|
|
|
|
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-05 01:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) Save() {
|
|
|
|
if a.ProfileID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
storage.Repo.SaveAttribute(a.ToDatabase(a.ProfileID))
|
2024-02-03 02:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AttributeConvertToSlice[T any](attribute *Attribute) []T {
|
|
|
|
valuesRaw := aid.JSONParse(attribute.ValueJSON).([]interface{})
|
|
|
|
values := make([]T, len(valuesRaw))
|
|
|
|
for i, value := range valuesRaw {
|
|
|
|
values[i] = value.(T)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
func AttributeConvert[T any](attribute *Attribute) T {
|
|
|
|
return aid.JSONParse(attribute.ValueJSON).(T)
|
2023-11-01 00:05:17 +00:00
|
|
|
}
|