Skip to content

Commit

Permalink
Enable 'search' by name using --name flag. It searches the Name and S…
Browse files Browse the repository at this point in the history
…cientific Name.
  • Loading branch information
dnndev committed Apr 20, 2017
1 parent dea4a20 commit cde9666
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions SoFried201704/ListHerbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ namespace KellyFord.Prompt.Commands
[ConsoleCommand("list-herbs", "kf", "Returns a list of edible plants", new string[] { })]
public class ListHerbs : ConsoleCommandBase, IConsoleCommand
{
public string ValidationMessage { get; private set; }
private const string FLAG_NAME = "name";

private string Name { get; set; }

public string ValidationMessage { get; private set; }

public void Init(string[] args, PortalSettings portalSettings, UserInfo userInfo, int activeTabId)
{
base.Initialize(args, portalSettings, userInfo, activeTabId);
System.Text.StringBuilder sbErrors = new System.Text.StringBuilder();


ValidationMessage = sbErrors.ToString();
}

Expand Down Expand Up @@ -52,10 +55,23 @@ public ConsoleResultModel Run()
new string[] { "Wild Mountain Thyme - The Chieftains",
"Purple Heather - Rod Steward" }));

return new ConsoleResultModel(herbs.Count + " herbs found") {
data = herbs,
fieldOrder = new string[] { "ScientificName", "Name", "Description" }
};
if (HasFlag(FLAG_NAME))
{
string searchTerm = Flag(FLAG_NAME, "");
// try to find the herb in our list
HerbModel herb = herbs.Find( x => x.Name.Contains(searchTerm) || x.ScientificName.Contains(searchTerm));
List<HerbModel> results = new List<HerbModel>();
results.Add(herb);
return new ConsoleResultModel() { data = results };
}
else
{
return new ConsoleResultModel(herbs.Count + " herbs found")
{
data = herbs,
fieldOrder = new string[] { "ScientificName", "Name", "Description" }
};
}
}
}
}

0 comments on commit cde9666

Please sign in to comment.