|
@@ -52,66 +52,66 @@ func (crud *CRUD) getCallbackByFormat(format string) EachQueryCallback {
|
|
|
}
|
|
|
|
|
|
func (crud *CRUD) Register(module string, value interface{}, opts ...Option) {
|
|
|
- o := &Options{ctx: crud.ctx}
|
|
|
+ o := &Options{ctx: crud.ctx, DB: crud.db}
|
|
|
for _, f := range opts {
|
|
|
f(o)
|
|
|
}
|
|
|
if crud.httpSvr == nil {
|
|
|
return
|
|
|
}
|
|
|
- e := newEntity(crud.ctx, value, o.db)
|
|
|
+ e := newEntity(crud.ctx, value, o.DB)
|
|
|
e.instance = crud
|
|
|
- e.Callbacks = o.callbacks
|
|
|
+ e.Callbacks = o.Callbacks
|
|
|
e.SetModule(module)
|
|
|
if o.enable != nil {
|
|
|
e.enable = o.enable
|
|
|
} else {
|
|
|
e.enable = &enable
|
|
|
}
|
|
|
- if o.formatter == nil {
|
|
|
+ if o.Formatter == nil {
|
|
|
e.formatter = crud.formatter
|
|
|
} else {
|
|
|
- e.formatter = o.formatter
|
|
|
+ e.formatter = o.Formatter
|
|
|
}
|
|
|
crud.entityLocker.Lock()
|
|
|
crud.entities[e.stmt.Table+"@"+e.Module] = e
|
|
|
crud.entityLocker.Unlock()
|
|
|
e.naming.plural = inflection.Plural(e.stmt.Table)
|
|
|
e.naming.singular = inflection.Singular(e.stmt.Table)
|
|
|
- if o.scenarios == nil || len(o.scenarios) == 0 {
|
|
|
+ if o.Scenarios == nil || len(o.Scenarios) == 0 {
|
|
|
e.Scenarios = []string{ScenarioList, ScenarioView, ScenarioCreate, ScenarioUpdate, ScenarioDelete, ScenarioExport}
|
|
|
} else {
|
|
|
- e.Scenarios = o.scenarios
|
|
|
+ e.Scenarios = o.Scenarios
|
|
|
}
|
|
|
if v, ok := value.(Naming); ok {
|
|
|
e.naming.label = v.DisplayName()
|
|
|
} else {
|
|
|
e.naming.label = strings.Title(e.naming.singular)
|
|
|
}
|
|
|
- e.prefix = o.prefix
|
|
|
+ e.prefix = o.Prefix
|
|
|
if e.hasScenarios(ScenarioList) {
|
|
|
- e.Urls[ScenarioList] = o.prefix + "/" + e.naming.plural
|
|
|
- crud.httpSvr.Handle("GET", e.Urls[ScenarioList], e.actionIndex, o.middleware...)
|
|
|
+ e.Urls[ScenarioList] = o.Prefix + "/" + e.naming.plural
|
|
|
+ crud.httpSvr.Handle("GET", e.Urls[ScenarioList], e.actionIndex, o.Middleware...)
|
|
|
}
|
|
|
if e.hasScenarios(ScenarioView) {
|
|
|
- e.Urls[ScenarioView] = o.prefix + "/" + e.naming.singular + "/:id"
|
|
|
- crud.httpSvr.Handle("GET", e.Urls[ScenarioView], e.actionView, o.middleware...)
|
|
|
+ e.Urls[ScenarioView] = o.Prefix + "/" + e.naming.singular + "/:id"
|
|
|
+ crud.httpSvr.Handle("GET", e.Urls[ScenarioView], e.actionView, o.Middleware...)
|
|
|
}
|
|
|
if e.hasScenarios(ScenarioCreate) {
|
|
|
- e.Urls[ScenarioCreate] = o.prefix + "/" + e.naming.singular
|
|
|
- crud.httpSvr.Handle("POST", e.Urls[ScenarioCreate], e.actionCreate, o.middleware...)
|
|
|
+ e.Urls[ScenarioCreate] = o.Prefix + "/" + e.naming.singular
|
|
|
+ crud.httpSvr.Handle("POST", e.Urls[ScenarioCreate], e.actionCreate, o.Middleware...)
|
|
|
}
|
|
|
if e.hasScenarios(ScenarioUpdate) {
|
|
|
- e.Urls[ScenarioUpdate] = o.prefix + "/" + e.naming.singular + "/:id"
|
|
|
- crud.httpSvr.Handle("PUT", e.Urls[ScenarioUpdate], e.actionUpdate, o.middleware...)
|
|
|
+ e.Urls[ScenarioUpdate] = o.Prefix + "/" + e.naming.singular + "/:id"
|
|
|
+ crud.httpSvr.Handle("PUT", e.Urls[ScenarioUpdate], e.actionUpdate, o.Middleware...)
|
|
|
}
|
|
|
if e.hasScenarios(ScenarioDelete) {
|
|
|
- e.Urls[ScenarioDelete] = o.prefix + "/" + e.naming.singular + "/:id"
|
|
|
- crud.httpSvr.Handle("DELETE", e.Urls[ScenarioDelete], e.actionDelete, o.middleware...)
|
|
|
+ e.Urls[ScenarioDelete] = o.Prefix + "/" + e.naming.singular + "/:id"
|
|
|
+ crud.httpSvr.Handle("DELETE", e.Urls[ScenarioDelete], e.actionDelete, o.Middleware...)
|
|
|
}
|
|
|
if e.hasScenarios(ScenarioExport) {
|
|
|
- e.Urls[ScenarioExport] = o.prefix + "/" + e.naming.singular + "-export"
|
|
|
- crud.httpSvr.Handle("GET", e.Urls[ScenarioExport], e.actionExport, o.middleware...)
|
|
|
+ e.Urls[ScenarioExport] = o.Prefix + "/" + e.naming.singular + "-export"
|
|
|
+ crud.httpSvr.Handle("GET", e.Urls[ScenarioExport], e.actionExport, o.Middleware...)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -125,6 +125,10 @@ func (crud *CRUD) SetDB(db *gorm.DB) {
|
|
|
crud.db = db
|
|
|
}
|
|
|
|
|
|
+func (crud *CRUD) GetDB() *gorm.DB {
|
|
|
+ return crud.db
|
|
|
+}
|
|
|
+
|
|
|
func (crud *CRUD) SetHttpServer(svr *http.Server) {
|
|
|
crud.httpSvr = svr
|
|
|
crud.once.Do(func() {
|
|
@@ -165,7 +169,7 @@ func (crud *CRUD) actionCatalog(c *http.Context) (err error) {
|
|
|
sb.WriteByte('\n')
|
|
|
es := crud.readSnapshot()
|
|
|
for _, e := range es {
|
|
|
- sb.WriteString("* [" + e.naming.label + "接口](/api/crud/doc/" + e.stmt.Table + "@" + e.Module + ")")
|
|
|
+ sb.WriteString("* [" + e.naming.label + "接口](/crud/doc/" + e.stmt.Table + "@" + e.Module + ")")
|
|
|
sb.WriteByte('\n')
|
|
|
}
|
|
|
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
|