diff --git a/handlers/login.go b/handlers/login.go index 183cde31..476f3c6e 100644 --- a/handlers/login.go +++ b/handlers/login.go @@ -47,7 +47,7 @@ func loginHandler() http.Handler { session.Values["token"] = token session.Save(r, w) r.Method = "GET" - http.Redirect(w, r, "/teachers?format=html&tpl_layout=base&tpl_content=teachers", http.StatusSeeOther) + http.Redirect(w, r, "/questions?format=html&tpl_layout=base&tpl_content=questions", http.StatusSeeOther) } } } diff --git a/main.go b/main.go index aee9b586..6103d0ca 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ var ( models = []interface{}{ &orm.Question{}, + &orm.Answer{}, } ) diff --git a/orm/answer.go b/orm/answer.go new file mode 100644 index 00000000..43b9a55c --- /dev/null +++ b/orm/answer.go @@ -0,0 +1,45 @@ +package orm + +import ( + "github.com/jinzhu/gorm" + "net/http" +) + +type Answer struct { + gorm.Model + + Text string + Correct bool + + QuestionID uint +} + +func (a *Answer) GetID() uint { return a.ID } + +func (a *Answer) String() string { + return a.Text +} + +func (a *Answer) Create(args map[string]string, r *http.Request) (interface{}, error) { + return nil, nil +} + +func (a *Answer) Read(args map[string]string, r *http.Request) (interface{}, error) { + return nil, nil +} + +func (a *Answer) ReadAll(args map[string]string, r *http.Request) (interface{}, error) { + var questions []*Question + if err := DB().Order("created_at").Find(&questions).Error; err != nil { + return nil, err + } + return questions, nil +} + +func (a *Answer) Update(args map[string]string, r *http.Request) (interface{}, error) { + return nil, nil +} + +func (a *Answer) Delete(args map[string]string, r *http.Request) (interface{}, error) { + return nil, nil +} diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 1a3a267f..9afa9570 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "html/template" - "log" "net/url" "reflect" "strings" @@ -44,9 +43,13 @@ var ( ) func modelName(value interface{}) string { - if t := reflect.TypeOf(value); t.Kind() == reflect.Ptr { + t := reflect.TypeOf(value) + switch t.Kind() { + case reflect.Ptr: return t.Elem().Name() - } else { + case reflect.Slice: + return strings.Replace(t.Elem().String(), "*orm.", "", -1) + default: return t.Name() } } @@ -60,7 +63,6 @@ func pluralize(text string) string { } func active(value string, model interface{}) string { - log.Println(pluralize(lower(modelName(model)))) if value == pluralize(lower(modelName(model))) { return "active" } diff --git a/templates/layout/base.html.tpl b/templates/layout/base.html.tpl index f32d24cf..0756c693 100644 --- a/templates/layout/base.html.tpl +++ b/templates/layout/base.html.tpl @@ -12,14 +12,18 @@