models: Painstakingly converted half the model package to a models & repostiry stucture. All binaries are probably dead at this point.
the build failed

This commit is contained in:
2018-09-16 00:13:12 +02:00
parent 2b05d90f4a
commit 0768cfd0bd
38 changed files with 878 additions and 64 deletions
+56
View File
@@ -0,0 +1,56 @@
package posts
import (
"log"
"sync"
"git.aiterp.net/rpdata/api/internal/store"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo"
)
var collection *mgo.Collection
var mutex sync.RWMutex
func find(query interface{}) (models.Post, error) {
post := models.Post{}
err := collection.Find(query).One(&post)
if err != nil {
return models.Post{}, err
}
return post, nil
}
func list(query interface{}, limit int, sort ...string) ([]models.Post, error) {
size := 64
if limit > 0 {
size = limit
}
posts := make([]models.Post, 0, size)
err := collection.Find(query).Limit(limit).Sort(sort...).All(&posts)
if err != nil {
return nil, err
}
return posts, nil
}
func init() {
store.HandleInit(func(db *mgo.Database) {
collection = db.C("logbot3.posts")
collection.EnsureIndexKey("logId")
collection.EnsureIndexKey("time")
collection.EnsureIndexKey("kind")
collection.EnsureIndexKey("position")
err := collection.EnsureIndex(mgo.Index{
Key: []string{"$text:text"},
})
if err != nil {
log.Fatalln("init logbot3.logs:", err)
}
})
}
+14
View File
@@ -0,0 +1,14 @@
package posts
import (
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// FindID finds a log post by ID.
func FindID(id string) (models.Post, error) {
mutex.RLock()
defer mutex.RUnlock()
return find(bson.M{"_id": id})
}
+57
View File
@@ -0,0 +1,57 @@
package posts
import (
"strings"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// Filter is used to generate a query to the database.
type Filter struct {
ID []string
Kind []string
LogID *string
Search *string
Limit int
}
// List lists the posts according to the filter
func List(filter *Filter) ([]models.Post, error) {
mutex.RLock()
defer mutex.RUnlock()
limit := 256
query := bson.M{}
if filter != nil {
if filter.LogID != nil {
query["logId"] = filter.LogID
}
if len(filter.ID) > 1 {
query["_id"] = bson.M{"$in": filter.ID}
} else if len(filter.ID) == 1 {
query["_id"] = filter.ID[0]
}
if len(filter.Kind) > 1 {
for i := range filter.Kind {
filter.Kind[i] = strings.ToLower(filter.Kind[i])
}
query["kind"] = bson.M{"$in": filter.Kind}
} else if len(filter.Kind) == 1 {
query["kind"] = strings.ToLower(filter.Kind[0])
}
limit = filter.Limit
}
posts, err := list(query, limit, "position")
if err != nil {
return nil, err
}
return posts, nil
}
+1
View File
@@ -0,0 +1 @@
package posts