Delete responses for unsubscribed participants

This commit is contained in:
Andrea Fazzi 2020-12-28 12:23:41 +01:00
parent a5cc757ddd
commit ada7a9a28b
3 changed files with 31 additions and 2 deletions

View file

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

View file

@ -91,6 +91,7 @@
<div class="row">
<div class="col-md-12">
<h2 class="karmen-relation-header">Gare a cui il partecipante è iscritto</h2>
{{if .Data.Responses}}
{{range $response := .Data.Responses}}
<div class="card">
<div class="card-header">
@ -102,6 +103,9 @@
</div>
</div>
{{end}}
{{else}}
Il partecipante non è iscritto ad alcuna gara.
{{end}}
</div>
</div>
{{end}}