예제 #1
0
 public void UpdateEnterprise(Enterprise enterprise)
 {
     using (var session = _documentStore.OpenSession())
     {
         session.Store(enterprise);
         session.SaveChanges();
         _logger.Info("Updated enterprise " + enterprise.Name, ", id: " + enterprise.Id);
     }
 }
예제 #2
0
        public static string FormatDisplayPostalInfo(Enterprise enterprise)
        {
            var postalInfo = "";

            if (enterprise.PostalCode > 0)
                postalInfo += enterprise.PostalCode.ToString();

            if (!string.IsNullOrEmpty(enterprise.PostalTown))
                postalInfo += " " + enterprise.PostalTown;
            
            //TODO. if town is empty take commune, then sublocality then county. 
            return postalInfo;
        }
예제 #3
0
        public static bool ValidEditableEnterprise(Enterprise enterprise, Raven.Client.IDocumentSession session)
        {
            if(enterprise != null)
            {
                if (enterprise.OwnedByAccount)
                {
                    //If enterprise is owned by an account, check if current account is the correct one
                    var account = session.Load<Account>(HttpContext.Current.User.Identity.Name);
                    return (account.Enterprises.Contains(enterprise.Id) || account.IsAdmin) && account.Enabled;
                }
                //Add product to a new enterprise
                if (enterprise.IsNew)
                    return true;
                //Add product to an enterprise in edit-mode
                if (!enterprise.LockedFromEdit)
                    return true;
            }

            return false;
        }
예제 #4
0
        public string CreateEnterprise(Enterprise enterprise)
        {
            using (var session = _documentStore.OpenSession())
            {
                if (enterprise == null)
                    return string.Empty;
                if (!string.IsNullOrEmpty(enterprise.Id))
                {
                    if (session.Load<Enterprise>(enterprise.Id) != null)
                        return string.Empty;
                }

                session.Store(enterprise);
                session.SaveChanges();
                _logger.Info(string.Format("New enterprise created: {0} ({1}), Code:[nBNbn5fgaq]", enterprise.Name, enterprise.Id));

                return enterprise.Id;
            }
        }
예제 #5
0
 public static EnterpriseViewModel ModelToViewModel(Enterprise model)
 {
     Mapper.CreateMap<Enterprise, EnterpriseViewModel>();
     return Mapper.Map<Enterprise, EnterpriseViewModel>(model);
 }
예제 #6
0
 public static string FormatDisplayStreet(Enterprise enterprise)
 {
     return string.Format("{0} {1}",enterprise.StreetRoute,enterprise.StreetNumber);
 }
예제 #7
0
        public ActionResult CreateTempEnterprise(EnterpriseViewModel viewModel)
        {
            if (!string.IsNullOrEmpty(viewModel.Nope))
                return RedirectToAction("Index", "Home");

            if (string.IsNullOrEmpty(viewModel.Name))
                ModelState.AddModelError("Name", "Ange restaurangens namn");

            if (viewModel.DisplayCategories == null || viewModel.DisplayCategories.Count < 1)
                ModelState.AddModelError("DisplayCategories", "Välj minst en kategori");
            else
            {
                viewModel.DisplayCategories = EnterpriseHelper.GetDisplayCategories(viewModel.DisplayCategories);
            }

            if (viewModel.Coordinates.Lat < 1 || viewModel.Coordinates.Lng < 1)
                ModelState.AddModelError("Coordinates", "Du måste ange någon platsinfo");

            if (ModelState.IsValid)
            {
                var categoryList = new List<string>();
                categoryList.AddRange(viewModel.DisplayCategories.Take(6).Select(catgory => catgory.Value));

                var enterprise = new Enterprise
                {
                    Name = viewModel.Name,
                    Phone = viewModel.Phone,
                    StreetNumber = viewModel.StreetNumber,
                    StreetRoute = viewModel.StreetRoute,
                    PostalCode = viewModel.PostalCode,
                    PostalTown = viewModel.PostalTown,
                    Commune = viewModel.Commune,
                    County = viewModel.County,
                    SubLocality = viewModel.SubLocality,
                    CountryCode = viewModel.CountryCode ?? "SE",

                    SearchTags = EnterpriseHelper.GenerateSearchTags(viewModel.Name),

                    Coordinates = new Coordinates { Lat = viewModel.Coordinates.Lat, Lng = viewModel.Coordinates.Lng },
                    Categories = categoryList,

                    IsNew = true,
                    OwnedByAccount = false,
                    LockedFromEdit = false,

                    LastUpdated = DateTime.Now,

                    Menu = new Menu()
                };

                if (string.IsNullOrEmpty(viewModel.Id))
                {
                    enterprise.Id = EnterpriseHelper.GetId(GeneralHelper.GetGuid());
                    Db.Enterprises.CreateEnterprise(enterprise);
                }
                else
                {
                    var enterpriseInDb = Db.Enterprises.GetEnterpriseById(enterprise.Id);
                    if (enterpriseInDb != null)
                    {
                        Db.Enterprises.UpdateEnterprise(enterprise);
                    }
                }
                return Json(new { url =  Url.Action("Edit", new { key = EnterpriseHelper.GetKey(enterprise.Id) }) });
            }

            viewModel.ShowForm = true;

            return PartialView("~/Views/Manage/_AddEnterpriseForm.cshtml", viewModel);
        }
예제 #8
0
        public static Enterprise CreateFakeEnterprise(Abstract.Db.IDb Db,bool modified)
        {
            var enterprise = new Enterprise
                                 {
                                     Id = EnterpriseHelper.GetId(GeneralHelper.GetGuid()),
                                     Name = RandomString(),
                                     Categories = RandomListString(),
                                     LastUpdated = DateTime.Now,
                                     Coordinates = RandomCoordinates(),
                                     StreetRoute = RandomString(),
                                     County = "Stockholm"
                                 };
            var menu = new Menu();
            var categories = new List<Category>();

            var products = new List<Product>();

            for (var i = 0; i < _random.Next(35, 40); i++)
            {
                var category = new Category
                                      {
                                          Id = GeneralHelper.GetGuid(),
                                          Name = RandomString(),
                                          Products = new List<string>()
                                      };
                for (var j = 0; j < _random.Next(1, 2); j++)
                {
                    var product = NewProduct(enterprise.Id,modified);


                    products.Add(product);
                    category.Products.Add(product.Id);
                }
                categories.Add(category);
            }


            menu.Categories = categories;


            if(!modified)
            {
                enterprise.IsNew = true; 
            }

            enterprise.Menu = menu;
            
            Db.Enterprises.CreateEnterprise(enterprise);
            Db.Products.AddProductsToDb(products);

            if(modified)
            {
                Thread.Sleep(1000);
                var categoryCount = enterprise.Menu.Categories.Count;

                //Remove random products
                for (var i = 0; i < _random.Next(1, 4); i++)
                {
                    var randomCategory = enterprise.Menu.Categories[_random.Next(categoryCount)];
                    var productCountForCategory = randomCategory.Products.Count;
                    enterprise.Menu.Categories[_random.Next(categoryCount)].Products.RemoveAt(_random.Next(productCountForCategory));
                }

                //Create new products
                var newProducts = new List<Product>();
                for (var i = 0; i < _random.Next(1, 8);i++ )
                {
                    newProducts.Add(NewProduct(enterprise.Id,false));
                }

                //Add the new products to random categories in random places
                foreach (var newProduct in newProducts)
                {
                    var randomCategory = enterprise.Menu.Categories[_random.Next(categoryCount)];
                    var productCountForCategory = randomCategory.Products.Count;
                    randomCategory.Products.Insert(_random.Next(productCountForCategory),newProduct.Id);
                }

                Db.Products.AddProductsToDb(newProducts);
                Thread.Sleep(1000);
                Db.Enterprises.UpdateEnterprise(enterprise.Id, enterprise.Menu);
            }

            return enterprise;
        }