From b142c1b757b35a0588de05d9213a06cd5b751beb Mon Sep 17 00:00:00 2001 From: Chen Junda Date: Fri, 2 Feb 2024 20:54:46 +0800 Subject: [PATCH] fix 600 --- repositories/summary.go | 48 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/repositories/summary.go b/repositories/summary.go index 1dc3f82f..12469479 100644 --- a/repositories/summary.go +++ b/repositories/summary.go @@ -36,28 +36,42 @@ func (r *SummaryRepository) GetAll() ([]*models.Summary, error) { func (r *SummaryRepository) Insert(summary *models.Summary) error { - itemsToCreate := []interface{}{summary} + if err := r.db.Transaction(func(tx *gorm.DB) error { + if err := r.db.Create(summary).Error; err != nil { + return err + } - // required due to setting gorm:"-" in the model definition - // see https://github.com/muety/wakapi/issues/600#issuecomment-1921723789 - // see https://github.com/muety/wakapi/pull/592#discussion_r1450478355 - for _, item := range summary.Machines { - itemsToCreate = append(itemsToCreate, item) - } + itemsToCreate := []*models.SummaryItem{} - for _, item := range summary.Languages { - itemsToCreate = append(itemsToCreate, item) - } + // required due to setting gorm:"-" in the model definition + // see https://github.com/muety/wakapi/issues/600#issuecomment-1921723789 + // see https://github.com/muety/wakapi/pull/592#discussion_r1450478355 + for _, item := range summary.Machines { + item.SummaryID = summary.ID + itemsToCreate = append(itemsToCreate, item) + } - for _, item := range summary.OperatingSystems { - itemsToCreate = append(itemsToCreate, item) - } + for _, item := range summary.Languages { + item.SummaryID = summary.ID + itemsToCreate = append(itemsToCreate, item) + } - for _, item := range summary.Editors { - itemsToCreate = append(itemsToCreate, item) - } + for _, item := range summary.OperatingSystems { + item.SummaryID = summary.ID + itemsToCreate = append(itemsToCreate, item) + } + + for _, item := range summary.Editors { + item.SummaryID = summary.ID + itemsToCreate = append(itemsToCreate, item) + } + + if err := r.db.Create(itemsToCreate).Error; err != nil { + return err + } - if err := r.db.Create(itemsToCreate).Error; err != nil { + return nil + }); err != nil { return err }