8000 fix container creating by zhangshanwen · Pull Request #570 · qor5/admin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix container creating #570

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

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions example/integration/pagebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,37 @@ func TestPageBuilder(t *testing.T) {
},
ExpectPortalUpdate0ContainsInOrder: []string{`"Page.CategoryID":1`},
},

{
Name: "Page Builder add container Wrap SaveFunc",
Debug: true,
ReqFunc: func() *http.Request {
pageBuilderContainerTestData.TruncatePut(dbr)
req := NewMultipartBuilder().
PageURL("/page_builder/pages-editors/10_2024-05-21-v01_International").
EventFunc(pagebuilder.AddContainerEvent).
Query("modelName", "BrandGrid").
BuildEventFuncRequest()

return req
},
EventResponseMatch: func(t *testing.T, er *TestEventResponse) {
var (
container pagebuilder.Container
bd containers.BrandGrid
)
TestDB.Order("id desc").First(&container)
TestDB.Order("id desc").First(&bd)
if container.ModelName != "BrandGrid" {
t.Fatalf("containers not add")
return
}
if bd.AnchorID == "" {
t.Fatalf("wrap container creating error")
return
}
},
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
Expand Down
22 changes: 18 additions & 4 deletions pagebuilder/example/containers/brand_grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"database/sql/driver"
"encoding/json"
"errors"
"strconv"
"time"

"github.com/qor5/web/v3"
. "github.com/theplant/htmlgo"
"gorm.io/gorm"

"github.com/qor5/admin/v3/media"
"github.com/qor5/admin/v3/media/media_library"
"github.com/qor5/admin/v3/pagebuilder"
"github.com/qor5/admin/v3/presets"
"github.com/qor5/web/v3"
. "github.com/theplant/htmlgo"
"gorm.io/gorm"
)

type BrandGrid struct {
Expand Down Expand Up @@ -56,7 +59,18 @@
})
mb := vb.Model(&BrandGrid{})
eb := mb.Editing("AddTopSpace", "AddBottomSpace", "AnchorID", "Brands")

eb.Creating().WrapSaveFunc(func(in presets.SaveFunc) presets.SaveFunc {
return func(obj interface{}, id string, ctx *web.EventContext) (err error) {
p := obj.(*BrandGrid)
if p.AnchorID == "" {
p.AnchorID = strconv.Itoa(int(time.Now().Unix()))
}
if err = in(obj, id, ctx); err != nil {
return

Check warning on line 69 in pagebuilder/example/containers/brand_grid.go

View check run for this annotation

Codecov / codecov/patch

pagebuilder/example/containers/brand_grid.go#L69

Added line #L69 was not covered by tests
}
return
}
})
fb := pb.GetPresetsBuilder().NewFieldsBuilder(presets.WRITE).Model(&Brand{}).Only("Image", "Name")
fb.Field("Image").WithContextValue(media.MediaBoxConfig, &media_library.MediaBoxConfig{
AllowType: "image",
Expand Down
5 changes: 3 additions & 2 deletions pagebuilder/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ func withLocale(builder *Builder, wh *gorm.DB, locale string) *gorm.DB {
}

func (b *ModelBuilder) addContainerToPage(ctx *web.EventContext, pageID int, containerID, pageVersion, locale, modelName string) (modelID uint, newContainerID string, err error) {
model := b.builder.ContainerByName(modelName).NewModel()
containerMb := b.builder.ContainerByName(modelName)
model := containerMb.NewModel()
var dc DemoContainer
b.db.Where("model_name = ? AND locale_code = ?", modelName, locale).First(&dc)
if dc.ID != 0 && dc.ModelID != 0 {
b.db.Where("id = ?", dc.ModelID).First(model)
reflectutils.Set(model, "ID", uint(0))
}
err = b.db.Create(model).Error
err = containerMb.Editing().Creating().Saver(model, "", ctx)
if err != nil {
return
}
Expand Down
Loading
0