package stories import ( "time" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Filter for stories.List type Filter struct { Author *string Tags []models.Tag EarliestFictionalDate time.Time LatestFictionalDate time.Time Category *models.StoryCategory Open *bool Unlisted *bool Limit int } // List lists stories by any non-zero criteria passed with it. func List(filter *Filter) ([]models.Story, error) { query := bson.M{"listed": true} limit := 0 if filter != nil { if filter.Author != nil { query["author"] = *filter.Author } if len(filter.Tags) > 0 { query["tags"] = bson.M{"$in": filter.Tags} } if !filter.EarliestFictionalDate.IsZero() && !filter.LatestFictionalDate.IsZero() { query["fictionalDate"] = bson.M{ "$gte": filter.EarliestFictionalDate, "$lt": filter.LatestFictionalDate, } } else if !filter.LatestFictionalDate.IsZero() { query["fictionalDate"] = bson.M{ "$lt": filter.LatestFictionalDate, } } else if !filter.EarliestFictionalDate.IsZero() { query["fictionalDate"] = bson.M{ "$gte": filter.EarliestFictionalDate, } } if filter.Category != nil { query["category"] = *filter.Category } if filter.Open != nil { query["open"] = *filter.Open } if filter.Unlisted != nil { query["listed"] = !*filter.Unlisted } limit = filter.Limit } return list(query, limit) }