2023-11-18 11:12:07 +01:00
|
|
|
package store
|
|
|
|
|
2023-12-21 17:38:05 +01:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/probo/lib/models"
|
|
|
|
"github.com/gocarina/gocsv"
|
|
|
|
)
|
2023-11-18 11:12:07 +01:00
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
type ParticipantStore struct {
|
|
|
|
*FilterStore[*models.Participant]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParticipantStore() *ParticipantStore {
|
|
|
|
store := new(ParticipantStore)
|
|
|
|
store.FilterStore = NewFilterStore[*models.Participant]()
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2023-12-21 17:38:05 +01:00
|
|
|
func (s *ParticipantStore) ImportCSV(path string) ([]*models.Participant, error) {
|
|
|
|
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
participants := []*models.Participant{}
|
|
|
|
|
|
|
|
if err := gocsv.UnmarshalFile(file, &participants); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return participants, nil
|
|
|
|
}
|
|
|
|
|
2023-12-05 22:11:08 +01:00
|
|
|
func (s *ParticipantStore) FilterInGroup(group *models.Group, filter map[string]string) []*models.Participant {
|
2023-11-21 15:12:13 +01:00
|
|
|
participants := s.ReadAll()
|
2023-12-05 22:11:08 +01:00
|
|
|
|
|
|
|
if filter == nil {
|
|
|
|
return participants
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
filteredParticipants := s.Filter(participants, func(p *models.Participant) bool {
|
|
|
|
for pk, pv := range p.Attributes {
|
2023-12-05 22:11:08 +01:00
|
|
|
for fk, fv := range filter {
|
2023-11-21 15:12:13 +01:00
|
|
|
if pk == fk && pv == fv {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 22:11:08 +01:00
|
|
|
|
2023-11-21 15:12:13 +01:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
group.Participants = filteredParticipants
|
|
|
|
|
|
|
|
return group.Participants
|
|
|
|
}
|