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"
port: "3000"
log_level: 2
language: "it"

View file

@ -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"
bcc: "bcc@fake.org"

View file

@ -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

View file

@ -34,6 +34,7 @@
</div>
</div>
{{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"