54 lines
1,020 B
Go
54 lines
1,020 B
Go
package models
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Participant struct {
|
|
ID string `csv:"id" gorm:"primaryKey"`
|
|
|
|
Firstname string `csv:"firstname"`
|
|
Lastname string `csv:"lastname"`
|
|
|
|
Token uint `csv:"token"`
|
|
|
|
Attributes map[string]string
|
|
}
|
|
|
|
func (p *Participant) String() string {
|
|
return fmt.Sprintf("%s %s", p.Lastname, p.Firstname)
|
|
}
|
|
|
|
func (p *Participant) GetID() string {
|
|
return p.ID
|
|
}
|
|
|
|
func (p *Participant) SetID(id string) {
|
|
p.ID = id
|
|
}
|
|
|
|
func (p *Participant) GetHash() string {
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(append([]string{p.Lastname, p.Firstname}, p.AttributesToSlice()...), ""))))
|
|
}
|
|
|
|
func (p *Participant) AttributesToSlice() []string {
|
|
result := make([]string, len(p.Attributes)*2)
|
|
|
|
for k, v := range p.Attributes {
|
|
result = append(result, k, v)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (p *Participant) Marshal() ([]byte, error) {
|
|
return json.Marshal(p)
|
|
|
|
}
|
|
|
|
func (p *Participant) Unmarshal(data []byte) error {
|
|
return json.Unmarshal(data, p)
|
|
}
|