public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } CatalogReference = await _context.CatalogReferences.FindAsync(id); if (CatalogReference != null) { _context.CatalogReferences.Remove(CatalogReference); //check other reference var oldReference = await _context.CatalogReferences .SingleOrDefaultAsync(x => x.CatalogItemId == CatalogReference.ReferenceCatalogItemId && x.ReferenceCatalogItemId == CatalogReference.CatalogItemId); if (oldReference != null) { _context.CatalogReferences.Remove(oldReference); } await _context.SaveChangesAsync(); } return(RedirectToPage("/Products/Edit", new { id = CatalogReference.CatalogItemId })); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(_mapper.Map <ShippingPriceWeight>(ShippingPriceWeight)).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShippingPriceWeightExists(ShippingPriceWeight.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.ShippingPriceWeights.Add(_mapper.Map <ShippingPriceWeight>(ShippingPriceWeight)); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <Basket> AddBasketItemAsync(int id, BasketItem item, int?option1 = null, int?option2 = null, int?option3 = null) { var basket = await _dbContext.Baskets .Include(b => b.Items) .FirstOrDefaultAsync(x => x.Id == id); //basket.AddItem(item.CatalogItemId, item.UnitPrice, item.Quantity, item.Details.Select(x => x.CatalogAttributeId).ToList()); basket.AddItem(item.CatalogItemId, item.UnitPrice, item.Quantity, option1, option2, option3); await _dbContext.SaveChangesAsync(); return(basket); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { await Initialize(); return(Page()); } foreach (var item in ShopConfigModel) { var shopConfig = _mapper.Map <ApplicationCore.Entities.ShopConfig>(item); _context.Attach(shopConfig).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var item = await _context.ShippingPriceWeights.FindAsync(id); if (item != null) { _context.ShippingPriceWeights.Remove(item); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var type = await _context.CatalogTypes.FindAsync(id); if (type != null) { if (!string.IsNullOrEmpty(type.PictureUri)) { _service.DeleteFile(_backofficeSettings.WebProductTypesPictureV2FullPath, Utils.GetFileName(type.PictureUri)); } _context.CatalogTypes.Remove(type); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name"); if (!ModelState.IsValid) { return(Page()); } //check if code exists if (_context.CatalogTypes.Any(x => x.Code.ToUpper() == ProductTypeModel.Code.ToUpper() && x.Id != ProductTypeModel.Id)) { ModelState.AddModelError("", $"O nome do Tipo do Produto '{ProductTypeModel.Code}' já existe!"); return(Page()); } if (ProductTypeModel.Picture?.Length > 2097152) { ModelState.AddModelError("", "A menina quer por favor diminuir o tamanho da imagem? O máximo é 2MB, obrigado! Ass.: O seu amor!"); return(Page()); } if (ProductTypeModel.FormFileTextHelpers?.Count > 0 && ProductTypeModel.FormFileTextHelpers.Any(x => x.Length > 2097152)) { ModelState.AddModelError("", "A menina quer por favor diminuir o tamanho das imagens da localização do nome? O máximo é 2MB, obrigado! Ass.: O seu amor!"); return(Page()); } ProductTypeModel.Slug = Utils.URLFriendly(ProductTypeModel.Slug); if ((await CheckIfSlugExistsAsync(ProductTypeModel.Id, ProductTypeModel.Slug))) { ModelState.AddModelError("ProductTypeModel.Slug", "Este slug já existe!"); return(Page()); } //Get entity var productTypeEntity = await _context.CatalogTypes .Include(x => x.Categories) .Include(x => x.PictureTextHelpers) .SingleOrDefaultAsync(x => x.Id == ProductTypeModel.Id); //Save Image if (ProductTypeModel?.Picture?.Length > 0) { if (!string.IsNullOrEmpty(productTypeEntity.PictureUri)) { _service.DeleteFile(_backofficeSettings.WebProductTypesPictureV2FullPath, Utils.GetFileName(productTypeEntity.PictureUri)); } ProductTypeModel.PictureUri = _service.SaveFile(ProductTypeModel.Picture, _backofficeSettings.WebProductTypesPictureV2FullPath, _backofficeSettings.WebProductTypesPictureV2Uri, ProductTypeModel.Id.ToString(), true, 300).PictureUri; } //Save Images Text Helpers if (ProductTypeModel?.FormFileTextHelpers?.Count > 0) { //Delete All foreach (var item in productTypeEntity.PictureTextHelpers) { _service.DeleteFile(item.Location); _context.Entry(item).State = EntityState.Deleted; } foreach (var item in ProductTypeModel.FormFileTextHelpers) { var lastId = _context.FileDetails.Count() > 0 ? GetLastFileDetailsId() : 0; var pictureInfo = _service.SaveFile(item, _backofficeSettings.WebProductTypesPictureV2FullPath, _backofficeSettings.WebProductTypesPictureV2Uri, (++lastId).ToString(), true, 150); productTypeEntity.PictureTextHelpers.Add(new FileDetail { PictureUri = pictureInfo.PictureUri, Extension = pictureInfo.Extension, FileName = pictureInfo.Filename, Location = pictureInfo.Location }); } } if (productTypeEntity != null) { productTypeEntity.Update(ProductTypeModel.Code, ProductTypeModel.Description, ProductTypeModel.DeliveryTimeMin, ProductTypeModel.DeliveryTimeMax, ProductTypeModel.DeliveryTimeUnit, ProductTypeModel.Price, ProductTypeModel.AdditionalTextPrice, ProductTypeModel.Weight, ProductTypeModel.MetaDescription, ProductTypeModel.Title, ProductTypeModel.Slug); if (!string.IsNullOrEmpty(ProductTypeModel.PictureUri)) { productTypeEntity.UpdatePicture(ProductTypeModel.PictureUri); } // //Remove // var to_remove = productTypeEntity.Categories.Where(c => !ProductTypeModel.CategoriesId.Any(c2 => c2 == c.CategoryId)); // foreach (var item in to_remove) // { // _context.Entry(item).State = EntityState.Deleted; // } // //Add // var to_add = ProductTypeModel.CategoriesId.Where(c => !productTypeEntity.Categories.Any(c2 => c2.CategoryId == c)); // foreach (var item in to_add) // { // productTypeEntity.AddCategory(new CatalogTypeCategory(item)); // } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name"); if (!ModelState.IsValid) { return(Page()); } //check if code exists if (_context.CatalogTypes.Any(x => x.Code.ToUpper() == ProductTypeModel.Code.ToUpper())) { ModelState.AddModelError("", $"O nome do Tipo do Produto '{ProductTypeModel.Code}' já existe!"); return(Page()); } if (ProductTypeModel.CategoriesId == null || ProductTypeModel.CategoriesId.Count == 0) { ModelState.AddModelError("", "O campo Categorias é obrigatório"); return(Page()); } if (ProductTypeModel.Picture?.Length > 2097152) { ModelState.AddModelError("", "A menina quer por favor diminuir o tamanho da imagem principal? O máximo é 2MB, obrigado! Ass.: O seu amor!"); return(Page()); } if (ProductTypeModel.FormFileTextHelpers?.Count > 0 && ProductTypeModel.FormFileTextHelpers.Any(x => x.Length > 2097152)) { ModelState.AddModelError("", "A menina quer por favor diminuir o tamanho das imagens da localização do nome? O máximo é 2MB, obrigado! Ass.: O seu amor!"); return(Page()); } ProductTypeModel.Slug = Utils.URLFriendly(ProductTypeModel.Slug); if ((await CheckIfSlugExistsAsync(ProductTypeModel.Slug))) { ModelState.AddModelError("ProductTypeModel.Slug", "Este slug já existe!"); return(Page()); } //Save Image if (ProductTypeModel?.Picture?.Length > 0) { var lastId = _context.CatalogTypes.Count() > 0 ? GetLastCatalogTypeId() : 0; ProductTypeModel.PictureUri = _service.SaveFile(ProductTypeModel.Picture, _backofficeSettings.WebProductTypesPictureV2FullPath, _backofficeSettings.WebProductTypesPictureV2Uri, (++lastId).ToString(), true, 300).PictureUri; } //Save Images Text Helpers if (ProductTypeModel?.FormFileTextHelpers?.Count > 0) { foreach (var item in ProductTypeModel.FormFileTextHelpers) { var lastId = _context.FileDetails.Count() > 0 ? GetLastFileDetailsId() : 0; var pictureInfo = _service.SaveFile(item, _backofficeSettings.WebProductTypesPictureV2FullPath, _backofficeSettings.WebProductTypesPictureV2Uri, (++lastId).ToString(), true, 150); ProductTypeModel.PictureTextHelpers.Add(new FileDetailViewModel { PictureUri = pictureInfo.PictureUri, Extension = pictureInfo.Extension, FileName = pictureInfo.Filename, Location = pictureInfo.Location }); } } var catalogType = _mapper.Map <ApplicationCore.Entities.CatalogType>(ProductTypeModel); foreach (var item in ProductTypeModel.CategoriesId) { catalogType.AddCategory(new CatalogTypeCategory(item)); } _context.CatalogTypes.Add(catalogType); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public static async Task SeedAsync(DamaContext catalogContext, ILoggerFactory loggerFactory, int?retry = 0, bool isDevelopment = false) { var log = loggerFactory.CreateLogger <DamaContextSeed>(); if (isDevelopment) { if (!catalogContext.Categories.Any()) { var cat1 = await catalogContext.Categories.AddAsync(new Category("Acessórios", "acessorios", 1)); var cat2 = await catalogContext.Categories.AddAsync(new Category("Papelaria", "papelaria", 2)); var cat3 = await catalogContext.Categories.AddAsync(new Category("Decoração", "decoracao", 3)); await catalogContext.SaveChangesAsync(); var type1 = new CatalogType("Canecas", "http://localhost:5500/images/producttypes/canecas-20.jpg"); type1.AddCategory(new CatalogTypeCategory(cat1.Entity.Id)); await catalogContext.CatalogTypes.AddAsync(type1); var type2 = new CatalogType("Cadernos", "http://localhost:5500/images/producttypes/cadernos-11.jpg"); type2.AddCategory(new CatalogTypeCategory(cat2.Entity.Id)); await catalogContext.CatalogTypes.AddAsync(type2); var type3 = new CatalogType("Imãs", "http://localhost:5500/images/producttypes/imas-38.jpg"); type3.AddCategory(new CatalogTypeCategory(cat3.Entity.Id)); await catalogContext.CatalogTypes.AddAsync(type3); await catalogContext.SaveChangesAsync(); var illustrationType = new IllustrationType("Ferias", "Ferias"); await catalogContext.IllustrationTypes.AddAsync(illustrationType); await catalogContext.SaveChangesAsync(); var illustration = new CatalogIllustration("DAMA_FERIAS", "Dama Férias", illustrationType.Id); await catalogContext.CatalogIllustrations.AddAsync(illustration); await catalogContext.SaveChangesAsync(); var item1 = new CatalogItem("Carteira de Mulher", "carteira-de-mulher", 18.99m, "http://localhost:5500/images/products/dama-de-ferias-(2)-75.jpg", true, true, true, true, illustration.Id, type1.Id); item1.AddCategory(cat1.Entity.Id); await catalogContext.CatalogItems.AddAsync(item1); var item2 = new CatalogItem("Caderno Educadora", "caderno-educadora", 9.99m, "http://localhost:5500/images/products/cadernos-(1)-208.jpg", true, true, true, true, illustration.Id, type2.Id); item2.AddCategory(cat2.Entity.Id); await catalogContext.CatalogItems.AddAsync(item2); var item3 = new CatalogItem("Ima Auxiliar", "ima-auxiliar", 5.99m, "http://localhost:5500/images/products/iman-auxiliar-264.jpg", true, true, true, true, illustration.Id, type3.Id); item3.AddCategory(cat3.Entity.Id); await catalogContext.CatalogItems.AddAsync(item3); await catalogContext.SaveChangesAsync(); log.LogInformation("Seed data create successuful"); } } // if(!catalogContext.ShippingPriceWeights.Any()) // { // catalogContext.ShippingPriceWeights.AddRange( // new ShippingPriceWeight // { // MinWeight = 0, // MaxWeight = 20, // Price = 0.53M // }, // new ShippingPriceWeight // { // MinWeight = 20, // MaxWeight = 50, // Price = 0.70M, // }, // new ShippingPriceWeight // { // MinWeight = 50, // MaxWeight = 100, // Price = 0.85M, // }, // new ShippingPriceWeight // { // MinWeight = 100, // MaxWeight = 500, // Price = 1.50M, // }, // new ShippingPriceWeight // { // MinWeight = 500, // MaxWeight = 2000, // Price = 3.5M // }); // await catalogContext.SaveChangesAsync(); // } // if (!catalogContext.ShopConfigs.Any(x => x.Type == ShopConfigType.SEO)) // { // catalogContext.ShopConfigs.AddRange( // new ShopConfig // { // Type = ShopConfigType.SEO, // IsActive = true, // Name = "Meta Description", // Value = "Bem-vindo à Dama no Jornal®. A Loja Online que oferece os Presentes Personalizados mais Criativos. Vem conhecer os nossos Produtos e Personaliza!" // }, // new ShopConfig // { // Type = ShopConfigType.SEO, // IsActive = true, // Name = "Title", // Value = "Dama no Jornal - Loja Online" // }); // await catalogContext.SaveChangesAsync(); // } // if(catalogContext.CatalogItems.All(x => string.IsNullOrEmpty(x.Slug))) // { // //Fix Duplicates catalog Names // var duplicates = await catalogContext.CatalogItems // .GroupBy(x => x.Name) // .Where(g => g.Count() > 1) // .ToListAsync(); // foreach (var item in duplicates) // { // for (int i = 0; i < item.Count(); i++) // { // item.ElementAt(i).UpdateName($"{item.ElementAt(i).Name}-{i + 1}"); // } // } // await catalogContext.SaveChangesAsync(); // //Update Slug // await catalogContext.CatalogItems.ForEachAsync(c => c.UpdateSlug(Utils.URLFriendly(c.Name))); // await catalogContext.SaveChangesAsync(); // } // if(catalogContext.Categories.All(x => string.IsNullOrEmpty(x.Slug))) // { // await catalogContext.Categories.ForEachAsync(x => x.Slug = Utils.URLFriendly(x.Name)); // await catalogContext.SaveChangesAsync(); // } // if(catalogContext.CatalogTypes.All(x => string.IsNullOrEmpty(x.Slug))) // { // var bolsas = await catalogContext.CatalogTypes.Where(x => x.Name == "Bolsa de Telemóvel").ToListAsync(); // if (bolsas?.Count > 1) // { // foreach (var item in bolsas) // { // item.Name = item.Code == "BTL_M" ? "Bolsa de Telemóvel Mulher" : "Bolsa de Telemóvel Homem"; // } // await catalogContext.SaveChangesAsync(); // } // await catalogContext.CatalogTypes.ForEachAsync(x => x.Slug = Utils.URLFriendly(x.Name)); // await catalogContext.SaveChangesAsync(); // } //if (!catalogContext.CatalogPrices.Any()) //{ // var products = await catalogContext.CatalogItems // .Include(x => x.CatalogType) // .ToListAsync(); // foreach (var item in products) // { // catalogContext.CatalogPrices.Add(new CatalogPrice // { // CatalogItemId = item.Id, // Price = item.Price ?? item.CatalogType.Price, // Active = true, // }); // } // await catalogContext.SaveChangesAsync(); //} //int retryForAvailability = retry.Value; //try //{ ////TODO: Only run this if using a real database // context.Database.Migrate(); // if (!catalogContext.CatalogBrands.Any()) // { // catalogContext.CatalogBrands.AddRange( // GetPreconfiguredCatalogBrands()); // await catalogContext.SaveChangesAsync(); // } // if (!catalogContext.CatalogTypes.Any()) // { // catalogContext.CatalogTypes.AddRange( // GetPreconfiguredCatalogTypes()); // await catalogContext.SaveChangesAsync(); // } // if (!catalogContext.CatalogItems.Any()) // { // catalogContext.CatalogItems.AddRange( // GetPreconfiguredItems()); // await catalogContext.SaveChangesAsync(); // } //} //catch (Exception ex) //{ // if (retryForAvailability < 10) // { // retryForAvailability++; // var log = loggerFactory.CreateLogger<DamaContextSeed>(); // log.LogError(ex.Message); // await SeedAsync(catalogContext, loggerFactory, retryForAvailability); // } //} }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(CatalogReference).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CatalogReferenceExists(CatalogReference.Id)) { return(NotFound()); } else { throw; } } //Check Reference Product if (OriginalReference != CatalogReference.ReferenceCatalogItemId) { //Delete reference var oldReference = await _context.CatalogReferences .SingleOrDefaultAsync(x => x.CatalogItemId == OriginalReference && x.ReferenceCatalogItemId == CatalogReference.CatalogItemId); _context.CatalogReferences.Remove(oldReference); //Create new reference var newReference = await _context.CatalogReferences .SingleOrDefaultAsync(x => x.CatalogItemId == CatalogReference.ReferenceCatalogItemId && x.ReferenceCatalogItemId == CatalogReference.CatalogItemId); if (newReference == null) { _context.CatalogReferences.Add( new CatalogReference(CatalogReference.ReferenceCatalogItemId, CatalogReference.LabelDescription, CatalogReference.CatalogItemId) ); } await _context.SaveChangesAsync(); } else if (!OriginalLabelReference.Equals(CatalogReference.LabelDescription)) { var catalogReference = await _context.CatalogReferences .SingleOrDefaultAsync(x => x.CatalogItemId == OriginalReference && x.ReferenceCatalogItemId == CatalogReference.CatalogItemId); catalogReference.UpdateLabel(CatalogReference.LabelDescription); await _context.SaveChangesAsync(); } return(RedirectToPage("/Products/Edit", new { id = CatalogReference.CatalogItemId })); }