From f7d0fa4b397678771e166d5eb6fc76c40b2cc2b3 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 28 Jan 2020 15:04:28 +0100 Subject: [PATCH] Use new error template for errors --- VERSION | 2 +- orm/participant.go | 46 ++++++++++++++----- orm/school.go | 6 ++- renderer/funcmap.go | 5 ++ templates/error_category_exists.html.tpl | 13 ++---- .../error_contest_has_zero_questions.html.tpl | 4 +- templates/error_out_of_time.html.tpl | 13 ++---- templates/error_participant_exists.html.tpl | 13 ++---- templates/error_school_exists.html.tpl | 13 ++---- templates/layout/error.html.tpl | 2 +- 10 files changed, 58 insertions(+), 59 deletions(-) diff --git a/VERSION b/VERSION index cbdc31f4..98d11b3c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0-6-gb421739 +0.1.0-9-g5616fce-master diff --git a/orm/participant.go b/orm/participant.go index c72d6978..eda653e0 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -268,16 +268,29 @@ func (model *Participant) Read(db *Database, args map[string]string, w http.Resp } if strconv.Itoa(int(participant.SchoolID)) != getUserIDFromToken(r) { - // setFlashMessage(w, r, "notAuthorized") return nil, errors.NotAuthorized } - if err := db._db.Preload("User").Preload("School").Preload("School.Participants").Preload("Category").First(&participant, id).Error; err != nil { + if err := db._db. + Preload("User"). + Preload("School"). + Preload("School.Participants"). + Preload("Category"). + First(&participant, id).Error; err != nil { return nil, err } } else { - if err := db._db.Preload("User").Preload("School").Preload("School.Participants").Preload("Responses").Preload("Contests").Preload("Category").First(&participant, id).Error; err != nil { + if err := db._db. + Preload("User"). + Preload("Creator"). + Preload("Updater"). + Preload("School"). + Preload("School.Participants"). + Preload("Responses"). + Preload("Contests"). + Preload("Category"). + First(&participant, id).Error; err != nil { return nil, err } @@ -295,7 +308,12 @@ func (model *Participant) ReadAll(db *Database, args map[string]string, w http.R if err != nil { return nil, err } - if err := db._db.Preload("Category").Preload("School").Preload("Contests").Order("lastname").Find(&participants, &Participant{SchoolID: uint(schoolId)}).Error; err != nil { + if err := db._db. + Preload("Category"). + Preload("School"). + Preload("Contests"). + Order("lastname"). + Find(&participants, &Participant{SchoolID: uint(schoolId)}).Error; err != nil { return nil, err } } else { @@ -359,11 +377,7 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re if user, err := participant.(*Participant).exists(db); err == nil && user != nil { if user.ID != participant.(*Participant).UserID { - // err := setFlashMessage(w, r, "participantExists") - // if err != nil { - // return nil, err - // } - return participant, nil + return nil, errors.ParticipantExists } } else if err != nil { return nil, err @@ -382,7 +396,9 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re return nil, errors.CategoryExists } - if err := db._db.Where([]uint(participant.(*Participant).ContestIDs)).Find(&participant.(*Participant).Contests).Error; err != nil { + if err := db._db. + Where([]uint(participant.(*Participant).ContestIDs)). + Find(&participant.(*Participant).Contests).Error; err != nil { return nil, err } @@ -393,7 +409,10 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re return nil, err } - if err := db._db.Model(participant).Association("Contests").Replace(participant.(*Participant).Contests).Error; err != nil { + if err := db._db. + Model(participant). + Association("Contests"). + Replace(participant.(*Participant).Contests).Error; err != nil { return nil, err } @@ -406,7 +425,10 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re if isAdministrator(r) { var response Response - if err := db._db.First(&response, &Response{ParticipantID: participant.(*Participant).ID}).Error; err != nil { + if err := db._db.First( + &response, + &Response{ParticipantID: participant.(*Participant).ID}, + ).Error; err != nil { return nil, err } diff --git a/orm/school.go b/orm/school.go index 281a4df9..d7d5fb56 100644 --- a/orm/school.go +++ b/orm/school.go @@ -228,7 +228,7 @@ func (model *School) Read(db *Database, args map[string]string, w http.ResponseW func (model *School) ReadAll(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { var schools []*School if err := db._db. - Preload("Participant"). + Preload("Participants"). Preload("Participants.Category"). Order("code"). Find(&schools).Error; err != nil { @@ -315,7 +315,9 @@ func SaveSchool(db *Database, school interface{}) (interface{}, error) { func (model *School) HasCategory(db *Database, participant *Participant) (bool, error) { var participants []*Participant - if err := db._db.Where("category_id = ? AND school_id = ? AND id <> ?", participant.CategoryID, model.ID, participant.ID).Find(&participants).Error; err != nil { + if err := db._db. + Where("category_id = ? AND school_id = ? AND id <> ?", participant.CategoryID, model.ID, participant.ID). + Find(&participants).Error; err != nil { return false, err } return len(participants) > 0, nil diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 2549d4ed..3d9bec3d 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -50,6 +50,7 @@ var ( "mod2": mod2, "toLower": toLower, "anchor": anchor, + "alertLink": alertLink, "html": html, "field": field, "modelName": modelName, @@ -185,6 +186,10 @@ func anchor(text, url string) template.HTML { return template.HTML(fmt.Sprintf("%s", url, text)) } +func alertLink(text, url string) template.HTML { + return template.HTML(fmt.Sprintf("%s", url, text)) +} + func toLower(text string) string { return strings.ToLower(text) } diff --git a/templates/error_category_exists.html.tpl b/templates/error_category_exists.html.tpl index c42d49dd..cc8d724c 100644 --- a/templates/error_category_exists.html.tpl +++ b/templates/error_category_exists.html.tpl @@ -1,11 +1,4 @@ {{ define "content" }} -
-

Errore

-

- Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: {{.Data}} -

-

- Clicca {{.Referer|anchor "qui"}} per tornare al modulo di creazione del partecipante. -

-
-{{ end }} +{{$options := `title: "Errore nella creazione/aggiornamento di un partecipante"`}} +{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{end}} diff --git a/templates/error_contest_has_zero_questions.html.tpl b/templates/error_contest_has_zero_questions.html.tpl index 91f14da0..cc8d724c 100644 --- a/templates/error_contest_has_zero_questions.html.tpl +++ b/templates/error_contest_has_zero_questions.html.tpl @@ -1,6 +1,4 @@ {{ define "content" }} -{{$options := ` -title: "Errore nella creazione/aggiornamento di un partecipante" -`}} +{{$options := `title: "Errore nella creazione/aggiornamento di un partecipante"`}} {{template "error" dict "options" ($options|yaml) "data" .Data}} {{end}} diff --git a/templates/error_out_of_time.html.tpl b/templates/error_out_of_time.html.tpl index 7779e486..1b5f4aa9 100644 --- a/templates/error_out_of_time.html.tpl +++ b/templates/error_out_of_time.html.tpl @@ -1,11 +1,4 @@ {{ define "content" }} -
-

Errore

-

- Si è verificato nella gestione della prova di gara: {{.Data}} -

-

- Clicca {{.Claims|userId|show "Participant"|anchor "qui"}} per tornare alle informazioni sul partecipante. -

-
-{{ end }} +{{$options := `title: "Errore nella gestione della prova di gara"`}} +{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{end}} diff --git a/templates/error_participant_exists.html.tpl b/templates/error_participant_exists.html.tpl index 07a5883c..cc8d724c 100644 --- a/templates/error_participant_exists.html.tpl +++ b/templates/error_participant_exists.html.tpl @@ -1,11 +1,4 @@ {{ define "content" }} -
-

Errore

-

- Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: {{.Data}} -

-

- Clicca {{create "Participant"|anchor "qui"}} per tornare al modulo di creazione del partecipante. -

-
-{{ end }} +{{$options := `title: "Errore nella creazione/aggiornamento di un partecipante"`}} +{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{end}} diff --git a/templates/error_school_exists.html.tpl b/templates/error_school_exists.html.tpl index 3005bd56..315539f0 100644 --- a/templates/error_school_exists.html.tpl +++ b/templates/error_school_exists.html.tpl @@ -1,11 +1,4 @@ {{ define "content" }} -
-

Errore

-

- Si è verificato un errore durante la creazione o l'aggiornamento di una scuola: {{.Data}} -

-

- Clicca {{create "School"|anchor "qui"}} per tornare al modulo di iscrizione. -

-
-{{ end }} +{{$options := `title: "Errore nella creazione/aggiornamento di una scuola"`}} +{{template "error" dict "options" ($options|yaml) "data" .Data}} +{{end}} diff --git a/templates/layout/error.html.tpl b/templates/layout/error.html.tpl index fdf43bd6..1d80a421 100644 --- a/templates/layout/error.html.tpl +++ b/templates/layout/error.html.tpl @@ -6,7 +6,7 @@ {{.data}} {{if .data.Referer}} - Clicca {{.data.Referer|anchor "qui"}} per tornare all'azione precedente. + Clicca {{.data.Referer|alertLink "qui"}} per tornare all'azione precedente. {{end}}