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.

67 lines
1.5 KiB

  1. package stories
  2. import (
  3. "time"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Filter for stories.List
  8. type Filter struct {
  9. Author *string
  10. Tags []models.Tag
  11. EarliestFictionalDate time.Time
  12. LatestFictionalDate time.Time
  13. Category *models.StoryCategory
  14. Open *bool
  15. Unlisted *bool
  16. Limit int
  17. }
  18. // List lists stories by any non-zero criteria passed with it.
  19. func List(filter *Filter) ([]models.Story, error) {
  20. query := bson.M{"listed": true}
  21. limit := 0
  22. if filter != nil {
  23. if filter.Author != nil {
  24. query["author"] = *filter.Author
  25. }
  26. if len(filter.Tags) > 0 {
  27. query["tags"] = bson.M{"$in": filter.Tags}
  28. }
  29. if !filter.EarliestFictionalDate.IsZero() && !filter.LatestFictionalDate.IsZero() {
  30. query["fictionalDate"] = bson.M{
  31. "$gte": filter.EarliestFictionalDate,
  32. "$lt": filter.LatestFictionalDate,
  33. }
  34. } else if !filter.LatestFictionalDate.IsZero() {
  35. query["fictionalDate"] = bson.M{
  36. "$lt": filter.LatestFictionalDate,
  37. }
  38. } else if !filter.EarliestFictionalDate.IsZero() {
  39. query["fictionalDate"] = bson.M{
  40. "$gte": filter.EarliestFictionalDate,
  41. }
  42. }
  43. if filter.Category != nil {
  44. query["category"] = *filter.Category
  45. }
  46. if filter.Open != nil {
  47. query["open"] = *filter.Open
  48. }
  49. if filter.Unlisted != nil {
  50. query["listed"] = !*filter.Unlisted
  51. }
  52. limit = filter.Limit
  53. }
  54. return list(query, limit)
  55. }