@@ -0,0 +1,104 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"git.aiterp.net/rpdata/api/repositories"
|
||||
"github.com/globalsign/mgo"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type changeRepository struct {
|
||||
changes *mgo.Collection
|
||||
idCounter *counter
|
||||
}
|
||||
|
||||
func (r *changeRepository) Find(ctx context.Context, id string) (*models.Change, error) {
|
||||
change := new(models.Change)
|
||||
err := r.changes.FindId(id).One(change)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return change, nil
|
||||
}
|
||||
|
||||
func (r *changeRepository) List(ctx context.Context, filter models.ChangeFilter) ([]*models.Change, error) {
|
||||
query := bson.M{}
|
||||
limit := 0
|
||||
if filter.EarliestDate != nil && !filter.EarliestDate.IsZero() {
|
||||
query["date"] = bson.M{"$gte": *filter.EarliestDate}
|
||||
}
|
||||
if len(filter.Keys) > 0 {
|
||||
query["keys"] = bson.M{"$in": filter.Keys}
|
||||
}
|
||||
if filter.Author != nil && *filter.Author != "" {
|
||||
query["author"] = *filter.Author
|
||||
}
|
||||
if filter.Limit != nil {
|
||||
limit = *filter.Limit
|
||||
}
|
||||
|
||||
initialSize := 64
|
||||
if limit > 0 && limit < 256 {
|
||||
initialSize = limit
|
||||
}
|
||||
|
||||
changes := make([]*models.Change, 0, initialSize)
|
||||
err := r.changes.Find(query).All(&changes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
func (r *changeRepository) Insert(ctx context.Context, change models.Change) (*models.Change, error) {
|
||||
next, err := r.idCounter.Increment(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
change.ID = "Change_" + strconv.Itoa(next)
|
||||
|
||||
err = r.changes.Insert(change)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &change, nil
|
||||
}
|
||||
|
||||
func (r *changeRepository) Remove(ctx context.Context, change models.Change) error {
|
||||
return r.changes.RemoveId(change.ID)
|
||||
}
|
||||
|
||||
func newChangeRepository(db *mgo.Database) (repositories.ChangeRepository, error) {
|
||||
collection := db.C("common.changes")
|
||||
|
||||
err := collection.EnsureIndex(mgo.Index{
|
||||
Name: "expiry",
|
||||
Key: []string{"date"},
|
||||
ExpireAfter: time.Hour * 2400, // 100 days
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = collection.EnsureIndexKey("author")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = collection.EnsureIndexKey("keys")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &changeRepository{
|
||||
changes: collection,
|
||||
idCounter: newCounter(db, "auto_increment", "Change"),
|
||||
}, nil
|
||||
}
|
||||
@@ -38,8 +38,15 @@ func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() erro
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
changes, err := newChangeRepository(db)
|
||||
if err != nil {
|
||||
session.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
bundle = &repositories.Bundle{
|
||||
Characters: characters,
|
||||
Changes: changes,
|
||||
Tags: newTagRepository(db),
|
||||
}
|
||||
|
||||
@@ -73,6 +80,22 @@ func (c *counter) WithName(name string) *counter {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *counter) WithCategory(category string) *counter {
|
||||
return &counter{
|
||||
coll: c.coll,
|
||||
category: category,
|
||||
name: c.name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *counter) With(category, name string) *counter {
|
||||
return &counter{
|
||||
coll: c.coll,
|
||||
category: category,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *counter) Increment(amount int) (int, error) {
|
||||
type counterDoc struct {
|
||||
ID string `bson:"_id"`
|
||||
|
||||
Reference in New Issue
Block a user