Produce test db using import script

This commit is contained in:
Andrea Fazzi 2020-01-11 08:03:56 +01:00
parent a4f0169dcf
commit 013f9717b4
6 changed files with 414 additions and 49 deletions

355
compose/sql/oef_test.sql Normal file

File diff suppressed because one or more lines are too long

View file

@ -27,6 +27,8 @@ type Participant struct {
Firstname string Firstname string
Lastname string Lastname string
Password string `gorm:"-"`
FiscalCode string FiscalCode string
CategoryID uint `schema:"category_id"` CategoryID uint `schema:"category_id"`
@ -107,6 +109,7 @@ func (model *Participant) BeforeSave(tx *gorm.DB) error {
if err := tx.FirstOrCreate(&user, &User{ if err := tx.FirstOrCreate(&user, &User{
Username: model.username(), Username: model.username(),
Password: model.Password,
Role: "participant", Role: "participant",
}).Error; err != nil { }).Error; err != nil {
return err return err

View file

@ -26,6 +26,13 @@ func (q *Question) String() string {
return q.Text return q.Text
} }
func (q *Question) BeforeCreate(tx *gorm.DB) error {
if len(q.Answers) > 0 {
q.Answers[0].Correct = true
}
return nil
}
func (q *Question) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { func (q *Question) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" { if r.Method == "GET" {
question := new(Question) question := new(Question)

View file

@ -139,6 +139,7 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht
qOrder := make([]uint, 0) qOrder := make([]uint, 0)
qIDs := strings.Split(response.QuestionsOrder, " ") qIDs := strings.Split(response.QuestionsOrder, " ")
log.Print("QO", response.QuestionsOrder)
for _, id := range qIDs { for _, id := range qIDs {
id, err := strconv.Atoi(id) id, err := strconv.Atoi(id)
if err != nil { if err != nil {

View file

@ -13,7 +13,7 @@ type User struct {
gorm.Model gorm.Model
Username string Username string
Password string Password string `csv:"-"`
Role string Role string
} }

View file

@ -90,24 +90,24 @@ func importContest(client *client.Client, filename string) error {
return err return err
} }
var contests []*orm.Contest // var contests []*orm.Contest
err := client.ReadAll(&contests) // err := client.ReadAll(&contests)
if err != nil { // if err != nil {
return err // return err
} // }
// Remove all contest with the same name // // Remove all contest with the same name
for _, c := range contests { // for _, c := range contests {
if c.Name == contest.Name { // if c.Name == contest.Name {
log.Println("Remove contest with ID", c.ID) // log.Println("Remove contest with ID", c.ID)
_, err := client.Delete(c) // _, err := client.Delete(c)
if err != nil { // if err != nil {
return err // return err
} // }
} // }
} // }
contestID, err := client.Create(contest) contestID, err := client.Create(contest)
if err != nil { if err != nil {
@ -115,46 +115,45 @@ func importContest(client *client.Client, filename string) error {
} }
log.Println("Create contest with ID", contestID) log.Println("Create contest with ID", contestID)
log.Println("Creating questions...") // log.Println("Creating questions...")
for _, question := range contest.Questions { // for _, question := range contest.Questions {
err := client.DeleteAllFunc(&[]*orm.Question{}, func(model interface{}) bool { // // err := client.DeleteAllFunc(&[]*orm.Question{}, func(model interface{}) bool {
return model.(*orm.Question).Text == question.Text // // return model.(*orm.Question).Text == question.Text && model.(*orm.Question).ContestID == contestID
}) // // })
if err != nil { // // if err != nil {
return err // // return err
} // // }
question.ContestID = contestID // question.ContestID = contestID
questionID, err := client.Create(question) // questionID, err := client.Create(question)
if err != nil { // if err != nil {
return err // return err
} // }
log.Println("Create question with ID", questionID) // log.Println("Create question with ID", questionID)
for pos, answer := range question.Answers { // for pos, answer := range question.Answers {
err := client.DeleteAllFunc(&[]*orm.Answer{}, func(model interface{}) bool { // // err := client.DeleteAllFunc(&[]*orm.Answer{}, func(model interface{}) bool {
return model.(*orm.Answer).Text == answer.Text && model.(*orm.Answer).Question != nil && model.(*orm.Answer).Question.Text == question.Text // // return model.(*orm.Answer).Text == answer.Text && model.(*orm.Answer).Question != nil && model.(*orm.Answer).Question.Text == question.Text
}) // // })
if err != nil { // // if err != nil {
return err // // return err
} // // }
answer.QuestionID = questionID // answer.QuestionID = questionID
if pos == 0 { // if pos == 0 {
answer.Correct = true // answer.Correct = true
} // }
id, err := client.Create(answer) // id, err := client.Create(answer)
if err != nil { // if err != nil {
return err // return err
} // }
log.Println("Create answer with ID", id) // log.Println("Create answer with ID", id)
// }
} // }
}
return nil return nil
} }