-
Notifications
You must be signed in to change notification settings - Fork 16
Refactor container creation to use context and saver #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a4ed1f7
c743f6d
d910c8f
e580e12
9dca452
16f6fdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -936,7 +936,7 @@ func (b *Builder) firstOrCreateDemoContainers(ctx *web.EventContext, cons ...*Co | |
cons = b.containerBuilders | ||
} | ||
for _, con := range cons { | ||
if err := con.firstOrCreate(slices.Concat(localeCodes)); err != nil { | ||
if err := con.firstOrCreate(ctx, slices.Concat(localeCodes)); err != nil { | ||
continue | ||
} | ||
} | ||
|
@@ -1320,20 +1320,25 @@ func (b *ContainerBuilder) getContainerDataID(modelID int, primarySlug string) s | |
return fmt.Sprintf(inflection.Plural(strcase.ToKebab(b.name))+"_%v_%v", modelID, primarySlug) | ||
} | ||
|
||
func (b *ContainerBuilder) firstOrCreate(localeCodes []string) (err error) { | ||
func (b *ContainerBuilder) firstOrCreate(ctx *web.EventContext, localeCodes []string) (err error) { | ||
var ( | ||
db = b.builder.db | ||
obj = b.mb.NewModel() | ||
cons []*DemoContainer | ||
m = &DemoContainer{} | ||
db = b.builder.db | ||
obj = b.mb.NewModel() | ||
cons []*DemoContainer | ||
m = &DemoContainer{} | ||
saver = b.mb.Editing().Creating().Saver | ||
) | ||
if len(localeCodes) == 0 { | ||
return | ||
} | ||
ctx.R.Form.Set(ParamContainerCreate, "1") | ||
return db.Transaction(func(tx *gorm.DB) (vErr error) { | ||
ctx.WithContextValue(gorm2op.CtxKeyDB{}, tx) | ||
defer ctx.WithContextValue(gorm2op.CtxKeyDB{}, nil) | ||
tx.Where("model_name = ? and locale_code in ? ", b.name, localeCodes).Find(&cons) | ||
|
||
if len(cons) == 0 { | ||
if vErr = tx.Create(obj).Error; vErr != nil { | ||
if vErr = saver(obj, "", ctx); vErr != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Nil Saver Causes Panic, Context Update FailsThe
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The empty string parameter passed to saver is unclear. Consider using a named constant or adding a comment to explain what this parameter represents. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
return | ||
} | ||
modelID := reflectutils.MustGet(obj, "ID").(uint) | ||
|
@@ -1364,7 +1369,7 @@ func (b *ContainerBuilder) firstOrCreate(localeCodes []string) (err error) { | |
continue | ||
} | ||
obj = b.mb.NewModel() | ||
if vErr = tx.Create(obj).Error; vErr != nil { | ||
if vErr = saver(obj, "", ctx); vErr != nil { | ||
return | ||
} | ||
modelID := reflectutils.MustGet(obj, "ID").(uint) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The context value is being set and then immediately cleared in the defer statement, which means the database transaction will not be available to the saver function. The defer should be moved after the saver calls or the context clearing should be removed.
Copilot uses AI. Check for mistakes.