participant.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package store
  2. import (
  3. "math/rand"
  4. "os"
  5. "strconv"
  6. "git.andreafazzi.eu/andrea/probo/pkg/models"
  7. "github.com/gocarina/gocsv"
  8. )
  9. type ParticipantStore struct {
  10. *FilterStore[*models.Participant]
  11. }
  12. func NewParticipantStore() *ParticipantStore {
  13. store := new(ParticipantStore)
  14. store.FilterStore = NewFilterStore[*models.Participant]()
  15. return store
  16. }
  17. func (s *ParticipantStore) ImportCSV(path string) ([]*models.Participant, error) {
  18. file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, os.ModePerm)
  19. if err != nil {
  20. return nil, err
  21. }
  22. defer file.Close()
  23. participants := make([]*models.Participant, 0)
  24. if err := gocsv.UnmarshalFile(file, &participants); err != nil {
  25. return nil, err
  26. }
  27. memParticipants := make([]*models.Participant, 0)
  28. for _, p := range participants {
  29. if p.Token == "" {
  30. p.Token = generateToken()
  31. }
  32. memParticipant, err := s.Create(p)
  33. if err != nil {
  34. return nil, err
  35. }
  36. memParticipants = append(memParticipants, memParticipant)
  37. }
  38. return memParticipants, nil
  39. }
  40. func generateToken() string {
  41. // Generate six random numbers from 1 to 9
  42. var token string
  43. for i := 0; i < 6; i++ {
  44. randomNumber := rand.Intn(9) + 1
  45. token += strconv.Itoa(randomNumber)
  46. }
  47. return token
  48. }
  49. // func (s *ParticipantStore) FilterInGroup(group *models.Group, filter map[string]string) []*models.Participant {
  50. // participants := s.ReadAll()
  51. // if filter == nil {
  52. // return participants
  53. // }
  54. // filteredParticipants := s.Filter(participants, func(p *models.Participant) bool {
  55. // for pk, pv := range p.Attributes {
  56. // for fk, fv := range filter {
  57. // if pk == fk && pv == fv {
  58. // return true
  59. // }
  60. // }
  61. // }
  62. // return false
  63. // })
  64. // group.Participants = filteredParticipants
  65. // return group.Participants
  66. // }