diff --git a/api/analysis.go b/api/analysis.go index e85b68cb5..9fe4b9556 100644 --- a/api/analysis.go +++ b/api/analysis.go @@ -2082,7 +2082,7 @@ func (h *AnalysisHandler) archive(ctx *gin.Context, q *gorm.DB) (err error) { db = db.Omit(clause.Associations) m.Archived = true m.Summary = summary - err = db.Updates(h.fields(&m)).Error + err = db.Save(&m).Error if err != nil { return } diff --git a/api/application.go b/api/application.go index 4207d175c..7a7411d5a 100644 --- a/api/application.go +++ b/api/application.go @@ -350,7 +350,7 @@ func (h ApplicationHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db = h.DB(ctx).Model(m) db = db.Omit(clause.Associations, "BucketID") - result = db.Updates(h.fields(m)) + result = db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/archetype.go b/api/archetype.go index d62d88a13..effb79420 100644 --- a/api/archetype.go +++ b/api/archetype.go @@ -211,7 +211,7 @@ func (h ArchetypeHandler) Update(ctx *gin.Context) { m.UpdateUser = h.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/assessment.go b/api/assessment.go index 6552f3ca0..438c16eb8 100644 --- a/api/assessment.go +++ b/api/assessment.go @@ -125,7 +125,7 @@ func (h AssessmentHandler) Update(ctx *gin.Context) { m.UpdateUser = h.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations, "Thresholds", "RiskMessages") - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/base.go b/api/base.go index bc19ce9ab..615a51261 100644 --- a/api/base.go +++ b/api/base.go @@ -84,12 +84,6 @@ func (h *BaseHandler) preLoad(db *gorm.DB, fields ...string) (tx *gorm.DB) { return } -// fields builds a map of fields. -func (h *BaseHandler) fields(m any) (mp map[string]any) { - mp = reflect.Fields(m) - return -} - // pk returns the PK (ID) parameter. func (h *BaseHandler) pk(ctx *gin.Context) (id uint) { s := ctx.Param(ID) diff --git a/api/businessservice.go b/api/businessservice.go index 41caf95b5..dabc03c09 100644 --- a/api/businessservice.go +++ b/api/businessservice.go @@ -153,7 +153,7 @@ func (h BusinessServiceHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/group.go b/api/group.go index 4e92337b3..8c56e9ce3 100644 --- a/api/group.go +++ b/api/group.go @@ -153,7 +153,7 @@ func (h StakeholderGroupHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/identity.go b/api/identity.go index 40d5bd410..90fedf642 100644 --- a/api/identity.go +++ b/api/identity.go @@ -202,7 +202,7 @@ func (h IdentityHandler) Update(ctx *gin.Context) { m.ID = id m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) - err = db.Updates(h.fields(m)).Error + err = db.Save(m).Error if err != nil { _ = ctx.Error(err) return diff --git a/api/jobfunction.go b/api/jobfunction.go index 82e615956..135dd1cb2 100644 --- a/api/jobfunction.go +++ b/api/jobfunction.go @@ -153,7 +153,7 @@ func (h JobFunctionHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/migrationwave.go b/api/migrationwave.go index 8651a369e..55cb37cef 100644 --- a/api/migrationwave.go +++ b/api/migrationwave.go @@ -130,7 +130,7 @@ func (h MigrationWaveHandler) Update(ctx *gin.Context) { m.UpdateUser = h.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/proxy.go b/api/proxy.go index 76500cc79..ccaf1551c 100644 --- a/api/proxy.go +++ b/api/proxy.go @@ -160,7 +160,7 @@ func (h ProxyHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/questionnaire.go b/api/questionnaire.go index 94a9b3e9f..ba183afe7 100644 --- a/api/questionnaire.go +++ b/api/questionnaire.go @@ -165,19 +165,16 @@ func (h QuestionnaireHandler) Update(ctx *gin.Context) { updated := r.Model() updated.ID = id updated.UpdateUser = h.CurrentUser(ctx) - var fields map[string]any if m.Builtin() { - fields = map[string]any{ - "updateUser": updated.UpdateUser, - "required": updated.Required, - } + m.UpdateUser = updated.UpdateUser + m.Required = updated.Required } else { - fields = h.fields(updated) + m = updated } db = h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result = db.Updates(fields) + result = db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/review.go b/api/review.go index 85fbbdcb8..db4c65222 100644 --- a/api/review.go +++ b/api/review.go @@ -30,7 +30,7 @@ func (h ReviewHandler) AddRoutes(e *gin.Engine) { routeGroup.GET(ReviewRoot, h.Get) routeGroup.PUT(ReviewRoot, h.Update) routeGroup.DELETE(ReviewRoot, h.Delete) - routeGroup.POST(CopyRoot, h.CopyReview) + routeGroup.POST(CopyRoot, h.CopyReview, Transaction) } // Get godoc @@ -155,7 +155,7 @@ func (h ReviewHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return @@ -194,26 +194,15 @@ func (h ReviewHandler) CopyReview(ctx *gin.Context) { Comments: m.Comments, ApplicationID: &id, } - existing := []model.Review{} - result = h.DB(ctx).Find(&existing, "applicationid = ?", id) + result = h.DB(ctx).Delete(&model.Review{}, "applicationid = ?", id) if result.Error != nil { _ = ctx.Error(result.Error) return } - // if the application doesn't already have a review, create one. - if len(existing) == 0 { - result = h.DB(ctx).Create(copied) - if result.Error != nil { - _ = ctx.Error(result.Error) - return - } - // if the application already has a review, replace it with the copied review. - } else { - result = h.DB(ctx).Model(&existing[0]).Updates(h.fields(copied)) - if result.Error != nil { - _ = ctx.Error(result.Error) - return - } + result = h.DB(ctx).Create(copied) + if result.Error != nil { + _ = ctx.Error(result.Error) + return } } h.Status(ctx, http.StatusNoContent) diff --git a/api/ruleset.go b/api/ruleset.go index 5732aa231..43d8e4023 100644 --- a/api/ruleset.go +++ b/api/ruleset.go @@ -248,7 +248,7 @@ func (h *RuleSetHandler) update(ctx *gin.Context, r *RuleSet) (err error) { m.UpdateUser = h.CurrentUser(ctx) db = h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - err = db.Updates(h.fields(m)).Error + err = db.Save(m).Error if err != nil { return } @@ -267,7 +267,7 @@ func (h *RuleSetHandler) update(ctx *gin.Context, r *RuleSet) (err error) { for i := range m.Rules { m := &m.Rules[i] db = h.DB(ctx).Model(m) - err = db.Updates(h.fields(m)).Error + err = db.Updates(m).Error if err != nil { return } diff --git a/api/setting.go b/api/setting.go index 082ca019c..ad5c6997e 100644 --- a/api/setting.go +++ b/api/setting.go @@ -178,19 +178,19 @@ func (h SettingHandler) Update(ctx *gin.Context) { return } - updates := Setting{} - updates.Key = key - err := h.Bind(ctx, &updates.Value) + m := &model.Setting{} + result := h.DB(ctx).First(m, "key = ?", key) + if result.Error != nil { + _ = ctx.Error(result.Error) + return + } + err := h.Bind(ctx, &m.Value) if err != nil { _ = ctx.Error(err) return } - - m := updates.Model() m.UpdateUser = h.BaseHandler.CurrentUser(ctx) - db := h.DB(ctx).Model(m) - db = db.Where("key", key) - result := db.Updates(h.fields(m)) + result = h.DB(ctx).Save(m) if result.Error != nil { _ = ctx.Error(result.Error) } diff --git a/api/stakeholder.go b/api/stakeholder.go index 4cb659310..4e5136bf1 100644 --- a/api/stakeholder.go +++ b/api/stakeholder.go @@ -153,7 +153,7 @@ func (h StakeholderHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/tag.go b/api/tag.go index 17d472444..648456b5f 100644 --- a/api/tag.go +++ b/api/tag.go @@ -153,7 +153,7 @@ func (h TagHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/tagcategory.go b/api/tagcategory.go index 8ae7799b2..025065095 100644 --- a/api/tagcategory.go +++ b/api/tagcategory.go @@ -160,7 +160,7 @@ func (h TagCategoryHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/target.go b/api/target.go index 83a123140..c27c8fe00 100644 --- a/api/target.go +++ b/api/target.go @@ -224,7 +224,7 @@ func (h TargetHandler) Update(ctx *gin.Context) { } db = h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result = db.Updates(h.fields(m)) + result = db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return diff --git a/api/tracker.go b/api/tracker.go index 650607bac..96434ed12 100644 --- a/api/tracker.go +++ b/api/tracker.go @@ -180,7 +180,7 @@ func (h TrackerHandler) Update(ctx *gin.Context) { m.UpdateUser = h.BaseHandler.CurrentUser(ctx) db := h.DB(ctx).Model(m) db = db.Omit(clause.Associations) - result := db.Updates(h.fields(m)) + result := db.Save(m) if result.Error != nil { _ = ctx.Error(result.Error) return