Resolve record not found bug when updating participants

This commit is contained in:
Andrea Fazzi 2020-12-24 18:05:24 +01:00
parent 3141891a12
commit 67fbb581fc
4 changed files with 45 additions and 43 deletions

View file

@ -1,4 +1,5 @@
url: "http://localhost:3000" url: "http://localhost:3000"
port: "3000"
log_level: 2 log_level: 2
language: "it" language: "it"

View file

@ -1,4 +1,5 @@
url: "http://localhost:3000" url: "http://localhost:3000"
port: "3000"
log_level: 2 log_level: 2
language: "it" language: "it"
profiling: false profiling: false
@ -29,4 +30,4 @@ smtp:
username: "" username: ""
password: "" password: ""
from: "no-reply@olimpiadi-economiaefinanza.it" from: "no-reply@olimpiadi-economiaefinanza.it"
bcc: "bcc@fake.org" bcc: "bcc@fake.org"

View file

@ -102,19 +102,6 @@ func (model *Participant) SetUpdaterIP(addr string) {
model.UpdaterIP = addr 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) { func (model *Participant) exists(db *Database) (*User, error) {
var user User var user User
if err := db._db.First(&user, &User{Username: model.username()}).Error; err != nil && err != gorm.ErrRecordNotFound { 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 { func (model *Participant) AfterSave(tx *gorm.DB) error {
for _, contest := range model.Contests { for _, contest := range model.Contests {
var response Response var response Response
if err := tx.FirstOrCreate( if err := tx.Debug().FirstOrCreate(
&response, &response,
&Response{ &Response{
Name: fmt.Sprintf("%s (%s)", contest.Name, model.String()), 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 // Check if a participant of the same category exists
var school School var school School
if err := db._db.First(&school, participant.SchoolID).Error; err != nil { if participant.SchoolID > 0 {
return nil, err if err := db._db.First(&school, participant.SchoolID).Error; err != nil {
} return nil, err
hasCategory, err := school.HasCategory(db, participant) }
if err != nil { hasCategory, err := school.HasCategory(db, participant)
return nil, err if err != nil {
} return nil, err
if hasCategory { }
return nil, errors.CategoryExists if hasCategory {
return nil, errors.CategoryExists
}
} }
WriteCreator(r, participant) WriteCreator(r, participant)
@ -241,14 +230,17 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
if isAdministrator(r) { if isAdministrator(r) {
var response Response 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 return nil, err
} }
response.CreatorID = getUserIDFromTokenAsUint(r) if err != gorm.ErrRecordNotFound {
response.CreatorID = getUserIDFromTokenAsUint(r)
if err := db._db.Save(&response).Error; err != nil { if err := db._db.Save(&response).Error; err != nil {
return nil, err 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 // Check if a participant of the same category exists
var school School var school School
if err := db._db.First(&school, participant.(*Participant).SchoolID).Error; err != nil { if participant.(*Participant).SchoolID > 0 {
return nil, err 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 { hasCategory, err := school.HasCategory(db, participant.(*Participant))
return nil, err if err != nil {
} return nil, err
if hasCategory { }
return nil, errors.CategoryExists if hasCategory {
return nil, errors.CategoryExists
}
} }
if err := db._db. if err := db._db.
@ -437,18 +431,22 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
if isAdministrator(r) { if isAdministrator(r) {
var response Response var response Response
if err := db._db.First( err := db._db.First(
&response, &response,
&Response{ParticipantID: participant.(*Participant).ID}, &Response{ParticipantID: participant.(*Participant).ID},
).Error; err != nil { ).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
} }
response.UpdaterID = getUserIDFromTokenAsUint(r) if err != gorm.ErrRecordNotFound {
response.UpdaterID = getUserIDFromTokenAsUint(r)
if err := db._db.Save(&response).Error; err != nil { if err := db._db.Save(&response).Error; err != nil {
return nil, err return nil, err
}
} }
} }
return participant.(*Participant), nil return participant.(*Participant), nil

View file

@ -34,6 +34,7 @@
</div> </div>
</div> </div>
{{if not $update}}
{{$options := ` {{$options := `
name: "FiscalCode" name: "FiscalCode"
id: "participant_fiscalcode" id: "participant_fiscalcode"
@ -43,7 +44,8 @@
inputClass: "form-control uppercase" inputClass: "form-control uppercase"
required: "true" `}} required: "true" `}}
{{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}} {{template "input" dict "options" ($options|yaml) "value" (.Data|field "FiscalCode") "update" $update}}
{{end}}
{{$options := ` {{$options := `
name: "category_id" name: "category_id"
id: "category_id" id: "category_id"