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

View file

@ -26,6 +26,13 @@ func (q *Question) String() string {
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) {
if r.Method == "GET" {
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)
qIDs := strings.Split(response.QuestionsOrder, " ")
log.Print("QO", response.QuestionsOrder)
for _, id := range qIDs {
id, err := strconv.Atoi(id)
if err != nil {

View file

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

View file

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