Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
e574667a8c
|
|||
|
0a966a637b
|
|||
|
b59be1a9d7
|
@@ -1,65 +0,0 @@
|
||||
package mgoapi
|
||||
|
||||
import (
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type Session interface {
|
||||
Close()
|
||||
Copy() Session
|
||||
DB(name string) Database
|
||||
}
|
||||
|
||||
type Database interface {
|
||||
Session() Session
|
||||
C(name string) Collection
|
||||
Name() string
|
||||
EnsureIndex(m mgocrud.ModelInterface) error
|
||||
ValidateObject(m mgocrud.ModelInterface, changes bson.M) error
|
||||
ReadDocument(m mgocrud.ModelInterface, selector bson.M) error
|
||||
CreateDocument(m mgocrud.ModelInterface) error
|
||||
ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier mgocrud.PipelineModifierFunction) error
|
||||
ReadCollectionCount(m mgocrud.ModelInterface, filter bson.M) (count int, err error)
|
||||
UpdateDocument(m mgocrud.ModelInterface, changes bson.M) error
|
||||
UpsertDocument(m mgocrud.ModelInterface, changes bson.M) error
|
||||
DeleteDocument(m mgocrud.ModelInterface) error
|
||||
DeleteDocuments(m mgocrud.ModelInterface, filter bson.M) (removed int, err error)
|
||||
}
|
||||
|
||||
type Collection interface {
|
||||
Insert(docs ...interface{}) error
|
||||
UpdateId(id interface{}, update interface{}) error
|
||||
RemoveId(id interface{}) error
|
||||
Upsert(selector interface{}, update interface{}) (ChangeInfo, error)
|
||||
RemoveAll(filter interface{}) (ChangeInfo, error)
|
||||
FindId(id interface{}) Query
|
||||
Find(query interface{}) Query
|
||||
EnsureIndex(index Index) error
|
||||
Pipe(pipeline interface{}) Pipe
|
||||
}
|
||||
|
||||
type ChangeInfo interface {
|
||||
Matched() int
|
||||
Removed() int
|
||||
Updated() int
|
||||
}
|
||||
|
||||
type Query interface {
|
||||
Select(selector interface{}) Query
|
||||
One(result interface{}) error
|
||||
Sort(fields ...string) Query
|
||||
Skip(n int) Query
|
||||
Limit(n int) Query
|
||||
All(result interface{}) error
|
||||
Count() (int, error)
|
||||
}
|
||||
|
||||
type Index interface {
|
||||
NewMgoIndex(index mgo.Index) Index
|
||||
}
|
||||
|
||||
type Pipe interface {
|
||||
All(result interface{}) error
|
||||
}
|
||||
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.5
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.9
|
||||
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/v2 v2.0.5 h1:sgRJXw0ZvNDtsjmIhNDoe/8pO0Bed313aXFX6SlW6fQ=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.5/go.mod h1:eAIqxjo60/nP/S+YA25SBLQZ98WUrHwpvjE7Zl+ewTM=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.9 h1:tIg5XKJiEd/QEjY9zW5/OK0kl6+auhlEO0wmd0vR4Mk=
|
||||
gitbase.de/gopackage/mgocrud/v2 v2.0.9/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=
|
||||
|
||||
10
handler.go
10
handler.go
@@ -15,7 +15,7 @@ import (
|
||||
func (api *API) collectionGetOneHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
@@ -79,7 +79,7 @@ func (api *API) collectionGetHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
|
||||
results := x.Interface()
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
@@ -205,7 +205,7 @@ func (api *API) collectionPostHandler(m mgocrud.ModelInterface) gin.HandlerFunc
|
||||
return
|
||||
}
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
@@ -257,7 +257,7 @@ func (api *API) collectionPostHandler(m mgocrud.ModelInterface) gin.HandlerFunc
|
||||
|
||||
func (api *API) collectionPutHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
@@ -399,7 +399,7 @@ func (api *API) collectionPutHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
func (api *API) collectionDeleteHandler(m mgocrud.ModelInterface) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
|
||||
@@ -154,7 +154,7 @@ func getModelMeta(m mgocrud.ModelInterface) []map[string]interface{} {
|
||||
return getStructMeta(modelType, dontRecurse)
|
||||
}
|
||||
|
||||
func getDocument(c *gin.Context, db 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
|
||||
|
||||
5
login.go
5
login.go
@@ -5,6 +5,7 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"gitbase.de/gopackage/mgocrud/v2"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -12,7 +13,7 @@ import (
|
||||
|
||||
// LoginModel is interface for modules which can be used for the login route
|
||||
type LoginModel interface {
|
||||
LoginCheck(db Database) (tokenData interface{}, err error)
|
||||
LoginCheck(db mgocrud.Database) (tokenData interface{}, err error)
|
||||
LoginResponse(token string) (interface{}, error)
|
||||
}
|
||||
|
||||
@@ -26,7 +27,7 @@ func (api *API) loginPostHandler(m LoginModel) func(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
|
||||
10
register.go
10
register.go
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
// API is wrapper for one RouterGroup and mgo DB
|
||||
type API struct {
|
||||
DBSession Session
|
||||
DBConnection mgocrud.Connection
|
||||
DBName string
|
||||
routerGroup *gin.RouterGroup
|
||||
jwtSecret []byte
|
||||
@@ -18,13 +18,13 @@ type API struct {
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
API *API
|
||||
DB Database
|
||||
DB mgocrud.Database
|
||||
}
|
||||
|
||||
// New returns new instance of the API
|
||||
func New(session Session, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
func New(connection mgocrud.Connection, dbname string, routerGroup *gin.RouterGroup) *API {
|
||||
return &API{
|
||||
DBSession: session,
|
||||
DBConnection: connection,
|
||||
DBName: dbname,
|
||||
routerGroup: routerGroup,
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func (api *API) RegisterModel(m mgocrud.ModelInterface) {
|
||||
|
||||
modelRegistry = append(modelRegistry, m)
|
||||
|
||||
session := api.DBSession.Copy()
|
||||
session := api.DBConnection.NewSession()
|
||||
defer session.Close()
|
||||
db := session.DB(api.DBName)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user