Skip to content

Commit

Permalink
Test generated endppint
Browse files Browse the repository at this point in the history
  • Loading branch information
pelusa-v committed May 1, 2024
1 parent e19c4f4 commit 57448af
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/pkg/admin/registers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func (admin *Admin) registerModelObjectCreatePage(modelType reflect.Type) {

func (admin *Admin) registerModelObjectCreateEndpoint(modelType reflect.Type) {
dbModel := data.NewDbModel(modelType, admin.GormDB)
modelObjectCreateRoute := fmt.Sprintf("/admin/%s/create", modelType.Name())
modelObjectCreateRoute := fmt.Sprintf("/admin/%s/actions/create", modelType.Name())
modelDetailPageRoute := fmt.Sprintf("/admin/%s", modelType.Name())
admin.Handler.RegisterCreateEndpoint(modelObjectCreateRoute, modelDetailPageRoute, func(data interface{}) {
dbModel.CreateObject(data)
admin.Handler.RegisterCreateEndpoint(modelObjectCreateRoute, modelDetailPageRoute, func(data interface{}) error {
return dbModel.CreateObject(data)
})
}
5 changes: 3 additions & 2 deletions src/pkg/data/db_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (m *DbModel) GetObject(pk string) interface{} {
return concreteObject
}

func (m *DbModel) CreateObject(newObject interface{}) {
m.db.Create(newObject)
func (m *DbModel) CreateObject(newObject interface{}) error {
res := m.db.Create(newObject)
return res.Error
}
2 changes: 1 addition & 1 deletion src/pkg/data/forms_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (form *FormData) SetFormInputs(model *DbModel) {
input.Id = f.Name
input.Name = f.Name
input.Label = f.Name
input.Disabled = IsPkField(f)
// input.Disabled = IsPkField(f)
input.Type = GetHtmlInputType(f)
form.SimpleInputs = append(form.SimpleInputs, input)

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/handlers/app_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type AppHandler interface {
RegisterSimplePage(tmpl *template.Template, templateName string, route string, tmplDataFunc func() any)
RegisterPkPage(tmpl *template.Template, templateName string, route string, tmplDataFunc func(pk string) any)
RegisterStatic(fs fs.FS)
RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{}))
RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{}) error)
// RegisterHomePage(tmpl *template.Template)
// RegisterModelDetailPage(modelType reflect.Type, tmpl *template.Template)
}
Expand Down
11 changes: 8 additions & 3 deletions src/pkg/handlers/fiber_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ func (handler *FiberHandler) RegisterPkPage(tmpl *template.Template, templateNam
})
}

func (handler *FiberHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{})) {
func (handler *FiberHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{}) error) {

RegisterFiberEndpoint(route, POST, handler, func(c *fiber.Ctx) error {
var dataToCreate interface{}
actionCreateFunc(c.BodyParser(dataToCreate))
return c.Redirect(redirect)
err := actionCreateFunc(c.BodyParser(dataToCreate))
if err != nil {
panic(err)
return c.SendStatus(500)
}
// return c.Redirect(redirect)
return c.SendStatus(200)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/handlers/gin_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func (handler *GinHandler) RegisterPkPage(tmpl *template.Template, templateName
func (handler *GinHandler) RegisterStatic(fs fs.FS) {
}

func (handler *GinHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{})) {
func (handler *GinHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{}) error) {
}
2 changes: 1 addition & 1 deletion src/pkg/handlers/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func (handler *BuiltInHandler) RegisterPkPage(tmpl *template.Template, templateN
func (handler *BuiltInHandler) RegisterStatic(fs fs.FS) {
}

func (handler *BuiltInHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{})) {
func (handler *BuiltInHandler) RegisterCreateEndpoint(route string, redirect string, actionCreateFunc func(data interface{}) error) {
}

0 comments on commit 57448af

Please sign in to comment.