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.

56 lines
1.2 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. }
  14. // List lists all characters
  15. func List(filter *Filter) ([]models.Character, error) {
  16. query := bson.M{}
  17. if filter != nil {
  18. if len(filter.IDs) > 1 {
  19. query["_id"] = bson.M{"$in": filter.IDs}
  20. } else if len(filter.IDs) == 1 {
  21. query["_id"] = filter.IDs[0]
  22. }
  23. if len(filter.Nicks) == 1 {
  24. query["nicks"] = filter.Nicks[0]
  25. } else if filter.Nicks != nil {
  26. query["nicks"] = bson.M{"$in": filter.Nicks}
  27. }
  28. if len(filter.Names) > 1 {
  29. query["$or"] = bson.M{
  30. "name": bson.M{"$in": filter.Names},
  31. "shortName": bson.M{"$in": filter.Names},
  32. }
  33. } else if len(filter.Names) == 1 {
  34. query["$or"] = bson.M{
  35. "name": filter.Names[0],
  36. "shortName": filter.Names[0],
  37. }
  38. }
  39. if filter.Author != nil {
  40. query["author"] = *filter.Author
  41. }
  42. if filter.Search != nil {
  43. query["$text"] = bson.M{"$search": *filter.Search}
  44. }
  45. }
  46. return list(query)
  47. }