Browse Source

修复文档错误

lxg 4 years ago
parent
commit
786c845185
3 changed files with 46 additions and 38 deletions
  1. 25 21
      crud/crud.go
  2. 12 12
      crud/options.go
  3. 9 5
      rest.go

+ 25 - 21
crud/crud.go

@@ -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")

+ 12 - 12
crud/options.go

@@ -21,13 +21,13 @@ type Callbacks struct {
 
 type Options struct {
 	ctx        context.Context
-	formatter  *Formatter
-	prefix     string
 	enable     *int32
-	db         *gorm.DB
-	callbacks  Callbacks
-	middleware []http.Middleware
-	scenarios  []string
+	DB         *gorm.DB
+	Formatter  *Formatter
+	Prefix     string
+	Callbacks  Callbacks
+	Middleware []http.Middleware
+	Scenarios  []string
 }
 
 type Option func(o *Options)
@@ -40,31 +40,31 @@ func WithContext(ctx context.Context) Option {
 
 func WithMiddleware(middleware ...http.Middleware) Option {
 	return func(o *Options) {
-		o.middleware = middleware
+		o.Middleware = middleware
 	}
 }
 
 func WithFormatter(formatter *Formatter) Option {
 	return func(o *Options) {
-		o.formatter = formatter
+		o.Formatter = formatter
 	}
 }
 
 func WithScenarios(ss []string) Option {
 	return func(o *Options) {
-		o.scenarios = ss
+		o.Scenarios = ss
 	}
 }
 
 func WithDB(db *gorm.DB) Option {
 	return func(o *Options) {
-		o.db = db
+		o.DB = db
 	}
 }
 
 func WithPrefix(prefix string) Option {
 	return func(o *Options) {
-		o.prefix = prefix
+		o.Prefix = prefix
 	}
 }
 
@@ -76,6 +76,6 @@ func WithEnable(enable *int32) Option {
 
 func WithCallback(cbs Callbacks) Option {
 	return func(o *Options) {
-		o.callbacks = cbs
+		o.Callbacks = cbs
 	}
 }

+ 9 - 5
rest.go

@@ -13,16 +13,20 @@ var (
 	once sync.Once
 )
 
-//register model instance
-func RegisterModel(db *gorm.DB, svr *http.Server, module string, value interface{}, opts ...crud.Option) (err error) {
+func Initialize(hs *http.Server, db *gorm.DB) {
+	var err error
 	once.Do(func() {
-		if err = schema.Initialize(context.Background(), db, svr); err != nil {
+		if err = schema.Initialize(context.Background(), db, hs); err != nil {
 			return
 		}
 		crud.Default.SetDB(db)
-		crud.Default.SetHttpServer(svr)
+		crud.Default.SetHttpServer(hs)
 	})
-	if err = schema.Migrate(module, db, value); err == nil {
+}
+
+//register model instance
+func RegisterModel(module string, value interface{}, opts ...crud.Option) (err error) {
+	if err = schema.Migrate(module, crud.Default.GetDB(), value); err == nil {
 		crud.Default.Register(module, value, opts...)
 	}
 	return