package chapters 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 var storyCollection *mgo.Collection func find(query interface{}) (*models.Chapter, error) { chapter := models.Chapter{} err := collection.Find(query).One(&chapter) if err != nil { return nil, err } return &chapter, err } func list(query interface{}) ([]models.Chapter, error) { chapters := make([]models.Chapter, 0, 8) err := collection.Find(query).Sort("createdDate").All(&chapters) if err != nil { return nil, err } return chapters, nil } func makeChapterID() string { result := "SC" offset := 0 data := make([]byte, 32) rand.Read(data) for len(result) < 24 { result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36) offset += 8 if offset >= 32 { rand.Read(data) offset = 0 } } return result[:24] } func init() { store.HandleInit(func(db *mgo.Database) { collection = db.C("story.chapters") storyCollection = db.C("story.stories") collection.EnsureIndexKey("storyId") collection.EnsureIndexKey("author") collection.EnsureIndexKey("createdDate") }) }