Add generateToken function

This commit is contained in:
andrea 2024-02-06 08:35:07 +01:00
parent ea38c49009
commit 81274dcf89
9 changed files with 56 additions and 28 deletions

View file

@ -14,7 +14,7 @@ func importCSV(cCtx *cli.Context) error {
return cli.Exit("Path for the CSV file not given.", 1) return cli.Exit("Path for the CSV file not given.", 1)
} }
pStore, err := file.NewParticipantDefaultFileStore() pStore, err := file.NewDefaultParticipantFileStore()
if err != nil { if err != nil {
return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1) return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1)
} }

View file

@ -12,7 +12,7 @@ import (
) )
func push(cCtx *cli.Context) error { func push(cCtx *cli.Context) error {
pStore, err := file.NewParticipantDefaultFileStore() pStore, err := file.NewDefaultParticipantFileStore()
if err != nil { if err != nil {
return cli.Exit(fmt.Sprintf("An error occurred: %v", err), 1) 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) log.Fatalf("An error occurred: %v", err)
} }
pStore, err := file.NewParticipantDefaultFileStore() pStore, err := file.NewDefaultParticipantFileStore()
if err != nil { if err != nil {
log.Fatalf("An error occurred: %v", err) log.Fatalf("An error occurred: %v", err)
} }

View file

@ -77,3 +77,7 @@ func (t *testSuite) TestMarkdownFromQuiz() {
`, md) `, md)
} }
} }
func (t *testSuite) TestParticipantTokenMarshalCSV() {
}

View file

@ -12,7 +12,6 @@ import (
type AttributeList map[string]string type AttributeList map[string]string
type Participant struct { type Participant struct {
// ID string `csv:"id" gorm:"primaryKey"`
Meta Meta
Firstname string Firstname string
@ -32,7 +31,7 @@ func (p *Participant) GetHash() string {
} }
func (p *Participant) AttributesToSlice() []string { func (p *Participant) AttributesToSlice() []string {
result := make([]string, len(p.Attributes)*2) result := make([]string, 0)
for k, v := range p.Attributes { for k, v := range p.Attributes {
result = append(result, k, v) result = append(result, k, v)
@ -81,6 +80,14 @@ func (al AttributeList) Get(key string) string {
return al[key] 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 { func convertMapToKeyValueOrderedString(m map[string]string) string {
keys := make([]string, 0, len(m)) keys := make([]string, 0, len(m))
for key := range m { for key := range m {

View file

@ -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"}} {"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"}}

View file

@ -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"}} {"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"}}

View file

@ -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"}} {"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"}}

View file

@ -1,7 +1,9 @@
package store package store
import ( import (
"math/rand"
"os" "os"
"strconv"
"git.andreafazzi.eu/andrea/probo/lib/models" "git.andreafazzi.eu/andrea/probo/lib/models"
"github.com/gocarina/gocsv" "github.com/gocarina/gocsv"
@ -34,6 +36,9 @@ func (s *ParticipantStore) ImportCSV(path string) ([]*models.Participant, error)
memParticipants := make([]*models.Participant, 0) memParticipants := make([]*models.Participant, 0)
for _, p := range participants { for _, p := range participants {
if p.Token == "" {
p.Token = generateToken()
}
memParticipant, err := s.Create(p) memParticipant, err := s.Create(p)
if err != nil { if err != nil {
return nil, err 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 { func generateToken() string {
participants := s.ReadAll() // Generate six random numbers from 1 to 9
var token string
if filter == nil { for i := 0; i < 6; i++ {
return participants randomNumber := rand.Intn(9) + 1
token += strconv.Itoa(randomNumber)
} }
filteredParticipants := s.Filter(participants, func(p *models.Participant) bool { return token
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
} }
// 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
// }

View file

@ -59,4 +59,5 @@ func (t *participantTestSuite) TestImportCSV() {
t.Nil(err) t.Nil(err)
t.Equal(3, len(participants)) t.Equal(3, len(participants))
t.Equal("1 D LIN", participants[0].Attributes.Get("class")) t.Equal("1 D LIN", participants[0].Attributes.Get("class"))
t.Equal(6, len(participants[0].Token))
} }