2019-11-18 17:04:07 +01:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
gorm.Model
|
|
|
|
|
2019-11-22 11:16:27 +01:00
|
|
|
Name string
|
|
|
|
|
|
|
|
Participant *Participant
|
|
|
|
ParticipantID uint
|
|
|
|
|
|
|
|
Contest *Contest
|
|
|
|
ContestID uint
|
|
|
|
|
2019-11-18 17:04:07 +01:00
|
|
|
Questions []*Question
|
|
|
|
|
|
|
|
// SelectedElement map[uint]string `gorm:"-"`
|
|
|
|
// AllElements []*Element `gorm:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) GetID() uint { return model.ID }
|
|
|
|
|
|
|
|
func (model *Response) String() string {
|
2019-11-22 11:16:27 +01:00
|
|
|
return model.Name
|
2019-11-18 17:04:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) Create(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
response := new(Response)
|
|
|
|
contestID := r.URL.Query().Get("contest_id")
|
|
|
|
|
|
|
|
if err := DB().Preload("Answers").Where("contest_id = ?", contestID).Find(&response.Questions).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
} else {
|
|
|
|
response := new(Response)
|
|
|
|
err := renderer.Decode(response, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response, err = CreateResponse(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) Read(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
var response Response
|
|
|
|
|
|
|
|
id := args["id"]
|
|
|
|
|
2019-11-22 11:16:27 +01:00
|
|
|
if err := DB().Preload("Contest").Preload("Participant").First(&response, id).Error; err != nil {
|
2019-11-18 17:04:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
var responses []*Response
|
2019-11-22 11:16:27 +01:00
|
|
|
if err := DB().Preload("Contest", "Participant").Order("created_at").Find(&responses).Error; err != nil {
|
2019-11-18 17:04:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return responses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) Update(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
result, err := model.Read(args, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := result.(*Response)
|
|
|
|
|
|
|
|
// if err := DB().Find(&response.AllElements).Error; err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// response.SelectedElement = make(map[uint]string)
|
|
|
|
// response.SelectedElement[response.ElementID] = "selected"
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
} else {
|
|
|
|
response, err := model.Read(args, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = renderer.Decode(response, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = SaveResponse(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response, err = model.Read(args, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response.(*Response), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (model *Response) Delete(args map[string]string, r *http.Request) (interface{}, error) {
|
|
|
|
response, err := model.Read(args, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := DB().Unscoped().Delete(response.(*Response)).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response.(*Response), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateResponse(response *Response) (*Response, error) {
|
|
|
|
if err := DB().Create(response).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveResponse(response interface{}) (interface{}, error) {
|
|
|
|
if err := DB(). /*.Omit("Something")*/ Save(response).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|