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

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,6 +206,7 @@ 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 participant.SchoolID > 0 {
if err := db._db.First(&school, participant.SchoolID).Error; err != nil {
return nil, err
}
@ -229,6 +217,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
if hasCategory {
return nil, errors.CategoryExists
}
}
WriteCreator(r, participant)
@ -241,16 +230,19 @@ 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
}
if err != gorm.ErrRecordNotFound {
response.CreatorID = getUserIDFromTokenAsUint(r)
if err := db._db.Save(&response).Error; err != nil {
return nil, err
}
}
}
return participant, nil
}
@ -397,6 +389,7 @@ 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 participant.(*Participant).SchoolID > 0 {
if err := db._db.First(&school, participant.(*Participant).SchoolID).Error; err != nil {
return nil, err
}
@ -407,6 +400,7 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
if hasCategory {
return nil, errors.CategoryExists
}
}
if err := db._db.
Where([]uint(participant.(*Participant).ContestIDs)).
@ -437,13 +431,15 @@ 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
}
if err != gorm.ErrRecordNotFound {
response.UpdaterID = getUserIDFromTokenAsUint(r)
if err := db._db.Save(&response).Error; err != nil {
@ -451,6 +447,8 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
}
}
}
return participant.(*Participant), nil
}
}

View file

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