From 67fbb581fcc0fe264ee1964755fcf09afcd5d3a3 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 24 Dec 2020 18:05:24 +0100 Subject: [PATCH] Resolve record not found bug when updating participants --- config/config.yaml | 1 + docker/oef_dev/config/config.yaml | 3 +- orm/participant.go | 80 +++++++++++----------- templates/participants_add_update.html.tpl | 4 +- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index c644c8be..546f33a4 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,4 +1,5 @@ url: "http://localhost:3000" +port: "3000" log_level: 2 language: "it" diff --git a/docker/oef_dev/config/config.yaml b/docker/oef_dev/config/config.yaml index ccd599f2..2a32e55f 100644 --- a/docker/oef_dev/config/config.yaml +++ b/docker/oef_dev/config/config.yaml @@ -1,4 +1,5 @@ url: "http://localhost:3000" +port: "3000" log_level: 2 language: "it" profiling: false @@ -29,4 +30,4 @@ smtp: username: "" password: "" from: "no-reply@olimpiadi-economiaefinanza.it" - bcc: "bcc@fake.org" \ No newline at end of file + bcc: "bcc@fake.org" diff --git a/orm/participant.go b/orm/participant.go index 88f01148..a8b0b7ec 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -102,19 +102,6 @@ func (model *Participant) SetUpdaterIP(addr string) { model.UpdaterIP = addr } -// func setFlashMessage(w http.ResponseWriter, r *http.Request, key string) error { -// session, err := store.Get(r, "flash-session") -// if err != nil { -// return err -// } -// session.AddFlash(i18n.FlashMessages[key][config.Config.Language]) -// err = session.Save(r, w) -// if err != nil { -// return err -// } -// return nil -// } - func (model *Participant) exists(db *Database) (*User, error) { var user User if err := db._db.First(&user, &User{Username: model.username()}).Error; err != nil && err != gorm.ErrRecordNotFound { @@ -142,7 +129,7 @@ func (model *Participant) BeforeSave(tx *gorm.DB) error { func (model *Participant) AfterSave(tx *gorm.DB) error { for _, contest := range model.Contests { var response Response - if err := tx.FirstOrCreate( + if err := tx.Debug().FirstOrCreate( &response, &Response{ Name: fmt.Sprintf("%s (%s)", contest.Name, model.String()), @@ -219,15 +206,17 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re // Check if a participant of the same category exists var school School - if err := db._db.First(&school, participant.SchoolID).Error; err != nil { - return nil, err - } - hasCategory, err := school.HasCategory(db, participant) - if err != nil { - return nil, err - } - if hasCategory { - return nil, errors.CategoryExists + if participant.SchoolID > 0 { + if err := db._db.First(&school, participant.SchoolID).Error; err != nil { + return nil, err + } + hasCategory, err := school.HasCategory(db, participant) + if err != nil { + return nil, err + } + if hasCategory { + return nil, errors.CategoryExists + } } WriteCreator(r, participant) @@ -241,14 +230,17 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re if isAdministrator(r) { var response Response - if err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil { + err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error + if err != nil && err != gorm.ErrRecordNotFound { return nil, err } - response.CreatorID = getUserIDFromTokenAsUint(r) + if err != gorm.ErrRecordNotFound { + response.CreatorID = getUserIDFromTokenAsUint(r) - if err := db._db.Save(&response).Error; err != nil { - return nil, err + if err := db._db.Save(&response).Error; err != nil { + return nil, err + } } } @@ -397,15 +389,17 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re // Check if a participant of the same category exists var school School - if err := db._db.First(&school, participant.(*Participant).SchoolID).Error; err != nil { - return nil, err - } - hasCategory, err := school.HasCategory(db, participant.(*Participant)) - if err != nil { - return nil, err - } - if hasCategory { - return nil, errors.CategoryExists + if participant.(*Participant).SchoolID > 0 { + if err := db._db.First(&school, participant.(*Participant).SchoolID).Error; err != nil { + return nil, err + } + hasCategory, err := school.HasCategory(db, participant.(*Participant)) + if err != nil { + return nil, err + } + if hasCategory { + return nil, errors.CategoryExists + } } if err := db._db. @@ -437,18 +431,22 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re if isAdministrator(r) { var response Response - if err := db._db.First( + err := db._db.First( &response, &Response{ParticipantID: participant.(*Participant).ID}, - ).Error; err != nil { + ).Error + if err != nil && err != gorm.ErrRecordNotFound { return nil, err } - response.UpdaterID = getUserIDFromTokenAsUint(r) + if err != gorm.ErrRecordNotFound { + response.UpdaterID = getUserIDFromTokenAsUint(r) - if err := db._db.Save(&response).Error; err != nil { - return nil, err + if err := db._db.Save(&response).Error; err != nil { + return nil, err + } } + } return participant.(*Participant), nil diff --git a/templates/participants_add_update.html.tpl b/templates/participants_add_update.html.tpl index ef1f9a73..2824abeb 100644 --- a/templates/participants_add_update.html.tpl +++ b/templates/participants_add_update.html.tpl @@ -34,6 +34,7 @@ + {{if not $update}} {{$options := ` name: "FiscalCode" id: "participant_fiscalcode" @@ -43,7 +44,8 @@ inputClass: "form-control uppercase" required: "true" `}} {{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}} - + {{end}} + {{$options := ` name: "category_id" id: "category_id"