Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
89f1535ecd
|
|||
|
7ad97385db
|
|||
|
aa8a1fa46f
|
|||
|
211394c1f1
|
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module gitbase.de/gopackage/mgoapi/v2
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.12
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.16
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-playground/validator/v10 v10.10.0 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@@ -1,7 +1,5 @@
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.11 h1:cti/2qxVXGJBYa/9Fb5gITVAQ2DgjBgEPHZEAYX32Bw=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.11/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.16 h1:pymLJLAaISj3dA3UNNcHnMaEDGrolPjF+lkn8xflgAk=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.16/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
|
||||
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.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
||||
2
hooks.go
2
hooks.go
@@ -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 {
|
||||
|
||||
17
register.go
17
register.go
@@ -12,6 +12,7 @@ type API struct {
|
||||
routerGroup *gin.RouterGroup
|
||||
jwtSecret []byte
|
||||
authenticationHeader string
|
||||
modelRegistry []mgocrud.ModelInterface
|
||||
}
|
||||
|
||||
// Context is gin.Context with extras
|
||||
@@ -23,19 +24,19 @@ type Context struct {
|
||||
|
||||
// New returns new instance of the API
|
||||
func New(connection mgocrud.Connection, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
modelRegistry := make([]mgocrud.ModelInterface, 0)
|
||||
return &API{
|
||||
DBConnection: connection,
|
||||
DBName: dbname,
|
||||
routerGroup: routerGroup,
|
||||
DBConnection: connection,
|
||||
DBName: dbname,
|
||||
routerGroup: routerGroup,
|
||||
modelRegistry: modelRegistry,
|
||||
}
|
||||
}
|
||||
|
||||
var modelRegistry = make([]mgocrud.ModelInterface, 0)
|
||||
|
||||
// RegisterModel setups gin routes for model GET, POST, PUT and DELETE
|
||||
func (api *API) RegisterModel(m mgocrud.ModelInterface) {
|
||||
|
||||
modelRegistry = append(modelRegistry, m)
|
||||
api.modelRegistry = append(api.modelRegistry, m)
|
||||
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
@@ -75,7 +76,7 @@ func (api *API) AddMetaRoute(route string) {
|
||||
api.routerGroup.GET(route, func(c *gin.Context) {
|
||||
modelMeta := make(map[string]interface{})
|
||||
|
||||
for _, m := range modelRegistry {
|
||||
for _, m := range api.modelRegistry {
|
||||
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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user