浏览代码

Add generateToken function

andrea 3 月之前
父节点
当前提交
81274dcf89

+ 1 - 1
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)
 	}

+ 2 - 2
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)
 	}

+ 4 - 0
lib/models/models_test.go

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

+ 9 - 2
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 {

+ 1 - 1
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"}}
+{"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"}}

+ 1 - 1
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"}}
+{"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"}}

+ 1 - 1
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"}}
+{"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"}}

+ 34 - 18
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 token
+}
 
-		return false
-	})
+// func (s *ParticipantStore) FilterInGroup(group *models.Group, filter map[string]string) []*models.Participant {
+// 	participants := s.ReadAll()
 
-	group.Participants = filteredParticipants
+// 	if filter == nil {
+// 		return participants
+// 	}
 
-	return group.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
+// }

+ 1 - 0
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))
 }