Пример #1
0
        public ActionResult GroceryInfo(string name)
        {
            ServiceHost.TraceLog.TraceDetail("GroceryInfo called with " + name);
            JsGroceryResults groceryResults = new JsGroceryResults();
            var context = new GroceryContext();
            List<GroceryReturnValue> grocery = new List<GroceryReturnValue>();
            var groceryName = name.ToLower();

            try
            {
                var groc = context.Groceries.Include("Category").OrderBy(g => g.Name).First(g => g.Name.StartsWith(groceryName));
                grocery.Add(new GroceryReturnValue() { Name = groc.Name, Category = groc.Category.Name, ImageUrl = groc.ImageUrl });
                ServiceHost.TraceLog.TraceDetail(String.Format("Found {0} category for {1}", groc.Category.Name, name));
            }
            catch (Exception)
            {
                ServiceHost.TraceLog.TraceDetail("Could not find a category for " + name);
            }

            groceryResults.Count = grocery.Count;
            groceryResults.Groceries = grocery;

            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = groceryResults;
            return result;
        }
Пример #2
0
        public ActionResult GroceryCategories(string startsWith = null, string contains = null, int maxCount = 10)
        {
            JsGroceryResults groceryResults = new JsGroceryResults();
            var context = new GroceryContext();
            List<string> categories = new List<string>();

            var possibleCategories = context.GroceryCategories.Where(g => g.Name.StartsWith(startsWith) || g.Name.Contains(contains)).ToList();
            foreach (var category in possibleCategories)
            {
                if (startsWith == null ||
                    category.Name.StartsWith(startsWith, StringComparison.OrdinalIgnoreCase) ||
                    (contains != null && category.Name.Contains(contains)))
                {
                    categories.Add(category.Name);
                }
                if (categories.Count == maxCount) { break; }
            }

            groceryResults.Count = categories.Count;
            groceryResults.Groceries = categories;

            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = groceryResults;
            return result;
        }
Пример #3
0
        public static bool ReloadGroceryData(string connectionString, string filename)
        {
            var context = new GroceryContext(connectionString);

            // remove groceries and categories
            Console.WriteLine("Removing groceries");
            var groceries = context.Groceries.ToList();
            foreach (var g in groceries)
                context.Groceries.Remove(g);
            context.SaveChanges();
            Console.WriteLine("Removing categories");
            var categories = context.GroceryCategories.ToList();
            foreach (var c in categories)
                context.GroceryCategories.Remove(c);
            context.SaveChanges();

            // recreate categories from the model
            Console.WriteLine("Adding categories");
            foreach (var c in BuiltSteady.Zaplify.GroceryService.Models.GroceryCategories.Categories)
                context.GroceryCategories.Add(new GroceryCategory() { ID = c.ID, Name = c.Name });
            context.SaveChanges();

            // load the grocery names and their categories from a tab-delimited flat file
            try
            {
                filename = filename ?? @"groceries.txt";
                using (var stream = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read))
                using (var reader = new StreamReader(stream))
                {
                    var groceryInfo = reader.ReadLine();
                    while (!String.IsNullOrEmpty(groceryInfo))
                    {
                        var keyval = groceryInfo.Split('\t');
                        if (keyval.Length >= 2)
                        {
                            // store the grocery name in lowercase and look up the category ID by name
                            var groceryName = keyval[0].ToLower();
                            var categoryName = keyval[1].Trim('"');
                            var category = context.GroceryCategories.First(c => c.Name == categoryName);
                            var grocery = new Grocery() { Name = groceryName, GroceryCategoryID = category.ID };
                            if (keyval.Length >= 3)
                                grocery.ImageUrl = keyval[2].Trim();
                            context.Groceries.Add(grocery);
                            Console.WriteLine("Added " + grocery.Name);
                        }
                        groceryInfo = reader.ReadLine();
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("GroceryLoader: load failed; ex: ", ex.Message);
                context.SaveChanges();
                return false;
            }
            return true;
        }
Пример #4
0
        public ActionResult GroceryNames(string startsWith = null, string contains = null, int maxCount = 10)
        {
            JsGroceryResults groceryResults = new JsGroceryResults();
            var context = new GroceryContext();
            List<GroceryReturnValue> groceries = new List<GroceryReturnValue>();

            List<Grocery> possibleGroceryNames;
            if (startsWith != null)
            {   // filter by startsWith
                possibleGroceryNames = context.Groceries.Include("Category").Where(g => g.Name.StartsWith(startsWith)).ToList();
            }
            else
            {   // get all and post-filter
                possibleGroceryNames = context.Groceries.Include("Category").ToList();
            }

            contains = (contains != null) ? contains.ToLowerInvariant() : null;
            foreach (var grocery in possibleGroceryNames)
            {
                if (contains == null || grocery.Name.Contains(contains))
                {   // upper-case each word in name and add to results
                    var groceryName = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(grocery.Name);
                    var groceryValue = new GroceryReturnValue() { Name = groceryName, Category = grocery.Category.Name };
                    groceryValue.ImageUrl = (!string.IsNullOrEmpty(grocery.ImageUrl)) ? grocery.ImageUrl : null;
                    groceries.Add(groceryValue);
                }
                if (groceries.Count == maxCount) { break; }
            }

            groceryResults.Count = groceries.Count;
            groceryResults.Groceries = groceries;

            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = groceryResults;
            return result;
        }