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
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (a *Answer) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
if r.Method == "GET" {
|
|
|
|
answer := new(Answer)
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&answer.AllQuestions).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer, nil
|
|
|
|
} else {
|
|
|
|
answer := new(Answer)
|
|
|
|
err := renderer.Decode(answer, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
answer, err = CreateAnswer(db, answer)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (a *Answer) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
var answer Answer
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("Question").First(&answer, id).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &answer, nil
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (a *Answer) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 13:57:41 +01:00
|
|
|
var answers []*Answer
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("Question").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
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (a *Answer) Update(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
if r.Method == "GET" {
|
2020-01-15 11:27:00 +01:00
|
|
|
result, err := a.Read(db, args, w, r)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer := result.(*Answer)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&answer.AllQuestions).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer.Selected = make(map[uint]string)
|
|
|
|
answer.Selected[answer.QuestionID] = "selected"
|
|
|
|
|
|
|
|
return answer, nil
|
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
answer, err := a.Read(db, args, w, r)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-14 12:55:22 +01:00
|
|
|
|
|
|
|
// FIXME: Should not be hard set.
|
|
|
|
answer.(*Answer).Correct = false
|
|
|
|
|
2019-11-13 15:54:17 +01:00
|
|
|
err = renderer.Decode(answer, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveAnswer(db, answer)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
answer, err = a.Read(db, args, w, r)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer.(*Answer), nil
|
|
|
|
}
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Answer) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
|
|
answer, err := model.Read(db, args, w, r)
|
2019-12-19 12:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Unscoped().Delete(answer.(*Answer)).Error; err != nil {
|
2019-12-19 12:12:56 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer.(*Answer), nil
|
2019-11-13 11:15:21 +01:00
|
|
|
}
|
2019-11-13 15:54:17 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateAnswer(db *Database, answer *Answer) (*Answer, error) {
|
|
|
|
if err := db._db.Create(answer).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveAnswer(db *Database, answer interface{}) (interface{}, error) {
|
|
|
|
if err := db._db.Omit("Answers").Save(answer).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return answer, nil
|
|
|
|
}
|