public static async Task <Species[]> GetSpeciesAsync(string name) { GenusSpeciesPair input = _parseGenusAndSpeciesFromUserInput(string.Empty, name); if (string.IsNullOrEmpty(input.GenusName)) { // Returns species by name and/or common name. List <Species> species = new List <Species>(); if (!string.IsNullOrEmpty(name)) { using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE name = $name OR common_name = $name OR id IN (SELECT species_id FROM SpeciesCommonNames where name = $name)")) { cmd.Parameters.AddWithValue("$name", input.SpeciesName.ToLower()); using (DataTable table = await Database.GetRowsAsync(cmd)) foreach (DataRow row in table.Rows) { species.Add(await SpeciesUtils.SpeciesFromDataRow(row)); } } } return(species.ToArray()); } else { return(await GetSpeciesAsync(input.GenusName, input.SpeciesName)); } }
public static async Task <Species[]> GetSpeciesAsync(string genus, string species) { GenusSpeciesPair input = _parseGenusAndSpeciesFromUserInput(genus, species); // If the species is the empty string, don't bother trying to find any matches. // This prevents species with an empty, but non-null common name (set to "") from being returned. if (string.IsNullOrEmpty(input.SpeciesName)) { return new Species[] { } } ; if (string.IsNullOrEmpty(input.GenusName)) { // If the user didn't pass in a genus, we'll look up the species by name (name or common name). return(await GetSpeciesAsync(input.SpeciesName)); } else if (input.IsAbbreviated) { // If the genus is abbreviated (e.g. "Cornu" -> "C.") but not null, we'll look for matching species and determine the genus afterwards. // Notice that this case does not allow for looking up the species by common name, which should not occur when the genus is included. Species[] result = await GetSpeciesAsync(input.SpeciesName); return(result.Where(x => !string.IsNullOrEmpty(x.GenusName) && x.GenusName.ToLower().StartsWith(input.GenusName.ToLower())).ToArray()); } else { // If we've been given full genus and species names, we can attempt to get the species directly. // Although genera can have common names, only allow the genus to be looked up by its scientific name. Generally users wouldn't use the common name in this context. Species species_result = await _getSpeciesByGenusAndSpeciesNameAsync(input.GenusName, input.SpeciesName); return((species_result is null) ? new Species[] { } : new Species[] { species_result }); } }
private static GenusSpeciesPair _parseGenusAndSpeciesFromUserInput(string inputGenus, string inputSpecies) { return(GenusSpeciesPair.Parse(inputGenus, inputSpecies)); }