|
|
@ -1,6 +1,7 @@ |
|
|
|
package mongodb |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"github.com/globalsign/mgo/bson" |
|
|
|
"time" |
|
|
@ -10,8 +11,33 @@ import ( |
|
|
|
"github.com/globalsign/mgo" |
|
|
|
) |
|
|
|
|
|
|
|
type MongoDB struct { |
|
|
|
session *mgo.Session |
|
|
|
|
|
|
|
changes repositories.ChangeRepository |
|
|
|
characters repositories.CharacterRepository |
|
|
|
tags repositories.TagRepository |
|
|
|
} |
|
|
|
|
|
|
|
func (m *MongoDB) Changes() repositories.ChangeRepository { |
|
|
|
return m.changes |
|
|
|
} |
|
|
|
|
|
|
|
func (m *MongoDB) Characters() repositories.CharacterRepository { |
|
|
|
return m.characters |
|
|
|
} |
|
|
|
|
|
|
|
func (m *MongoDB) Tags() repositories.TagRepository { |
|
|
|
return m.tags |
|
|
|
} |
|
|
|
|
|
|
|
func (m *MongoDB) Close(ctx context.Context) error { |
|
|
|
m.session.Close() |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// Init initializes the mongodb database
|
|
|
|
func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() error, err error) { |
|
|
|
func Init(cfg config.Database) (*MongoDB, error) { |
|
|
|
port := cfg.Port |
|
|
|
if port <= 0 { |
|
|
|
port = 27017 |
|
|
@ -27,7 +53,7 @@ func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() erro |
|
|
|
Source: cfg.Db, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
db := session.DB(cfg.Db) |
|
|
@ -35,27 +61,22 @@ func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() erro |
|
|
|
characters, err := newCharacterRepository(db) |
|
|
|
if err != nil { |
|
|
|
session.Close() |
|
|
|
return nil, nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
changes, err := newChangeRepository(db) |
|
|
|
if err != nil { |
|
|
|
session.Close() |
|
|
|
return nil, nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
bundle = &repositories.Bundle{ |
|
|
|
Characters: characters, |
|
|
|
Changes: changes, |
|
|
|
Tags: newTagRepository(db), |
|
|
|
} |
|
|
|
|
|
|
|
closeFn = func() error { |
|
|
|
session.Close() |
|
|
|
return nil |
|
|
|
} |
|
|
|
return &MongoDB{ |
|
|
|
session: session, |
|
|
|
|
|
|
|
return |
|
|
|
changes: changes, |
|
|
|
characters: characters, |
|
|
|
tags: newTagRepository(db), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|
|
|
|
type counter struct { |
|
|
|