2019-11-04 15:00:46 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-11-13 12:15:29 +01:00
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
2019-11-04 15:00:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Question struct {
|
|
|
|
gorm.Model
|
|
|
|
|
2019-11-14 12:55:22 +01:00
|
|
|
Text string
|
|
|
|
Contest *Contest
|
|
|
|
ContestID uint `schema:"contest_id"`
|
2019-11-13 15:54:17 +01:00
|
|
|
|
|
|
|
Answers []*Answer
|
2019-11-14 12:55:22 +01:00
|
|
|
|
|
|
|
SelectedContest map[uint]string `gorm:"-"`
|
|
|
|
AllContests []*Contest `gorm:"-"`
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Question) GetID() uint { return q.ID }
|
|
|
|
|
|
|
|
func (q *Question) String() string {
|
|
|
|
return q.Text
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:03:56 +01:00
|
|
|
func (q *Question) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
if len(q.Answers) > 0 {
|
|
|
|
q.Answers[0].Correct = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (q *Question) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 12:15:29 +01:00
|
|
|
if r.Method == "GET" {
|
2019-11-14 12:55:22 +01:00
|
|
|
question := new(Question)
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&question.AllContests).Error; err != nil {
|
2019-11-14 12:55:22 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question, nil
|
2019-11-13 12:15:29 +01:00
|
|
|
} else {
|
|
|
|
question := new(Question)
|
|
|
|
err := renderer.Decode(question, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
question, err = CreateQuestion(db, question)
|
2019-11-13 12:15:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question, nil
|
|
|
|
}
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (q *Question) Read(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-13 15:54:17 +01:00
|
|
|
var question Question
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("Answers", func(db *gorm.DB) *gorm.DB {
|
2019-12-13 12:55:11 +01:00
|
|
|
return db.Order("answers.correct DESC")
|
|
|
|
}).Preload("Contest").First(&question, id).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &question, nil
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (q *Question) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
2019-11-04 15:00:46 +01:00
|
|
|
var questions []*Question
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Preload("Contest").Order("created_at").Find(&questions).Error; err != nil {
|
2019-11-04 15:00:46 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return questions, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (q *Question) 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 := q.Read(db, args, w, r)
|
2019-11-14 12:55:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
question := result.(*Question)
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
if err := db._db.Find(&question.AllContests).Error; err != nil {
|
2019-11-14 12:55:22 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
question.SelectedContest = make(map[uint]string)
|
|
|
|
question.SelectedContest[question.ContestID] = "selected"
|
|
|
|
|
|
|
|
return question, nil
|
2019-11-13 15:54:17 +01:00
|
|
|
} else {
|
2020-01-15 11:27:00 +01:00
|
|
|
question, err := q.Read(db, args, w, r)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(question, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
_, err = SaveQuestion(db, question)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 11:27:00 +01:00
|
|
|
question, err = q.Read(db, args, w, r)
|
2019-11-13 15:54:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question.(*Question), nil
|
|
|
|
}
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func (model *Question) Delete(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|
|
|
question, 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(question.(*Question)).Error; err != nil {
|
2019-12-19 12:12:56 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question.(*Question), nil
|
2019-11-04 15:00:46 +01:00
|
|
|
}
|
2019-11-13 12:15:29 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func CreateQuestion(db *Database, question *Question) (*Question, error) {
|
|
|
|
if err := db._db.Create(question).Error; err != nil {
|
2019-11-13 12:15:29 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question, nil
|
|
|
|
}
|
2019-11-13 15:54:17 +01:00
|
|
|
|
2020-01-15 11:27:00 +01:00
|
|
|
func SaveQuestion(db *Database, question interface{}) (interface{}, error) {
|
|
|
|
if err := db._db.Omit("Answers", "Contest").Save(question).Error; err != nil {
|
2019-11-13 15:54:17 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return question, nil
|
|
|
|
}
|