Skip to content

Commit

Permalink
Revert ":seedling: Omit associations during Create operations (#733)"
Browse files Browse the repository at this point in the history
This reverts commit 65d0910.
  • Loading branch information
jortel authored Aug 5, 2024
1 parent 92bde32 commit 1f79681
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 113 deletions.
39 changes: 8 additions & 31 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ func (h ApplicationHandler) AddRoutes(e *gin.Engine) {
routeGroup.DELETE(ApplicationRoot, h.Delete)
// Tags
routeGroup = e.Group("/")
routeGroup.Use(Required("applications"), Transaction)
routeGroup.Use(Required("applications"))
routeGroup.GET(ApplicationTagsRoot, h.TagList)
routeGroup.GET(ApplicationTagsRoot+"/", h.TagList)
routeGroup.POST(ApplicationTagsRoot, h.TagAdd)
routeGroup.DELETE(ApplicationTagRoot, h.TagDelete)
routeGroup.PUT(ApplicationTagsRoot, h.TagReplace)
routeGroup.PUT(ApplicationTagsRoot, h.TagReplace, Transaction)
// Facts
routeGroup = e.Group("/")
routeGroup.Use(Required("applications.facts"), Transaction)
routeGroup.Use(Required("applications.facts"))
routeGroup.GET(ApplicationFactsRoot, h.FactGet)
routeGroup.GET(ApplicationFactsRoot+"/", h.FactGet)
routeGroup.POST(ApplicationFactsRoot, h.FactCreate)
routeGroup.GET(ApplicationFactRoot, h.FactGet)
routeGroup.PUT(ApplicationFactRoot, h.FactPut)
routeGroup.DELETE(ApplicationFactRoot, h.FactDelete)
routeGroup.PUT(ApplicationFactsRoot, h.FactPut)
routeGroup.PUT(ApplicationFactsRoot, h.FactPut, Transaction)
// Bucket
routeGroup = e.Group("/")
routeGroup.Use(Required("applications.bucket"))
Expand All @@ -84,11 +84,11 @@ func (h ApplicationHandler) AddRoutes(e *gin.Engine) {
routeGroup.DELETE(AppBucketContentRoot, h.BucketDelete)
// Stakeholders
routeGroup = e.Group("/")
routeGroup.Use(Required("applications.stakeholders"), Transaction)
routeGroup.Use(Required("applications.stakeholders"))
routeGroup.PUT(AppStakeholdersRoot, h.StakeholdersUpdate)
// Assessments
routeGroup = e.Group("/")
routeGroup.Use(Required("applications.assessments"), Transaction)
routeGroup.Use(Required("applications.assessments"))
routeGroup.GET(AppAssessmentsRoot, h.AssessmentList)
routeGroup.POST(AppAssessmentsRoot, h.AssessmentCreate)
}
Expand Down Expand Up @@ -210,23 +210,11 @@ func (h ApplicationHandler) Create(ctx *gin.Context) {
}
m := r.Model()
m.CreateUser = h.BaseHandler.CurrentUser(ctx)
result := h.DB(ctx).Omit(clause.Associations).Create(m)
result := h.DB(ctx).Omit("Tags").Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
db := h.DB(ctx).Model(m)
err = db.Association("Identities").Replace(m.Identities)
if err != nil {
_ = ctx.Error(err)
return
}
db = h.DB(ctx).Model(m)
err = db.Association("Contributors").Replace(m.Contributors)
if err != nil {
_ = ctx.Error(err)
return
}

tags := []model.ApplicationTag{}
if len(r.Tags) > 0 {
Expand Down Expand Up @@ -1090,21 +1078,11 @@ func (h ApplicationHandler) AssessmentCreate(ctx *gin.Context) {
assessment.PrepareForApplication(resolver, application, m)
newAssessment = true
}
result = h.DB(ctx).Omit(clause.Associations).Create(m)
result = h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err = h.DB(ctx).Model(m).Association("Stakeholders").Replace("Stakeholders", m.Stakeholders)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("StakeholderGroups").Replace("StakeholderGroups", m.StakeholderGroups)
if err != nil {
_ = ctx.Error(err)
return
}
if newAssessment {
metrics.AssessmentsInitiated.Inc()
}
Expand Down Expand Up @@ -1160,7 +1138,6 @@ func (r *Application) With(m *model.Application, tags []model.ApplicationTag) {
r.Identities,
ref)
}
r.Tags = []TagRef{}
for i := range tags {
ref := TagRef{}
ref.With(tags[i].TagID, tags[i].Tag.Name, tags[i].Source, false)
Expand Down
35 changes: 2 additions & 33 deletions api/archetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,12 @@ func (h ArchetypeHandler) Create(ctx *gin.Context) {
}
m := r.Model()
m.CreateUser = h.CurrentUser(ctx)
result := h.DB(ctx).Omit(clause.Associations).Create(m)
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}

err = h.DB(ctx).Model(m).Association("Stakeholders").Replace("Stakeholders", m.Stakeholders)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("StakeholderGroups").Replace("StakeholderGroups", m.StakeholderGroups)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("CriteriaTags").Replace("CriteriaTags", m.CriteriaTags)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("Tags").Replace("Tags", m.Tags)
if err != nil {
_ = ctx.Error(err)
return
}

archetypes := []model.Archetype{}
db := h.preLoad(h.DB(ctx), "Tags", "CriteriaTags")
result = db.Find(&archetypes)
Expand Down Expand Up @@ -340,21 +319,11 @@ func (h ArchetypeHandler) AssessmentCreate(ctx *gin.Context) {
assessment.PrepareForArchetype(resolver, archetype, m)
newAssessment = true
}
result = h.DB(ctx).Omit(clause.Associations).Create(m)
result = h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err = h.DB(ctx).Model(m).Association("Stakeholders").Replace("Stakeholders", m.Stakeholders)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("StakeholderGroups").Replace("StakeholderGroups", m.StakeholderGroups)
if err != nil {
_ = ctx.Error(err)
return
}
if newAssessment {
metrics.AssessmentsInitiated.Inc()
}
Expand Down
12 changes: 1 addition & 11 deletions api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,11 @@ func (h StakeholderGroupHandler) Create(ctx *gin.Context) {
}
m := r.Model()
m.CreateUser = h.BaseHandler.CurrentUser(ctx)
result := h.DB(ctx).Omit(clause.Associations).Create(m)
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err = h.DB(ctx).Model(m).Association("Stakeholders").Replace(m.Stakeholders)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("MigrationWaves").Replace(m.MigrationWaves)
if err != nil {
_ = ctx.Error(err)
return
}
r.With(m)

h.Respond(ctx, http.StatusCreated, r)
Expand Down
2 changes: 1 addition & 1 deletion api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (h IdentityHandler) AddRoutes(e *gin.Engine) {
routeGroup.GET(IdentitiesRoot+"/", h.setDecrypted, h.List)
routeGroup.POST(IdentitiesRoot, h.Create)
routeGroup.GET(IdentityRoot, h.setDecrypted, h.Get)
routeGroup.PUT(IdentityRoot, Transaction, h.Update)
routeGroup.PUT(IdentityRoot, h.Update, Transaction)
routeGroup.DELETE(IdentityRoot, h.Delete)
}

Expand Down
17 changes: 1 addition & 16 deletions api/migrationwave.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,11 @@ func (h MigrationWaveHandler) Create(ctx *gin.Context) {
}
m := r.Model()
m.CreateUser = h.CurrentUser(ctx)
result := h.DB(ctx).Omit(clause.Associations).Create(m)
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err = h.DB(ctx).Model(m).Association("Applications").Replace("Applications", m.Applications)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("Stakeholders").Replace("Stakeholders", m.Stakeholders)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("StakeholderGroups").Replace("StakeholderGroups", m.StakeholderGroups)
if err != nil {
_ = ctx.Error(err)
return
}
r.With(m)

h.Respond(ctx, http.StatusCreated, r)
Expand Down
22 changes: 1 addition & 21 deletions api/stakeholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,11 @@ func (h StakeholderHandler) Create(ctx *gin.Context) {
}
m := r.Model()
m.CreateUser = h.BaseHandler.CurrentUser(ctx)
result := h.DB(ctx).Omit(clause.Associations).Create(m)
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
err = h.DB(ctx).Model(m).Association("Groups").Replace(m.Groups)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("Owns").Replace(m.Owns)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("Contributes").Replace(m.Contributes)
if err != nil {
_ = ctx.Error(err)
return
}
err = h.DB(ctx).Model(m).Association("MigrationWaves").Replace(m.MigrationWaves)
if err != nil {
_ = ctx.Error(err)
return
}
r.With(m)

h.Respond(ctx, http.StatusCreated, r)
Expand Down

0 comments on commit 1f79681

Please sign in to comment.