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") }) }