Permalink
Browse files

Changed code to demonstrate with a read-only db account.

  • Loading branch information...
primaryobjects committed Jan 5, 2012
1 parent 732808b commit 4e7b91e6df151c1166e8041b12e5d158e5989897
Showing with 25 additions and 31 deletions.
  1. +16 −0 MongoDragons.Managers/DragonManager.cs
  2. +9 −31 MongoDragons/Program.cs
@@ -47,6 +47,22 @@ public static List<Dragon> GetAll()
return DbContext.Current.All<Dragon>().OrderBy(d => d.Name).ToList();
}
+ public static List<Dragon> Find(string keyword)
+ {
+ List<Dragon> dragons = null;
+
+ if (keyword.Length > 0)
+ {
+ dragons = DbContext.Current.All<Dragon>().Where(d => d.Name.Contains(keyword)).OrderBy(d => d.Name).ToList();
+ }
+ else
+ {
+ dragons = GetAll();
+ }
+
+ return dragons;
+ }
+
public static void Save(Dragon dragon)
{
DbContext.Current.Add(dragon);
View
@@ -9,55 +9,33 @@ class Program
{
static void Main(string[] args)
{
+ string keyword = "";
+
// Initialize our database provider.
Setup.Initialize();
- // Create some dragons and save them in the database.
- GenerateDragons();
-
while (true)
{
// Display the dragons.
- List<Dragon> dragons = DisplayDragons();
+ List<Dragon> dragons = DisplayDragons(keyword);
// Get input from the user.
- Console.Write("Enter an Id to Delete, or Q to quit:>");
- string input = Console.ReadLine();
+ Console.Write("Enter text to search by or Q to quit:>");
+ keyword = Console.ReadLine();
- // Execute the user's command.
- if (input.ToUpper() == "Q")
- {
+ // Check the input.
+ if (keyword.ToUpper() == "Q")
break;
- }
- else
- {
- // See if this is a delete request.
- int index;
- if (Int32.TryParse(input, out index))
- {
- // Delete the dragon.
- DragonManager.Delete(dragons[index - 1]);
- }
- }
- }
- }
-
- private static void GenerateDragons()
- {
- for (int i = 0; i < 3; i++)
- {
- Dragon dragon = DragonManager.CreateRandom();
- DragonManager.Save(dragon);
}
}
- private static List<Dragon> DisplayDragons()
+ private static List<Dragon> DisplayDragons(string keyword = "")
{
Console.WriteLine(String.Format("{0,3} | {1,-24} | {2,3} | {3,4} | {4,3} | {5,10} | {6,8}", "Id", "Name", "Age", "Gold", "HP", "Breath", "Born"));
Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
int count = 0;
- List<Dragon> dragons = DragonManager.GetAll();
+ List<Dragon> dragons = DragonManager.Find(keyword);
foreach (Dragon dragon in dragons)
{
Console.WriteLine(String.Format("{0, 3} | {1}", ++count, dragon));

0 comments on commit 4e7b91e

Please sign in to comment.