public async Task SaveTranslationAsync(TranslationViewModel translationIn) { ProductLang productTranslation = new ProductLang { Title = translationIn.TitleTranslation, Text = translationIn.Translation, Active = translationIn.Active, Lang = translationIn.Lang, ProductId = translationIn.ProductId, }; await this.translationRepository.AddAsync(productTranslation); this.productRepository.SaveChanges(); }
public async Task <IActionResult> Details(int?id) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } if (!User.IsInRole("Admin")) { return(RedirectToAction("Login", "Account")); } if (id == null) { return(View("Error")); } ProductLang category = await _context.ProductLangs.FirstOrDefaultAsync(p => p.Product.Id == id && p.Lang.Code.ToLower() == "az"); if (category == null) { return(View("Error")); } return(View(category)); }
public async Task <IActionResult> EditPost(int id, CreateProductVM category) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } if (!User.IsInRole("Admin")) { return(RedirectToAction("Login", "Account")); } if (!ModelState.IsValid) { ViewBag.Category = await _context.CategoryLangs.Where(p => p.Lang.Code.ToLower() == "az").ToListAsync(); ModelState.AddModelError("", "Xaiş olunur düzgün doldurun."); return(View(category)); } Product newCategory = await _context.Products.FindAsync(id); if (newCategory == null) { return(View("Error")); } if (category.AllPhotos != null) { foreach (var p in newCategory.Photos) { string computerPhoto = Path.Combine(_env.WebRootPath, "images", p.PhotoURL); if (System.IO.File.Exists(computerPhoto)) { System.IO.File.Delete(computerPhoto); } IEnumerable <ProductPhoto> productPhotos = _context.ProductPhotos.Where(p => p.ProductId == newCategory.Id); _context.ProductPhotos.RemoveRange(productPhotos); } foreach (var p in category.AllPhotos) { string fileName = await p.SaveAsync(_env.WebRootPath); ProductPhoto photo = new ProductPhoto { PhotoURL = fileName, ProductId = newCategory.Id }; await _context.ProductPhotos.AddAsync(photo); } } ProductLang azBlogLangFromDb = await _context.ProductLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "az" && x.ProductId == newCategory.Id); ProductLang ruBlogLangFromDb = await _context.ProductLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "ru" && x.ProductId == newCategory.Id); ProductLang enBlogLangFromDb = await _context.ProductLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "en" && x.ProductId == newCategory.Id); azBlogLangFromDb.Name = category.NameAZ; enBlogLangFromDb.Name = category.NameEN; ruBlogLangFromDb.Name = category.NameRU; azBlogLangFromDb.Description = category.DescriptionAZ; enBlogLangFromDb.Description = category.DescriptionEN; ruBlogLangFromDb.Description = category.DescriptionRU; azBlogLangFromDb.Maifacturer = category.MaifacturerAZ; enBlogLangFromDb.Maifacturer = category.MaifacturerEN; ruBlogLangFromDb.Maifacturer = category.MaifacturerRU; newCategory.CategoryId = category.CategoryId; newCategory.IsStock = category.IsStock; newCategory.Price = category.Price; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(CreateProductVM category) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } if (!User.IsInRole("Admin")) { return(RedirectToAction("Login", "Account")); } if (!ModelState.IsValid) { ViewBag.Category = await _context.CategoryLangs.Where(p => p.Lang.Code.ToLower() == "az").ToListAsync(); return(View(category)); } if (category.AllPhotos == null) { ViewBag.Category = await _context.CategoryLangs.Where(p => p.Lang.Code.ToLower() == "az").ToListAsync(); return(View(category)); } Lang azLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "az"); Lang ruLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "ru"); Lang enLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "en"); Product newCategory = new Product() { Price = category.Price, CategoryId = category.CategoryId, IsStock = category.IsStock }; await _context.Products.AddAsync(newCategory); await _context.SaveChangesAsync(); foreach (var p in category.AllPhotos) { string fileName = await p.SaveAsync(_env.WebRootPath); ProductPhoto photo = new ProductPhoto { PhotoURL = fileName, ProductId = newCategory.Id }; await _context.ProductPhotos.AddAsync(photo); } ProductLang historyAZ = new ProductLang { Name = category.NameAZ, Description = category.DescriptionAZ, Maifacturer = category.MaifacturerAZ, LangId = azLang.Id, ProductId = newCategory.Id }; ProductLang historyEN = new ProductLang { Name = category.NameEN, Description = category.DescriptionEN, Maifacturer = category.MaifacturerEN, LangId = enLang.Id, ProductId = newCategory.Id }; ProductLang historyRU = new ProductLang { Name = category.NameRU, Description = category.DescriptionRU, Maifacturer = category.MaifacturerRU, LangId = ruLang.Id, ProductId = newCategory.Id }; _context.ProductLangs.AddRange(historyAZ, historyEN, historyRU); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }