From 2aa98c1bbbd478184e7fe7879939297606595884 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 13 Dec 2019 08:32:20 +0100 Subject: [PATCH] Add NumQuestions and NumAnswersPerQuestion --- Makefile | 2 +- orm/contest.go | 35 ++++++++++++++++---------- orm/participant.go | 5 ++-- orm/school.go | 6 +++++ orm/useraction.go | 2 -- renderer/funcmap.go | 5 ++++ renderer/renderer.go | 1 + templates/contests_add_update.html.tpl | 17 ++++++++++--- templates/contests_show.html.tpl | 19 +++++++++++++- 9 files changed, 69 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 0d5d9783..5df5221b 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,6 @@ dev: docker-compose -f compose/docker-compose_outside_docker.yml down docker-compose -f compose/docker-compose_outside_docker.yml up -d db docker-compose -f compose/docker-compose_outside_docker.yml up -d smtp - go run main.go --config=config/config_outside_docker.yaml & + go run -race main.go --config=config/config_outside_docker.yaml & all: dockerized diff --git a/orm/contest.go b/orm/contest.go index 366da332..aa2c7e4e 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -2,6 +2,7 @@ package orm import ( "fmt" + "log" "net/http" "time" @@ -21,6 +22,9 @@ type Contest struct { StartTime time.Time EndTime time.Time + NumQuestions int + NumAnswersPerQuestion int + Questions []*Question Participants []*Participant `gorm:"many2many:subscriptions"` } @@ -45,10 +49,16 @@ func (c *Contest) Create(args map[string]string, w http.ResponseWriter, r *http. date := r.FormValue("Date") startTime := r.FormValue("StartTime") endTime := r.FormValue("EndTime") - - r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) - r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) - r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime)) + log.Println("Zero time", date) + if date == "" { + r.PostForm.Set("Date", time.Time{}.String()) + r.PostForm.Set("StartTime", time.Time{}.String()) + r.PostForm.Set("EndTime", time.Time{}.String()) + } else { + r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) + r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) + r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime)) + } err = renderer.Decode(contest, r) if err != nil { @@ -120,17 +130,16 @@ func (c *Contest) Update(args map[string]string, w http.ResponseWriter, r *http. startTime := r.FormValue("StartTime") endTime := r.FormValue("EndTime") - r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) - r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) - r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime)) - - err = r.ParseForm() - if err != nil { - return nil, err + if date == "" { + r.PostForm.Set("Date", time.Time{}.Format(time.RFC3339)) + r.PostForm.Set("StartTime", time.Time{}.Format(time.RFC3339)) + r.PostForm.Set("EndTime", time.Time{}.Format(time.RFC3339)) + } else { + r.PostForm.Set("Date", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) + r.PostForm.Set("StartTime", fmt.Sprintf("%sT%s:00+00:00", date, startTime)) + r.PostForm.Set("EndTime", fmt.Sprintf("%sT%s:00+00:00", date, endTime)) } - r.Form["Date"][0] = fmt.Sprintf("%sT%s+00:00", date, startTime) - err = renderer.Decode(contest, r) if err != nil { return nil, err diff --git a/orm/participant.go b/orm/participant.go index 43174553..c59da3b4 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -2,9 +2,8 @@ package orm import ( "fmt" - "strconv" - "net/http" + "strconv" "strings" "git.andreafazzi.eu/andrea/oef/config" @@ -108,7 +107,7 @@ func (model *Participant) AfterSave(tx *gorm.DB) error { &Response{ Name: fmt.Sprintf("%s (%s)", contest.Name, model.String()), ContestID: contest.ID, - ParticipantID: model.UserID, + ParticipantID: model.ID, }).Error; err != nil { return err } diff --git a/orm/school.go b/orm/school.go index 496c283e..a97cb777 100644 --- a/orm/school.go +++ b/orm/school.go @@ -24,6 +24,12 @@ type School struct { Code string EmailSentDate time.Time + SchoolResponsibleLastname string + SchoolResponsibleFirstname string + + ContestResponsibleLastname string + ContestResponsibleFirstname string + UserID uint RegionID uint `schema:"region_id"` diff --git a/orm/useraction.go b/orm/useraction.go index 4d1e3faa..c0fb0ad6 100644 --- a/orm/useraction.go +++ b/orm/useraction.go @@ -1,7 +1,6 @@ package orm import ( - "log" "net/http" "github.com/dgrijalva/jwt-go" @@ -74,7 +73,6 @@ func NewUserModifierUpdate(r *http.Request) *UserModifierUpdate { if r.Context().Value("user") != nil { claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims) } - log.Println("UPDATE Inside useraction", claims["user_id"]) return &UserModifierUpdate{ UpdaterID: claims["user_id"].(string), UpdaterRole: claims["role"].(string), diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 99876f6f..fd7e22c6 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -28,6 +28,7 @@ var ( "prettyDate": prettyDate, "prettyTime": prettyTime, "prettyDateTime": prettyDateTime, + "zeroTime": zeroTime, "modelPath": modelPath, "dict": dict, "yaml": yaml, @@ -270,6 +271,10 @@ func prettyTime(value interface{}) string { return convertTime(value) } +func zeroTime(t *time.Time) bool { + return *t == time.Time{} +} + func modelPath(model string, action string, id uint) string { var q template.URL diff --git a/renderer/renderer.go b/renderer/renderer.go index 105f970d..24398b3c 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -323,6 +323,7 @@ func Decode(dst interface{}, r *http.Request) error { case "html": var timeConverter = func(value string) reflect.Value { + log.Println(value) if value == "" { return reflect.ValueOf(time.Time{}) } diff --git a/templates/contests_add_update.html.tpl b/templates/contests_add_update.html.tpl index 9edbba04..3539a708 100644 --- a/templates/contests_add_update.html.tpl +++ b/templates/contests_add_update.html.tpl @@ -5,12 +5,12 @@ {{if $update}} -{{template "breadcrumb" toSlice "Contests" (all "Contest") (.Data|string) (.Data.ID|show "Contest") "Aggiorna" "current"}} +{{template "breadcrumb" toSlice "Gare" (all "Contest") (.Data|string) (.Data.ID|show "Contest") "Aggiorna" "current"}} {{else}} -{{template "breadcrumb" toSlice "Contests" (all "Contest") "Aggiungi" "current"}} +{{template "breadcrumb" toSlice "Gare" (all "Contest") "Aggiungi" "current"}} {{end}} -{{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo ELEMENTO" "updateTitle" (printf "Aggiorna gara %s" (.Data|string))}} +{{template "add_update_header" dict "update" $update "addTitle" "Crea nuova gara" "updateTitle" (printf "Aggiorna gara %s" (.Data|string))}} {{$form := "form_add_update"}}
+
+
+ {{$options := ` { name: "NumQuestions",id: "contest_num_questions",label: "Numero totale delle domande presenti nella gara",placeholder: "Inserire il numero delle domande",type: "number",required: "true"} `}} + {{template "input" dict "options" ($options|yaml) "value" (.Data|field "NumQuestions") "update" $update}} +
+
+ {{$options := ` { name: "NumAnswersPerQuestion",id: "contest_num_answers_per_question",label: "Numero delle risposte per domanda",placeholder: "Inserire il numero delle risposte per domanda",type: "number",required: "true"} `}} + {{template "input" dict "options" ($options|yaml) "value" (.Data|field "NumAnswersPerQuestion") "update" $update}} +
+
+ {{$options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Contest" }`}} {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}} diff --git a/templates/contests_show.html.tpl b/templates/contests_show.html.tpl index 7a0a8ee8..3921f4dc 100644 --- a/templates/contests_show.html.tpl +++ b/templates/contests_show.html.tpl @@ -6,7 +6,24 @@ {{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Contest") "deletePath" (.Data.ID|delete "Contest")}}

Informazioni generali sulla gara

-

La gara si svolgerà il giorno {{.Data.Date|prettyDate}} dalle ore {{.Data.StartTime|convertTime}} alle ore {{.Data.EndTime|convertTime}}.

+ + +
+
Periodo di attività
+
+ {{if not (.Data.Date|zeroTime)}} + La gara si svolgerà il giorno {{.Data.Date|prettyDate}} dalle ore {{.Data.StartTime|convertTime}} alle ore {{.Data.EndTime|convertTime}} + {{else}} + La gara è sempre attiva + {{end}} +
+
Numero di domande
+
{{.Data.NumQuestions}}
+
Numero di risposte per domanda
+
{{.Data.NumAnswersPerQuestion}}
+ +
+