package characters import ( "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Filter is used to filter the list of characters type Filter struct { IDs []string `json:"ids"` Nicks []string `json:"nicks"` Names []string `json:"names"` Author *string `json:"author"` Search *string `json:"search"` } // List lists all characters func List(filter *Filter) ([]models.Character, error) { query := bson.M{} if filter != nil { if len(filter.IDs) > 1 { query["_id"] = bson.M{"$in": filter.IDs} } else if len(filter.IDs) == 1 { query["_id"] = filter.IDs[0] } if len(filter.Nicks) > 1 { query["nicks"] = bson.M{"$in": filter.Nicks} } else if len(filter.Nicks) == 1 { query["nicks"] = filter.Nicks[0] } if len(filter.Names) > 1 { query["$or"] = bson.M{ "name": bson.M{"$in": filter.Names}, "shortName": bson.M{"$in": filter.Names}, } } else if len(filter.Names) == 1 { query["$or"] = bson.M{ "name": filter.Names[0], "shortName": filter.Names[0], } } if filter.Author != nil { query["author"] = *filter.Author } if filter.Search != nil { query["$text"] = bson.M{"$search": *filter.Search} } } return list(query) }