You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
package stories
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"strconv"
|
|
|
|
"git.aiterp.net/rpdata/api/internal/store"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"github.com/globalsign/mgo"
|
|
)
|
|
|
|
var collection *mgo.Collection
|
|
|
|
func find(query interface{}) (models.Story, error) {
|
|
story := models.Story{}
|
|
err := collection.Find(query).One(&story)
|
|
|
|
return story, err
|
|
}
|
|
|
|
func list(query interface{}, limit int) ([]models.Story, error) {
|
|
stories := make([]models.Story, 0, 64)
|
|
err := collection.Find(query).Limit(limit).Sort("-updatedDate").All(&stories)
|
|
|
|
return stories, err
|
|
}
|
|
|
|
// makeStoryID makes a random story ID that's 16 characters long
|
|
func makeStoryID() string {
|
|
result := "S"
|
|
offset := 0
|
|
data := make([]byte, 32)
|
|
|
|
rand.Read(data)
|
|
for len(result) < 16 {
|
|
result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
|
|
offset += 8
|
|
|
|
if offset >= 32 {
|
|
rand.Read(data)
|
|
offset = 0
|
|
}
|
|
}
|
|
|
|
return result[:16]
|
|
}
|
|
|
|
func init() {
|
|
store.HandleInit(func(db *mgo.Database) {
|
|
collection = db.C("story.stories")
|
|
|
|
collection.EnsureIndexKey("tags")
|
|
collection.EnsureIndexKey("author")
|
|
collection.EnsureIndexKey("updatedDate")
|
|
collection.EnsureIndexKey("fictionalDate")
|
|
collection.EnsureIndexKey("listed")
|
|
})
|
|
}
|