Merge branch 'master' into response_2

This commit is contained in:
Andrea Fazzi 2019-12-13 13:25:51 +01:00
commit 24ad134e66
2 changed files with 28 additions and 0 deletions

View file

@ -3,6 +3,9 @@ package orm
import (
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"net/http"
"time"
@ -173,3 +176,23 @@ func SaveContest(contest interface{}) (interface{}, error) {
}
return contest, nil
}
func (c *Contest) generateQuestionsOrder() string {
var order []string
count := 0
generated := make(map[int]bool, 0)
for count == len(c.Questions) {
number := rand.Intn(len(c.Questions) + 1)
if generated[number] {
continue
}
generated[number] = true
order = append(order, strconv.Itoa(number))
count++
}
log.Println(strings.Join(order, " "))
return strings.Join(order, " ")
}

View file

@ -116,6 +116,11 @@ func (model *Participant) AfterSave(tx *gorm.DB) error {
}).Error; err != nil {
return err
}
if err := tx.Model(&response).Update("QuestionsOrder", contest.generateQuestionsOrder()).Error; err != nil {
return err
}
}
return nil
}