oef/orm/question.go
2019-11-13 12:15:29 +01:00

64 lines
1.3 KiB
Go

package orm
import (
"net/http"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
)
type Question struct {
gorm.Model
Text string
}
func (q *Question) GetID() uint { return q.ID }
func (q *Question) String() string {
return q.Text
}
func (q *Question) Create(args map[string]string, r *http.Request) (interface{}, error) {
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) {
return nil, nil
}
func (q *Question) 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 (q *Question) Update(args map[string]string, r *http.Request) (interface{}, error) {
return nil, nil
}
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
}