package posts import ( "strings" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Filter is used to generate a query to the database. type Filter struct { ID []string Kind []string LogID *string Search *string Limit int } // List lists the posts according to the filter func List(filter *Filter) ([]models.Post, error) { mutex.RLock() defer mutex.RUnlock() limit := 256 query := bson.M{} if filter != nil { if filter.LogID != nil { query["logId"] = filter.LogID } if len(filter.ID) > 1 { query["_id"] = bson.M{"$in": filter.ID} } else if len(filter.ID) == 1 { query["_id"] = filter.ID[0] } if len(filter.Kind) > 1 { for i := range filter.Kind { filter.Kind[i] = strings.ToLower(filter.Kind[i]) } query["kind"] = bson.M{"$in": filter.Kind} } else if len(filter.Kind) == 1 { query["kind"] = strings.ToLower(filter.Kind[0]) } limit = filter.Limit } posts, err := list(query, limit, "position") if err != nil { return nil, err } return posts, nil }