2 Commits

Author SHA1 Message Date
7ad97385db ValidateObject not in interface 2022-02-14 16:04:27 +01:00
aa8a1fa46f modelRegistry per api 2022-02-14 14:45:43 +01:00
4 changed files with 13 additions and 16 deletions

2
go.mod
View File

@@ -3,7 +3,7 @@ module gitbase.de/gopackage/mgoapi/v2
go 1.16 go 1.16
require ( require (
gitbase.de/gopackage/mgocrud/v2 v2.0.13 gitbase.de/gopackage/mgocrud/v2 v2.0.15
github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.7.7 github.com/gin-gonic/gin v1.7.7
github.com/go-playground/validator/v10 v10.10.0 // indirect github.com/go-playground/validator/v10 v10.10.0 // indirect

8
go.sum
View File

@@ -1,9 +1,5 @@
gitbase.de/gopackage/mgocrud/v2 v2.0.11 h1:cti/2qxVXGJBYa/9Fb5gITVAQ2DgjBgEPHZEAYX32Bw= gitbase.de/gopackage/mgocrud/v2 v2.0.15 h1:zC+j7zFENAtdkhgjr2nPV05aAeoSNxT6eW51ZojQmbo=
gitbase.de/gopackage/mgocrud/v2 v2.0.11/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM= gitbase.de/gopackage/mgocrud/v2 v2.0.15/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
gitbase.de/gopackage/mgocrud/v2 v2.0.12 h1:7Z3+uNfw0uLUeHsBSBmiqtLaBPfwCE1B9tuwBLgnlt0=
gitbase.de/gopackage/mgocrud/v2 v2.0.12/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
gitbase.de/gopackage/mgocrud/v2 v2.0.13 h1:zPVE2gsoxcd1i2liC6BcMq2DFVYc6873hEe9Fn9rqcI=
gitbase.de/gopackage/mgocrud/v2 v2.0.13/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@@ -71,7 +71,7 @@ func validateObject(c *Context, m mgocrud.ModelInterface, changes bson.M) error
} }
} }
return c.DB.ValidateObject(m, changes) return mgocrud.ValidateObject(c.DB, m, changes)
} }
func savedObject(c *Context, m mgocrud.ModelInterface, changes bson.M) error { func savedObject(c *Context, m mgocrud.ModelInterface, changes bson.M) error {

View File

@@ -12,6 +12,7 @@ type API struct {
routerGroup *gin.RouterGroup routerGroup *gin.RouterGroup
jwtSecret []byte jwtSecret []byte
authenticationHeader string authenticationHeader string
modelRegistry []mgocrud.ModelInterface
} }
// Context is gin.Context with extras // Context is gin.Context with extras
@@ -23,19 +24,19 @@ type Context struct {
// New returns new instance of the API // New returns new instance of the API
func New(connection mgocrud.Connection, dbname string, routerGroup *gin.RouterGroup) *API { func New(connection mgocrud.Connection, dbname string, routerGroup *gin.RouterGroup) *API {
modelRegistry := make([]mgocrud.ModelInterface, 0)
return &API{ return &API{
DBConnection: connection, DBConnection: connection,
DBName: dbname, DBName: dbname,
routerGroup: routerGroup, routerGroup: routerGroup,
modelRegistry: modelRegistry,
} }
} }
var modelRegistry = make([]mgocrud.ModelInterface, 0)
// RegisterModel setups gin routes for model GET, POST, PUT and DELETE // RegisterModel setups gin routes for model GET, POST, PUT and DELETE
func (api *API) RegisterModel(m mgocrud.ModelInterface) { func (api *API) RegisterModel(m mgocrud.ModelInterface) {
modelRegistry = append(modelRegistry, m) api.modelRegistry = append(api.modelRegistry, m)
session := api.DBConnection.NewSession() session := api.DBConnection.NewSession()
defer session.Close() defer session.Close()
@@ -75,7 +76,7 @@ func (api *API) AddMetaRoute(route string) {
api.routerGroup.GET(route, func(c *gin.Context) { api.routerGroup.GET(route, func(c *gin.Context) {
modelMeta := make(map[string]interface{}) modelMeta := make(map[string]interface{})
for _, m := range modelRegistry { for _, m := range api.modelRegistry {
modelMeta[mgocrud.GetCollectionName(m)] = getModelMeta(m) modelMeta[mgocrud.GetCollectionName(m)] = getModelMeta(m)
} }
@@ -84,7 +85,7 @@ func (api *API) AddMetaRoute(route string) {
}) })
}) })
for _, m := range modelRegistry { for _, m := range api.modelRegistry {
api.routerGroup.GET("/_meta/"+mgocrud.GetCollectionName(m), api.collectionGetMetaHandler(m)) api.routerGroup.GET("/_meta/"+mgocrud.GetCollectionName(m), api.collectionGetMetaHandler(m))
} }