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.

57 lines
1.0 KiB

  1. package posts
  2. import (
  3. "strings"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Filter is used to generate a query to the database.
  8. type Filter struct {
  9. ID []string
  10. Kind []string
  11. LogID *string
  12. Search *string
  13. Limit int
  14. }
  15. // List lists the posts according to the filter
  16. func List(filter *Filter) ([]models.Post, error) {
  17. mutex.RLock()
  18. defer mutex.RUnlock()
  19. limit := 256
  20. query := bson.M{}
  21. if filter != nil {
  22. if filter.LogID != nil {
  23. query["logId"] = filter.LogID
  24. }
  25. if len(filter.ID) > 1 {
  26. query["_id"] = bson.M{"$in": filter.ID}
  27. } else if len(filter.ID) == 1 {
  28. query["_id"] = filter.ID[0]
  29. }
  30. if len(filter.Kind) > 1 {
  31. for i := range filter.Kind {
  32. filter.Kind[i] = strings.ToLower(filter.Kind[i])
  33. }
  34. query["kind"] = bson.M{"$in": filter.Kind}
  35. } else if len(filter.Kind) == 1 {
  36. query["kind"] = strings.ToLower(filter.Kind[0])
  37. }
  38. limit = filter.Limit
  39. }
  40. posts, err := list(query, limit, "position")
  41. if err != nil {
  42. return nil, err
  43. }
  44. return posts, nil
  45. }