Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
119243297e
|
@@ -162,7 +162,7 @@ func _validateFilter(structT reflect.Type, f bson.M, checkValues bool) (bson.M,
|
||||
valT := reflect.TypeOf(val)
|
||||
if valT.Kind() == reflect.Slice {
|
||||
l := len(val.([]interface{}))
|
||||
newVal = make([]interface{}, l, l)
|
||||
newVal = make([]interface{}, l)
|
||||
for i := 0; i < l; i++ {
|
||||
var err error
|
||||
switch valF := val.([]interface{})[i].(type) {
|
||||
|
||||
4
go.mod
4
go.mod
@@ -1,9 +1,9 @@
|
||||
module gitbase.de/gopackage/mgoapi
|
||||
module gitbase.de/gopackage/mgoapi/v2
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
gitbase.de/gopackage/mgocrud v0.0.0-20210301125326-161e3b46fb99
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.3
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/gin-gonic/gin v1.6.3
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
gitbase.de/gopackage/mgocrud v0.0.0-20210301125326-161e3b46fb99 h1:2PovCTzQpTTmgxKdO915G+JmQPFYY6NKRZMKxw9jSYE=
|
||||
gitbase.de/gopackage/mgocrud v0.0.0-20210301125326-161e3b46fb99/go.mod h1:obb5Piqrb7N1VnmPp8HHJsAcOqIn9l04ibvH6I2qkhg=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.3 h1:uWNi5YX9eo1tdys3XZGKVvEjQ7fv14rQ3PNgNlR9XDs=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.3/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
|
||||
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
||||
@@ -7,9 +7,8 @@ import (
|
||||
|
||||
"runtime/debug"
|
||||
|
||||
"gitbase.de/gopackage/mgocrud"
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
@@ -42,7 +41,7 @@ func (api *API) collectionGetOneHandler(m mgocrud.ModelInterface) gin.HandlerFun
|
||||
newM, err := getDocument(c, db, m, validSelect)
|
||||
if err != nil {
|
||||
status := 500
|
||||
if err == mgo.ErrNotFound {
|
||||
if err == mgocrud.ErrNotFound {
|
||||
status = 404
|
||||
}
|
||||
c.JSON(status, errorObject(err))
|
||||
@@ -265,7 +264,7 @@ func (api *API) collectionPutHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
orgM, err := getDocument(c, db, m, nil)
|
||||
if err != nil {
|
||||
status := 500
|
||||
if err == mgo.ErrNotFound {
|
||||
if err == mgocrud.ErrNotFound {
|
||||
status = 404
|
||||
}
|
||||
c.JSON(status, errorObject(err))
|
||||
@@ -407,7 +406,7 @@ func (api *API) collectionDeleteHandler(m mgocrud.ModelInterface) gin.HandlerFun
|
||||
orgM, err := getDocument(c, db, m, bson.M{"_id": 1})
|
||||
if err != nil {
|
||||
status := 500
|
||||
if err == mgo.ErrNotFound {
|
||||
if err == mgocrud.ErrNotFound {
|
||||
status = 404
|
||||
}
|
||||
c.JSON(status, errorObject(err))
|
||||
|
||||
13
helper.go
13
helper.go
@@ -6,9 +6,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gitbase.de/gopackage/mgocrud"
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
@@ -41,7 +40,7 @@ func string2ObjectID(id string) (objectID *bson.ObjectId, err error) {
|
||||
case error:
|
||||
err = x
|
||||
default:
|
||||
err = errors.New("Unknown panic in: string2ObjectID")
|
||||
err = errors.New("unknown panic in: string2ObjectID")
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -72,9 +71,7 @@ func getStructMeta(s reflect.Type, dontRecurse map[string]bool) []map[string]int
|
||||
if f.Anonymous {
|
||||
// embed directly
|
||||
embed := getStructMeta(_type, dontRecurse)
|
||||
for _, e := range embed {
|
||||
meta = append(meta, e)
|
||||
}
|
||||
meta = append(meta, embed...)
|
||||
} else {
|
||||
fMeta := make(map[string]interface{})
|
||||
|
||||
@@ -89,7 +86,7 @@ func getStructMeta(s reflect.Type, dontRecurse map[string]bool) []map[string]int
|
||||
fType = _type.String()
|
||||
}
|
||||
//fType = strings.Replace(fType, "*", "", -1)
|
||||
fType = regexp.MustCompile("\\*[a-zA-Z0-9]*\\.?").ReplaceAllString(fType, "")
|
||||
fType = regexp.MustCompile(`\*[a-zA-Z0-9]*\.?`).ReplaceAllString(fType, "")
|
||||
fMeta["type"] = fType
|
||||
kind = _type.Kind()
|
||||
fMeta["kind"] = kind.String()
|
||||
@@ -157,7 +154,7 @@ func getModelMeta(m mgocrud.ModelInterface) []map[string]interface{} {
|
||||
return getStructMeta(modelType, dontRecurse)
|
||||
}
|
||||
|
||||
func getDocument(c *gin.Context, db *mgo.Database, m mgocrud.ModelInterface, selector bson.M) (mgocrud.ModelInterface, error) {
|
||||
func getDocument(c *gin.Context, db *mgocrud.Database, m mgocrud.ModelInterface, selector bson.M) (mgocrud.ModelInterface, error) {
|
||||
objectID, err := string2ObjectID(c.Param("id"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
2
hooks.go
2
hooks.go
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"gitbase.de/gopackage/mgocrud"
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
|
||||
4
login.go
4
login.go
@@ -5,15 +5,15 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
// LoginModel is interface for modules which can be used for the login route
|
||||
type LoginModel interface {
|
||||
LoginCheck(db *mgo.Database) (tokenData interface{}, err error)
|
||||
LoginCheck(db *mgocrud.Database) (tokenData interface{}, err error)
|
||||
LoginResponse(token string) (interface{}, error)
|
||||
}
|
||||
|
||||
|
||||
11
register.go
11
register.go
@@ -1,14 +1,13 @@
|
||||
package mgoapi
|
||||
|
||||
import (
|
||||
"gitbase.de/gopackage/mgocrud"
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
// API is wrapper for one RouterGroup and mgo DB
|
||||
type API struct {
|
||||
DBSession *mgo.Session
|
||||
DBSession *mgocrud.Session
|
||||
DBName string
|
||||
routerGroup *gin.RouterGroup
|
||||
jwtSecret []byte
|
||||
@@ -19,11 +18,11 @@ type API struct {
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
API *API
|
||||
DB *mgo.Database
|
||||
DB *mgocrud.Database
|
||||
}
|
||||
|
||||
// New returns new instance of the API
|
||||
func New(session *mgo.Session, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
func New(session *mgocrud.Session, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
return &API{
|
||||
DBSession: session,
|
||||
DBName: dbname,
|
||||
@@ -31,7 +30,7 @@ func New(session *mgo.Session, dbname string, routerGroup *gin.RouterGroup) *API
|
||||
}
|
||||
}
|
||||
|
||||
var modelRegistry = make([]mgocrud.ModelInterface, 0, 0)
|
||||
var modelRegistry = make([]mgocrud.ModelInterface, 0)
|
||||
|
||||
// RegisterModel setups gin routes for model GET, POST, PUT and DELETE
|
||||
func (api *API) RegisterModel(m mgocrud.ModelInterface) {
|
||||
|
||||
Reference in New Issue
Block a user