Working on contest toml import

This commit is contained in:
Andrea Fazzi 2020-01-07 18:33:11 +01:00
parent a590a57b65
commit 766b54322a
5 changed files with 21 additions and 29 deletions

View file

@ -6,12 +6,13 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"git.andreafazzi.eu/andrea/oef/orm"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/inflection"
)
// A client represents a client connection to the Headmaster test
@ -115,33 +116,10 @@ func (c *Client) getModel(name string, model interface{}) (interface{}, error) {
return model, nil
}
func (c *Client) Contests(contests *[]*orm.Contest) error {
var response renderer.JsonResponse
data, err := c.SendRequest("GET", "/api/contests?format=json", nil)
if err != nil {
return err
}
if err := json.Unmarshal(data, &response); err != nil {
return err
}
if string(response.Error) != "" {
return errors.New(string(response.Error))
}
if err := json.Unmarshal(response.Result, &contests); err != nil {
return err
}
return nil
}
func (c *Client) GetAll(model interface{}) error {
var response renderer.JsonResponse
log.Println(orm.ModelName(model))
data, err := c.SendRequest("GET", "/api/participants?format=json", nil)
data, err := c.SendRequest("GET", fmt.Sprintf("/api/%s?format=json", pluralizedModelName(model)), nil)
if err != nil {
return err
}
@ -159,3 +137,7 @@ func (c *Client) GetAll(model interface{}) error {
}
return nil
}
func pluralizedModelName(value interface{}) string {
return inflection.Plural(strings.ToLower(orm.ModelName(value)))
}

View file

@ -139,9 +139,17 @@ func PostNothing(args map[string]string, w http.ResponseWriter, r *http.Request)
}
func ModelName(s interface{}) string {
if t := reflect.TypeOf(s); t.Kind() == reflect.Ptr {
return t.Elem().Name()
} else {
t := reflect.TypeOf(s)
switch t.Kind() {
case reflect.Ptr:
elem := t.Elem()
if strings.Contains(elem.String(), "[]") {
return strings.Replace(elem.String(), "[]*orm.", "", -1)
}
return elem.Name()
case reflect.Slice:
return strings.Replace(t.Elem().String(), "*orm.", "", -1)
default:
return t.Name()
}
}

2
scripts/import_contest/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
import_contest
contests

View file

@ -18,7 +18,7 @@ func main() {
}
var contests []*orm.Contest
err = client.Contests(&contests)
err = client.GetAll(&contests)
if err != nil {
panic(err)
}