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.

61 lines
1.3 KiB

  1. package characters
  2. import (
  3. "git.aiterp.net/rpdata/api/models"
  4. "github.com/globalsign/mgo/bson"
  5. )
  6. // Filter is used to filter the list of characters
  7. type Filter struct {
  8. IDs []string `json:"ids"`
  9. Nicks []string `json:"nicks"`
  10. Names []string `json:"names"`
  11. Author *string `json:"author"`
  12. Search *string `json:"search"`
  13. Logged *bool `json:"logged"`
  14. }
  15. // List lists all characters
  16. func List(filter *Filter) ([]models.Character, error) {
  17. query := bson.M{}
  18. if filter != nil {
  19. if len(filter.IDs) > 1 {
  20. query["_id"] = bson.M{"$in": filter.IDs}
  21. } else if len(filter.IDs) == 1 {
  22. query["_id"] = filter.IDs[0]
  23. }
  24. if len(filter.Nicks) > 1 {
  25. query["nicks"] = bson.M{"$in": filter.Nicks}
  26. } else if len(filter.Nicks) == 1 {
  27. query["nicks"] = filter.Nicks[0]
  28. }
  29. if len(filter.Names) > 1 {
  30. query["$or"] = bson.M{
  31. "name": bson.M{"$in": filter.Names},
  32. "shortName": bson.M{"$in": filter.Names},
  33. }
  34. } else if len(filter.Names) == 1 {
  35. query["$or"] = bson.M{
  36. "name": filter.Names[0],
  37. "shortName": filter.Names[0],
  38. }
  39. }
  40. if filter.Logged != nil {
  41. query["logged"] = *filter.Logged
  42. }
  43. if filter.Author != nil {
  44. query["author"] = *filter.Author
  45. }
  46. if filter.Search != nil {
  47. query["$text"] = bson.M{"$search": *filter.Search}
  48. }
  49. }
  50. return list(query)
  51. }