Example #1
0
        public HomeModule()
        {
            //todo: inject mysql and elastic context as dependencies to constructor with some container
            var searchContext = new ElasticSearchContext("locations", HttpContext.Current.Server.MapPath(@"~\Data\Settings.json"));
            searchContext.SetupIndex();

            Get["/"] = _ => View["Home"];

            Post["/"] = _ =>
            {
                Task.Run(() =>
                {
                    var model = this.Bind<Location>();
                    using (var context = new LocationsDbContext())
                    {
                        //note:here could be an atomicity problem
                        context.Locations.Add(model);
                        context.SaveChanges();
                    }
                    searchContext.Add(model);
                });

                return View["Home"];
            };

            Get["/search"] = _ =>
            {
                return View["Search"];
            };

            //todo: this should be DELETE. Left as GET to easy call from browser.
            Get["/clear"] = _ =>
            {
                using (var context = new LocationsDbContext())
                {
                    context.Database.Delete();
                }

                searchContext.RemoveIndex();

                return "DB and index cleared.";
            };

            //todo: this should be POST. Left as GET to easy call from browser.
            Get["/load/{count}"] = _ =>
            {
                var locations = ReadWorldCities(_.count);

                using (var context = new LocationsDbContext())
                {
                    context.Locations.AddRange(locations);
                    context.SaveChanges();
                }

                searchContext.Add(locations);

                return $"{_.count} items inserted.";
            };
        }
        public void Setup()
        {
            indexName = "locs" + Guid.NewGuid().ToString().Substring(0, 3);
            context = new ElasticSearchContext(indexName, @"Data\Settings.json");

            context.SetupIndex();

            context.Add(GetTestLocations());

            //todo: how to know when indexing is done?
            Thread.Sleep(3000);
        }