Generalize import functions
This commit is contained in:
parent
c875a73b9a
commit
478eaf05b7
1 changed files with 101 additions and 63 deletions
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
|
@ -11,8 +12,106 @@ import (
|
|||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type importer struct {
|
||||
client *client.Client
|
||||
importFuncs map[string]func(*client.Client, string) error
|
||||
}
|
||||
|
||||
func newImporter(c *client.Client) *importer {
|
||||
return &importer{
|
||||
c,
|
||||
map[string]func(*client.Client, string) error{
|
||||
"contest": importContest,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (i *importer) runImport(importer, filename string) error {
|
||||
f, ok := i.importFuncs[importer]
|
||||
if !ok {
|
||||
return fmt.Errorf("Importer %s not defined!", importer)
|
||||
}
|
||||
return f(i.client, filename)
|
||||
}
|
||||
|
||||
func importContest(client *client.Client, filename string) error {
|
||||
log.Println("Reading toml file...")
|
||||
var contest *orm.Contest
|
||||
if _, err := toml.DecodeFile(filename, &contest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var contests []*orm.Contest
|
||||
err := client.ReadAll(&contests)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
contestID, err := client.Create(contest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Create contest with ID", contestID)
|
||||
|
||||
log.Println("Creating questions...")
|
||||
|
||||
for _, question := range contest.Questions {
|
||||
|
||||
err := client.DeleteAllByName(&[]*orm.Question{}, question.Text)
|
||||
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)
|
||||
|
||||
for pos, answer := range question.Answers {
|
||||
|
||||
err := client.DeleteAllByName(&[]*orm.Answer{}, answer.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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var importer string
|
||||
|
||||
flag.StringVar(&importer, "importer", "contest", "Name of importer to use")
|
||||
flag.Parse()
|
||||
|
||||
if flag.NArg() == 0 {
|
||||
panic("A toml filename is needed as first argument of this script.")
|
||||
}
|
||||
|
@ -29,71 +128,10 @@ func main() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Reading toml file...")
|
||||
var contest *orm.Contest
|
||||
if _, err := toml.DecodeFile(flag.Arg(0), &contest); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var contests []*orm.Contest
|
||||
err = client.ReadAll(&contests)
|
||||
i := newImporter(client)
|
||||
err = i.runImport(importer, flag.Arg(0))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
contestID, err := client.Create(contest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Create contest with ID", contestID)
|
||||
|
||||
log.Println("Creating questions...")
|
||||
|
||||
for _, question := range contest.Questions {
|
||||
|
||||
err := client.DeleteAllByName(&[]*orm.Question{}, question.Text)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
question.ContestID = contestID
|
||||
questionID, err := client.Create(question)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Create question with ID", questionID)
|
||||
|
||||
for pos, answer := range question.Answers {
|
||||
|
||||
err := client.DeleteAllByName(&[]*orm.Answer{}, answer.Text)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
answer.QuestionID = questionID
|
||||
if pos == 0 {
|
||||
answer.Correct = true
|
||||
}
|
||||
id, err := client.Create(answer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Create answer with ID", id)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue