diff --git a/orm/participant.go b/orm/participant.go index a8b0b7ec..5fd58a6c 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -129,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.Debug().FirstOrCreate( + if err := tx.FirstOrCreate( &response, &Response{ Name: fmt.Sprintf("%s (%s)", contest.Name, model.String()), @@ -449,6 +449,31 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re } + // Delete responses for unsubscribed participants + + var found bool + toBeDeletedResponses := make([]*Response, 0) + for _, response := range participant.(*Participant).Responses { + for _, contest := range participant.(*Participant).Contests { + if response.ContestID == contest.ID { + found = true + break + } + } + if !found { + toBeDeletedResponses = append(toBeDeletedResponses, response) + } + found = false + } + + if len(toBeDeletedResponses) > 0 { + for _, response := range toBeDeletedResponses { + if err := db._db.Unscoped().Delete(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 2824abeb..5669f4ab 100644 --- a/templates/participants_add_update.html.tpl +++ b/templates/participants_add_update.html.tpl @@ -59,7 +59,7 @@ {{$options := ` { name: "school_id", id: "school_id", label: "Scuola del partecipante", title: "Seleziona la scuola"}`}} {{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllSchools") "selected" (.Data|field "SelectedSchool") "update" $update "form" $form}} - {{$options := ` + {{$options := ` name: "contest_ids" id: "contest_ids" label: "Gare a cui il partecipante è iscritto" diff --git a/templates/participants_show.html.tpl b/templates/participants_show.html.tpl index ee555be4..5b436198 100644 --- a/templates/participants_show.html.tpl +++ b/templates/participants_show.html.tpl @@ -91,6 +91,7 @@

Gare a cui il partecipante è iscritto

+ {{if .Data.Responses}} {{range $response := .Data.Responses}}
@@ -102,6 +103,9 @@
{{end}} + {{else}} + Il partecipante non è iscritto ad alcuna gara. + {{end}}
{{end}}