Implement Create for Question

This commit is contained in:
Andrea Fazzi 2019-11-13 12:15:29 +01:00
parent d547cf320e
commit fea4b6902b

View file

@ -1,8 +1,10 @@
package orm
import (
"github.com/jinzhu/gorm"
"net/http"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
)
type Question struct {
@ -18,7 +20,20 @@ func (q *Question) String() string {
}
func (q *Question) Create(args map[string]string, r *http.Request) (interface{}, error) {
return nil, nil
if r.Method == "GET" {
return make([]*Question, 0), nil
} else {
question := new(Question)
err := renderer.Decode(question, r)
if err != nil {
return nil, err
}
question, err = CreateQuestion(question)
if err != nil {
return nil, err
}
return question, nil
}
}
func (q *Question) Read(args map[string]string, r *http.Request) (interface{}, error) {
@ -40,3 +55,10 @@ func (q *Question) Update(args map[string]string, r *http.Request) (interface{},
func (q *Question) Delete(args map[string]string, r *http.Request) (interface{}, error) {
return nil, nil
}
func CreateQuestion(question *Question) (*Question, error) {
if err := DB().Create(question).Error; err != nil {
return nil, err
}
return question, nil
}