126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package orm
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
type Question struct {
|
|
gorm.Model
|
|
|
|
Text string
|
|
Contest *Contest
|
|
ContestID uint `schema:"contest_id"`
|
|
|
|
Answers []*Answer
|
|
|
|
SelectedContest map[uint]string `gorm:"-"`
|
|
AllContests []*Contest `gorm:"-"`
|
|
}
|
|
|
|
func (q *Question) GetID() uint { return q.ID }
|
|
|
|
func (q *Question) String() string {
|
|
return q.Text
|
|
}
|
|
|
|
func (q *Question) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
question := new(Question)
|
|
if err := DB().Find(&question.AllContests).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return question, 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, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
var question Question
|
|
|
|
id := args["id"]
|
|
|
|
if err := DB().Preload("Answers", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("answers.correct DESC")
|
|
}).Preload("Contest").First(&question, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &question, nil
|
|
}
|
|
|
|
func (q *Question) ReadAll(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
var questions []*Question
|
|
if err := DB().Preload("Contest").Order("created_at").Find(&questions).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return questions, nil
|
|
}
|
|
|
|
func (q *Question) Update(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
if r.Method == "GET" {
|
|
result, err := q.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
question := result.(*Question)
|
|
|
|
if err := DB().Find(&question.AllContests).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
question.SelectedContest = make(map[uint]string)
|
|
question.SelectedContest[question.ContestID] = "selected"
|
|
|
|
return question, nil
|
|
} else {
|
|
question, err := q.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = renderer.Decode(question, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = SaveQuestion(question)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
question, err = q.Read(args, w, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return question.(*Question), nil
|
|
}
|
|
}
|
|
|
|
func (q *Question) Delete(args map[string]string, w http.ResponseWriter, 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
|
|
}
|
|
|
|
func SaveQuestion(question interface{}) (interface{}, error) {
|
|
if err := DB().Omit("Answers", "Contest").Save(question).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return question, nil
|
|
}
|