probo/pkg/store/file/participant.go

54 lines
1.4 KiB
Go
Raw Normal View History

package file
import (
2024-02-06 09:03:57 +01:00
"git.andreafazzi.eu/andrea/probo/pkg/models"
"git.andreafazzi.eu/andrea/probo/pkg/store"
)
2023-12-27 15:05:11 +01:00
type ParticipantFileStore struct {
*FileStore[*models.Participant, *store.ParticipantStore]
}
2023-11-21 15:12:13 +01:00
func NewParticipantFileStore(config *FileStoreConfig[*models.Participant, *store.ParticipantStore]) (*ParticipantFileStore, error) {
2023-12-27 15:05:11 +01:00
var err error
pStore := new(ParticipantFileStore)
pStore.FileStore, err = NewFileStore[*models.Participant, *store.ParticipantStore](config, store.NewParticipantStore())
if err != nil {
return nil, err
}
return pStore, nil
2023-11-21 15:12:13 +01:00
}
2023-12-27 15:05:11 +01:00
func NewDefaultParticipantFileStore() (*ParticipantFileStore, error) {
2023-11-21 15:12:13 +01:00
return NewParticipantFileStore(
&FileStoreConfig[*models.Participant, *store.ParticipantStore]{
FilePathConfig: FilePathConfig{GetDefaultParticipantsDir(), "participant", ".json"},
IndexDirFunc: DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
2023-11-21 18:24:10 +01:00
CreateEntityFunc: func() *models.Participant {
return &models.Participant{
Attributes: make(map[string]string),
}
},
2023-11-21 15:12:13 +01:00
},
)
}
2023-12-27 15:05:11 +01:00
func (ps *ParticipantFileStore) ImportCSV(path string) ([]*models.Participant, error) {
participants, err := ps.FileStore.Storer.ImportCSV(path)
if err != nil {
return nil, err
}
for _, p := range participants {
_, err := ps.Create(p)
if err != nil {
return nil, err
}
}
return participants, nil
}