Add username field

This commit is contained in:
Andrea Fazzi 2025-11-05 08:27:49 +01:00
commit 5fd79917b3
4 changed files with 67 additions and 17 deletions

View file

@ -14,6 +14,7 @@ type AttributeList map[string]string
type Participant struct {
Meta
Username string `json:"username" csv:"username"`
Firstname string `json:"firstname" csv:"firstname"`
Lastname string `json:"lastname" csv:"lastname"`
@ -23,11 +24,11 @@ type Participant struct {
}
func (p *Participant) String() string {
return fmt.Sprintf("%s %s", p.Lastname, p.Firstname)
return p.Username
}
func (p *Participant) GetHash() string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(append([]string{p.Lastname, p.Firstname}, p.AttributesToSlice()...), ""))))
return fmt.Sprintf("%x", sha256.Sum256([]byte(p.Username)))
}
func (p *Participant) AttributesToSlice() []string {

View file

@ -10,7 +10,7 @@ import (
)
var DefaultFilePathFunc = func(p *models.Participant) string {
return fmt.Sprintf("%s_%s", strings.ReplaceAll(p.String(), " ", "_"), p.ID[:file.UuidFirstN])
return fmt.Sprintf("%s_%s", strings.ReplaceAll(p.String(), ".", "_"), p.ID[:file.UuidFirstN])
}
type Store struct {
@ -21,7 +21,7 @@ func DefaultConfig() *file.Config[*models.Participant, *participant.Store] {
return &file.Config[*models.Participant, *participant.Store]{
Root: ".",
FilePrefix: "participant_",
FileSuffix: "json",
FileSuffix: ".json",
FilePathFunc: DefaultFilePathFunc,
IndexDirFunc: file.DefaultIndexDirFunc[*models.Participant, *participant.Store],
CreateEntityFunc: func() *models.Participant {

View file

@ -2,6 +2,7 @@ package store
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
@ -10,6 +11,8 @@ import (
"github.com/google/uuid"
)
var ErrEmptyHash = errors.New("Hash string cannot be empty!")
type Storable interface {
GetHash() string
@ -65,13 +68,19 @@ func (s *Store[T]) Create(entity T) (T, error) {
s.lock.Lock()
defer s.lock.Unlock()
if hash := entity.GetHash(); hash != "" {
storedEntity, ok := s.hashes[hash]
if ok {
storedEntity.SetDuplicate(entity.GetMeta())
return storedEntity, nil
}
hash := entity.GetHash()
if hash == "" {
return entity, ErrEmptyHash
}
storedEntity, ok := s.hashes[hash]
if ok {
entity.SetID(storedEntity.GetID())
s.hashes[hash] = entity
s.ids[storedEntity.GetID()] = entity
return entity, nil
}
id := entity.GetID()
@ -82,16 +91,11 @@ func (s *Store[T]) Create(entity T) (T, error) {
entity.SetID(id)
if !entity.GetCreatedAt().IsZero() {
entity.SetUpdatedAt(time.Now())
} else {
if entity.GetCreatedAt().IsZero() {
entity.SetCreatedAt(time.Now())
}
if entity.GetUpdatedAt().IsZero() {
entity.SetUpdatedAt(time.Now())
}
s.hashes[hash] = entity
s.ids[id] = entity
return entity, nil

45
pkg/store/store_test.go Normal file
View file

@ -0,0 +1,45 @@
package store
import (
"testing"
"git.andreafazzi.eu/andrea/probo/pkg/models"
"github.com/remogatto/prettytest"
)
type storeTestSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(storeTestSuite),
)
}
func (t *storeTestSuite) TestCreate() {
store := New[*models.Participant]()
created, err := store.Create(&models.Participant{
Username: "john.doe",
Lastname: "Doe",
Firstname: "John",
Token: "123456",
Attributes: map[string]string{"class": "1 D LIN"},
})
t.Nil(err)
t.True(created.ID != "")
updated, err := store.Create(&models.Participant{
Username: "john.doe",
Lastname: "Doe",
Firstname: "John",
Token: "654321",
Attributes: map[string]string{"class": "1 D LIN"},
})
t.True(created.ID == updated.ID)
t.Equal("654321", updated.Token)
}