public ActionResult DeleteConfirmed(ViewBrandDelete model) { ShopProductsBrand brand = db.ShopProductsBrands .Include(b => b.ShopProducts) .Where(b => b.Id == model.Id) .SingleOrDefault(); if (model.DeleteAll) { if (brand.ShopProducts != null) { foreach (ShopProduct product in brand.ShopProducts) { List <ShopProductsPrice> removePrices = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == product.Id).ToList(); db.ShopProductsPrices.RemoveRange(removePrices); db.SaveChanges(); string dirPath = HttpContext.Server.MapPath("~/Content/Images/Shop/Products"); product.PhotoName = Image.Delete(dirPath, product.PhotoName); db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } db.ShopProducts.RemoveRange(brand.ShopProducts); db.SaveChanges(); } } db.ShopProductsBrands.Remove(brand); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(ViewBrandEdit model) { if (ModelState.IsValid) { ShopProductsBrand brand = db.ShopProductsBrands.Find(model.Id); brand.Name = model.Name; brand.Alias = Translit.TranslitString(model.Name); db.Entry(brand).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Create(ViewBrandCreate model) { if (ModelState.IsValid) { ShopProductsBrand brand = new ShopProductsBrand { Name = model.Name, Alias = Translit.TranslitString(model.Name) }; db.ShopProductsBrands.Add(brand); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
//========================================================== //========================================================== public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ShopProductsBrand brand = db.ShopProductsBrands.Find(id); if (brand == null) { return(HttpNotFound()); } ViewBrandEdit model = new ViewBrandEdit { Id = brand.Id, Name = brand.Name }; return(View(model)); }
//========================================================== //========================================================== public ActionResult Details(int?id) { ShopProductsBrand brand = db.ShopProductsBrands .Include(b => b.ShopProducts) .Where(b => b.Id == id) .SingleOrDefault(); if (brand == null) { return(HttpNotFound()); } ViewBrandDetails model = new ViewBrandDetails { Name = brand.Name, Products = brand.ShopProducts, ProductsCount = brand.ShopProducts.Count, }; return(View(model)); }
protected override void Seed(ShopRosKvartal.Models.ApplicationDbContext context) { var userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(context)); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); //============================================================ //инициализация ролей if (!context.Roles.Any()) { // создание роли var role = new IdentityRole { Name = "Администратор" }; // добавление роли в бд roleManager.Create(role); role = new IdentityRole { Name = "Модератор" }; roleManager.Create(role); role = new IdentityRole { Name = "Покупатель" }; roleManager.Create(role); } //============================================================ //============================================================ //создание админа для пустой БД var admin = userManager.FindByName("Admin"); if (admin == null || !context.Users.Any()) { // создание админа admin = new ApplicationUser { Email = "*****@*****.**", UserName = "******" }; string password = "******"; var result = userManager.Create(admin, password); // если создание пользователя прошло успешно if (result.Succeeded) { var role = roleManager.FindByName("Администратор"); if (role != null) { // добавляем для пользователя роль userManager.AddToRole(admin.Id, role.Name); } else { // создание роли role = new IdentityRole { Name = "Администратор" }; // добавление роли в бд roleManager.Create(role); // добавляем для пользователя роль userManager.AddToRole(admin.Id, role.Name); } } } //============================================================ //============================================================ //инициализация SMTP сервера if (!context.ToolsSMTPSettings.Any()) { ToolsSMTPSetting smtp = new ToolsSMTPSetting(); smtp.EmailFrom = "*****@*****.**"; smtp.UserName = "******"; smtp.Password = "******"; smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; context.ToolsSMTPSettings.Add(smtp); context.SaveChanges(); } //============================================================ //============================================================ //инициализация таблицы полов пользователей if (!context.UserGenders.Any()) { UserGender gender = new UserGender(); gender.Gender = "Мужской"; context.UserGenders.Add(gender); context.SaveChanges(); gender.Gender = "Женский"; context.UserGenders.Add(gender); context.SaveChanges(); } //============================================================ //============================================================ //инициализация таблицы категории if (!context.ShopCategories.Any()) { ShopCategory parent = new ShopCategory(); parent.Name = "Спортивное питание"; parent.Alias = Translit.TranslitString(parent.Name); context.ShopCategories.Add(parent); context.SaveChanges(); //----------------------------------------------- ShopCategory child = new ShopCategory { Name = "Протеины", ParentId = parent.Id }; child.Alias = Translit.TranslitString(child.Name); context.ShopCategories.Add(child); context.SaveChanges(); //----------------------------------------------- child = new ShopCategory { Name = "Гейнеры", ParentId = parent.Id }; child.Alias = Translit.TranslitString(child.Name); context.ShopCategories.Add(child); context.SaveChanges(); } //============================================================ //============================================================ //инициализация таблицы вкус товара if (!context.ShopProductsTastes.Any()) { string[] tastes = { "Ваниль", "Клубника", "Шоколад", "Карамель-ваниль", "Малина-шоколад", "Миндаль-шоколад", "Малина", "Банан", "Вишня", "Абрикос", "Персик", "Апельсин" }; for (int i = 0; i < tastes.Length; i++) { ShopProductsTaste taste = new ShopProductsTaste { Name = tastes[i] }; context.ShopProductsTastes.Add(taste); context.SaveChanges(); } } //============================================================ //============================================================ //инициализация таблицы брэнды if (!context.ShopProductsBrands.Any()) { string[] brands = { "Optimum Nutrition", "Multipower", "BSN", "Dymatize", "MuscleTech", "Weider", "Sponser", "Twinlab", "Gaspari Nutrition", "Universal Nutrition" }; for (int i = 0; i < brands.Length; i++) { ShopProductsBrand brand = new ShopProductsBrand { Name = brands[i], Alias = brands[i].Replace(" ", "-") }; context.ShopProductsBrands.Add(brand); context.SaveChanges(); } } //============================================================ }