2023-11-18 11:12:07 +01:00
|
|
|
package store
|
|
|
|
|
2023-12-21 17:38:05 +01:00
|
|
|
import (
|
2024-02-06 08:35:07 +01:00
|
|
|
"math/rand"
|
2023-12-21 17:38:05 +01:00
|
|
|
"os"
|
2024-02-06 08:35:07 +01:00
|
|
|
"strconv"
|
2023-12-21 17:38:05 +01:00
|
|
|
|
2024-02-06 09:03:57 +01:00
|
|
|
"git.andreafazzi.eu/andrea/probo/pkg/models"
|
2023-12-21 17:38:05 +01:00
|
|
|
"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()
|
|
|
|
|
2023-12-27 15:05:11 +01:00
|
|
|
participants := make([]*models.Participant, 0)
|
2023-12-21 17:38:05 +01:00
|
|
|
|
|
|
|
if err := gocsv.UnmarshalFile(file, &participants); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-27 15:05:11 +01:00
|
|
|
|
|
|
|
memParticipants := make([]*models.Participant, 0)
|
|
|
|
|
|
|
|
for _, p := range participants {
|
2024-02-06 08:35:07 +01:00
|
|
|
if p.Token == "" {
|
|
|
|
p.Token = generateToken()
|
|
|
|
}
|
2023-12-27 15:05:11 +01:00
|
|
|
memParticipant, err := s.Create(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
memParticipants = append(memParticipants, memParticipant)
|
|
|
|
}
|
|
|
|
|
|
|
|
return memParticipants, nil
|
|
|
|
|
2023-12-21 17:38:05 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 08:35:07 +01:00
|
|
|
func generateToken() string {
|
|
|
|
// Generate six random numbers from 1 to 9
|
|
|
|
var token string
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
randomNumber := rand.Intn(9) + 1
|
|
|
|
token += strconv.Itoa(randomNumber)
|
2023-12-05 22:11:08 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 08:35:07 +01:00
|
|
|
return token
|
|
|
|
}
|