diff --git a/cmd/probo-cli/participant.go b/cmd/probo-cli/participant.go index fb69a2e..74e2cc7 100644 --- a/cmd/probo-cli/participant.go +++ b/cmd/probo-cli/participant.go @@ -14,7 +14,7 @@ func importCSV(cCtx *cli.Context) error { return cli.Exit("Path for the CSV file not given.", 1) } - pStore, err := file.NewParticipantDefaultFileStore() + pStore, err := file.NewDefaultParticipantFileStore() if err != nil { return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1) } diff --git a/cmd/probo-cli/session.go b/cmd/probo-cli/session.go index c9ca7be..239fc73 100644 --- a/cmd/probo-cli/session.go +++ b/cmd/probo-cli/session.go @@ -12,7 +12,7 @@ import ( ) func push(cCtx *cli.Context) error { - pStore, err := file.NewParticipantDefaultFileStore() + pStore, err := file.NewDefaultParticipantFileStore() if err != nil { return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1) } @@ -85,7 +85,7 @@ func pull(cCtx *cli.Context) error { log.Fatalf("An error occurred: %v", err) } - pStore, err := file.NewParticipantDefaultFileStore() + pStore, err := file.NewDefaultParticipantFileStore() if err != nil { log.Fatalf("An error occurred: %v", err) } diff --git a/lib/models/models_test.go b/lib/models/models_test.go index 7a6fa1b..314e157 100644 --- a/lib/models/models_test.go +++ b/lib/models/models_test.go @@ -77,3 +77,7 @@ func (t *testSuite) TestMarkdownFromQuiz() { `, md) } } + +func (t *testSuite) TestParticipantTokenMarshalCSV() { + +} diff --git a/lib/models/participant.go b/lib/models/participant.go index 1d0d3b0..3636d9c 100644 --- a/lib/models/participant.go +++ b/lib/models/participant.go @@ -12,7 +12,6 @@ import ( type AttributeList map[string]string type Participant struct { - // ID string `csv:"id" gorm:"primaryKey"` Meta Firstname string @@ -32,7 +31,7 @@ func (p *Participant) GetHash() string { } func (p *Participant) AttributesToSlice() []string { - result := make([]string, len(p.Attributes)*2) + result := make([]string, 0) for k, v := range p.Attributes { result = append(result, k, v) @@ -81,6 +80,14 @@ func (al AttributeList) Get(key string) string { return al[key] } +func (al AttributeList) String() string { + result := make([]string, 0) + for k, v := range al { + result = append(result, fmt.Sprintf("%s: %s", k, v)) + } + return strings.Join(result, ",") +} + func convertMapToKeyValueOrderedString(m map[string]string) string { keys := make([]string, 0, len(m)) for key := range m { diff --git a/lib/store/file/testdata/exams/participants/jack.json b/lib/store/file/testdata/exams/participants/jack.json index b30a4a0..92d7c8b 100644 --- a/lib/store/file/testdata/exams/participants/jack.json +++ b/lib/store/file/testdata/exams/participants/jack.json @@ -1 +1 @@ -{"id":"5467","created_at":"2023-12-05T22:00:51.525533451+01:00","updated_at":"2023-12-27T15:04:47.492868083+01:00","Firstname":"Jack","Lastname":"Sparrow","Token":"333444","Attributes":{"class":"2 D LIN"}} \ No newline at end of file +{"id":"5467","created_at":"2023-12-05T22:00:51.525533451+01:00","updated_at":"2024-01-24T21:17:51.893846564+01:00","Firstname":"Jack","Lastname":"Sparrow","Token":"333444","Attributes":{"class":"2 D LIN"}} \ No newline at end of file diff --git a/lib/store/file/testdata/exams/participants/john.json b/lib/store/file/testdata/exams/participants/john.json index e21f056..4506c5f 100644 --- a/lib/store/file/testdata/exams/participants/john.json +++ b/lib/store/file/testdata/exams/participants/john.json @@ -1 +1 @@ -{"id":"1234","created_at":"2023-12-05T22:00:51.525601298+01:00","updated_at":"2023-12-27T15:04:47.493037183+01:00","Firstname":"John","Lastname":"Smith","Token":"111222","Attributes":{"class":"1 D LIN"}} \ No newline at end of file +{"id":"1234","created_at":"2023-12-05T22:00:51.525601298+01:00","updated_at":"2024-01-24T21:17:51.894021827+01:00","Firstname":"John","Lastname":"Smith","Token":"111222","Attributes":{"class":"1 D LIN"}} \ No newline at end of file diff --git a/lib/store/file/testdata/exams/participants/wendy.json b/lib/store/file/testdata/exams/participants/wendy.json index d8e7300..c80ded7 100644 --- a/lib/store/file/testdata/exams/participants/wendy.json +++ b/lib/store/file/testdata/exams/participants/wendy.json @@ -1 +1 @@ -{"id":"567812","created_at":"2023-12-05T22:00:51.525667963+01:00","updated_at":"2023-12-27T15:04:47.493141634+01:00","Firstname":"Wendy","Lastname":"Darling","Token":"333444","Attributes":{"class":"2 D LIN"}} \ No newline at end of file +{"id":"567812","created_at":"2023-12-05T22:00:51.525667963+01:00","updated_at":"2024-01-24T21:17:51.894119666+01:00","Firstname":"Wendy","Lastname":"Darling","Token":"333444","Attributes":{"class":"2 D LIN"}} \ No newline at end of file diff --git a/lib/store/participant.go b/lib/store/participant.go index c32476c..a59474c 100644 --- a/lib/store/participant.go +++ b/lib/store/participant.go @@ -1,7 +1,9 @@ package store import ( + "math/rand" "os" + "strconv" "git.andreafazzi.eu/andrea/probo/lib/models" "github.com/gocarina/gocsv" @@ -34,6 +36,9 @@ func (s *ParticipantStore) ImportCSV(path string) ([]*models.Participant, error) memParticipants := make([]*models.Participant, 0) for _, p := range participants { + if p.Token == "" { + p.Token = generateToken() + } memParticipant, err := s.Create(p) if err != nil { return nil, err @@ -45,26 +50,37 @@ func (s *ParticipantStore) ImportCSV(path string) ([]*models.Participant, error) } -func (s *ParticipantStore) FilterInGroup(group *models.Group, filter map[string]string) []*models.Participant { - participants := s.ReadAll() - - if filter == nil { - return participants +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) } - filteredParticipants := s.Filter(participants, func(p *models.Participant) bool { - for pk, pv := range p.Attributes { - for fk, fv := range filter { - if pk == fk && pv == fv { - return true - } - } - } - - return false - }) - - group.Participants = filteredParticipants - - return group.Participants + return token } + +// func (s *ParticipantStore) FilterInGroup(group *models.Group, filter map[string]string) []*models.Participant { +// participants := s.ReadAll() + +// if filter == nil { +// return participants +// } + +// filteredParticipants := s.Filter(participants, func(p *models.Participant) bool { +// for pk, pv := range p.Attributes { +// for fk, fv := range filter { +// if pk == fk && pv == fv { +// return true +// } +// } +// } + +// return false +// }) + +// group.Participants = filteredParticipants + +// return group.Participants +// } diff --git a/lib/store/participant_test.go b/lib/store/participant_test.go index 201143c..5e093c5 100644 --- a/lib/store/participant_test.go +++ b/lib/store/participant_test.go @@ -59,4 +59,5 @@ func (t *participantTestSuite) TestImportCSV() { t.Nil(err) t.Equal(3, len(participants)) t.Equal("1 D LIN", participants[0].Attributes.Get("class")) + t.Equal(6, len(participants[0].Token)) }