2019-11-13 11:15:21 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-11-13 15:54:17 +01:00
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
2019-11-13 11:15:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Answer struct {
|
|
|
|
gorm.Model
|
|
|
|
|
|
|
|
Text string
|
|
|
|
Correct bool
|
|
|
|
|
2019-11-13 15:54:17 +01:00
|
|
|
Question *Question
|
|
|
|
QuestionID uint `schema:"question_id"`
|
|
|
|
|
|
|
|
AllQuestions []*Question `gorm:"-"`
|
|
|
|
Selected map[uint]string `gorm:"-"`
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-11-13 15:54:17 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
answer := new(Answer)
|
|
|
|
if err := DB().Find(&answer.AllQuestions).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer, nil
|
|
|
|
} else {
|
|
|
|
answer := new(Answer)
|
|
|
|
err := renderer.Decode(answer, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
answer, err = CreateAnswer(answer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Answer) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
var answer Answer
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
|
|
|
if err := DB().Preload("Question").First(&answer, id).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &answer, nil
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Answer) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
2019-11-13 13:57:41 +01:00
|
|
|
var answers []*Answer
|
|
|
|
if err := DB().Order("created_at").Find(&answers).Error; err != nil {
|
2019-11-13 11:15:21 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-13 13:57:41 +01:00
|
|
|
return answers, nil
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Answer) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
result, err := a.Read(args, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer := result.(*Answer)
|
|
|
|
|
|
|
|
if err := DB().Find(&answer.AllQuestions).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer.Selected = make(map[uint]string)
|
|
|
|
answer.Selected[answer.QuestionID] = "selected"
|
|
|
|
|
|
|
|
return answer, nil
|
|
|
|
} else {
|
|
|
|
answer, err := a.Read(args, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(answer, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = SaveAnswer(answer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
answer, err = a.Read(args, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer.(*Answer), nil
|
|
|
|
}
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Answer) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-11-13 15:54:17 +01:00
|
|
|
|
|
|
|
func CreateAnswer(answer *Answer) (*Answer, error) {
|
|
|
|
if err := DB().Create(answer).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveAnswer(answer interface{}) (interface{}, error) {
|
|
|
|
if err := DB().Omit("Answers").Save(answer).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|