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.

55 lines
1.2 KiB

  1. package characters
  2. import (
  3. "log"
  4. "git.aiterp.net/rpdata/api/internal/store"
  5. "git.aiterp.net/rpdata/api/models"
  6. "github.com/globalsign/mgo"
  7. )
  8. var collection *mgo.Collection
  9. func find(query interface{}) (models.Character, error) {
  10. character := models.Character{}
  11. err := collection.Find(query).One(&character)
  12. if err != nil {
  13. return models.Character{}, err
  14. }
  15. return character, nil
  16. }
  17. func list(query interface{}) ([]models.Character, error) {
  18. characters := make([]models.Character, 0, 64)
  19. err := collection.Find(query).All(&characters)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return characters, nil
  24. }
  25. func init() {
  26. store.HandleInit(func(db *mgo.Database) {
  27. collection = db.C("common.characters")
  28. collection.EnsureIndexKey("name")
  29. collection.EnsureIndexKey("shortName")
  30. collection.EnsureIndexKey("author")
  31. err := collection.EnsureIndex(mgo.Index{
  32. Key: []string{"nicks"},
  33. Unique: true,
  34. DropDups: true,
  35. })
  36. if err != nil {
  37. log.Fatalln("init common.characters:", err)
  38. }
  39. err = collection.EnsureIndex(mgo.Index{
  40. Key: []string{"$text:description"},
  41. })
  42. if err != nil {
  43. log.Fatalln("init common.characters:", err)
  44. }
  45. })
  46. }