From c19631e65b33ba2d14c635819d37ffc38e7d245c Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 18 Dec 2019 09:47:37 +0100 Subject: [PATCH] Encode/decode single responses --- orm/response.go | 65 ++++++++++++++++++++----- templates/responses_add_update.html.tpl | 4 +- templates/responses_show.html.tpl | 8 +++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/orm/response.go b/orm/response.go index 97adbaca..6e0cad05 100644 --- a/orm/response.go +++ b/orm/response.go @@ -2,11 +2,17 @@ package orm import ( "net/http" + "strconv" + "strings" "git.andreafazzi.eu/andrea/oef/renderer" "github.com/jinzhu/gorm" ) +type SingleResponse struct { + AnswerID uint +} + type Response struct { gorm.Model @@ -22,19 +28,42 @@ type Response struct { ContestID uint QuestionsOrder string + AnswersIDs string - Questions []*Question + Score int `gorm:"-"` - // SelectedElement map[uint]string `gorm:"-"` - // AllElements []*Element `gorm:"-"` + Questions []*Question + SingleResponses []*SingleResponse `gorm:"-"` } +func (sr *SingleResponse) UnmarshalText(text []byte) error { + id, err := strconv.Atoi(string(text)) + sr.AnswerID = uint(id) + if err != nil { + return err + } + return nil + +} func (model *Response) GetID() uint { return model.ID } func (model *Response) String() string { return model.Name } +func (model *Response) generateAnswersIDs() string { + ids := make([]string, 0) + for _, sr := range model.SingleResponses { + ids = append(ids, strconv.Itoa(int(sr.AnswerID))) + } + return strings.Join(ids, " ") +} + +func (model *Response) BeforeSave(tx *gorm.DB) error { + model.AnswersIDs = model.generateAnswersIDs() + return nil +} + func (model *Response) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { if r.Method == "GET" { response := new(Response) @@ -71,8 +100,29 @@ func (model *Response) Read(args map[string]string, w http.ResponseWriter, r *ht return nil, err } + srIDs := strings.Split(response.AnswersIDs, " ") + for _, srID := range srIDs { + id, err := strconv.Atoi(srID) + if err != nil { + return nil, err + } + response.SingleResponses = append(response.SingleResponses, &SingleResponse{uint(id)}) + } + + for _, sr := range response.SingleResponses { + var answer Answer + + if err := DB().First(&answer, sr.AnswerID).Error; err != nil { + return nil, err + } + + if answer.Correct { + response.Score++ + } + } + // Fetch questions in the given order - if err := DB().Where("contest_id = ?", response.Contest.ID).Find(&response.Questions).Error; err != nil { + if err := DB().Where("contest_id = ?", response.Contest.ID).Preload("Answers").Find(&response.Questions).Error; err != nil { return nil, err } @@ -96,13 +146,6 @@ func (model *Response) Update(args map[string]string, w http.ResponseWriter, r * 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, w, r) diff --git a/templates/responses_add_update.html.tpl b/templates/responses_add_update.html.tpl index b4c54edd..23d543d4 100644 --- a/templates/responses_add_update.html.tpl +++ b/templates/responses_add_update.html.tpl @@ -23,12 +23,12 @@ role="form" id={{$form}}> - {{range $question := .Data.Questions}} + {{range $id,$question := .Data.Questions}}

{{$question.Text}}

{{range $answer := $question.Answers}}
- + diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl index 02075011..5e3c5d3b 100644 --- a/templates/responses_show.html.tpl +++ b/templates/responses_show.html.tpl @@ -11,6 +11,14 @@

Informazioni generali

{{if $isAdmin}} +
Risposte fornite (IDs)
+ {{if .Data.AnswersIDs}} +
{{printf "[%v]" .Data.AnswersIDs}}
+ {{else}} +
nessuna risposta fornita
+ {{end}} +
Punteggio
+
{{.Data.Score}}
{{if $creatorUser:=.Data.CreatedBy}}
Creato da
{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}} da {{.Data.CreatorIP}}