GraphQL API and utilities for the rpdata project
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.

36 lines
776 B

  1. package chapters
  2. import (
  3. "git.aiterp.net/rpdata/api/internal/store"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo"
  6. )
  7. var collection *mgo.Collection
  8. func find(query interface{}) (models.Chapter, error) {
  9. chapter := models.Chapter{}
  10. err := collection.Find(query).One(&chapter)
  11. return chapter, err
  12. }
  13. func list(query interface{}) ([]models.Chapter, error) {
  14. chapters := make([]models.Chapter, 0, 8)
  15. err := collection.Find(query).Sort("createdDate").All(&chapters)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return chapters, nil
  20. }
  21. func init() {
  22. store.HandleInit(func(db *mgo.Database) {
  23. collection = db.C("story.chapters")
  24. collection.EnsureIndexKey("storyId")
  25. collection.EnsureIndexKey("author")
  26. collection.EnsureIndexKey("createdDate")
  27. })
  28. }