diff --git a/client/client.go b/client/client.go index 60b96325..18046bbd 100644 --- a/client/client.go +++ b/client/client.go @@ -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))) +} diff --git a/orm/orm.go b/orm/orm.go index 5df35436..00b99253 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -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() } } diff --git a/scripts/import_contest/.gitignore b/scripts/import_contest/.gitignore new file mode 100644 index 00000000..51f895ee --- /dev/null +++ b/scripts/import_contest/.gitignore @@ -0,0 +1,2 @@ +import_contest +contests diff --git a/scripts/import_contest/import_contest b/scripts/import_contest/import_contest deleted file mode 100755 index 6dde1a0c..00000000 Binary files a/scripts/import_contest/import_contest and /dev/null differ diff --git a/scripts/import_contest/main.go b/scripts/import_contest/main.go index cddcaa6f..3ac78838 100644 --- a/scripts/import_contest/main.go +++ b/scripts/import_contest/main.go @@ -18,7 +18,7 @@ func main() { } var contests []*orm.Contest - err = client.Contests(&contests) + err = client.GetAll(&contests) if err != nil { panic(err) }