public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (IsImageSizeInvalid(ShopConfigDetailModel.Picture)) { return(Page()); } if (ShopConfigDetailModel.PictureWebp?.Length > 0 && IsImageSizeInvalid(ShopConfigDetailModel.PictureWebp)) { return(Page()); } var lastShopDetailId = (_context.ShopConfigDetails.Count() > 0 ? GetLastShopConfigDetailsId() : 0) + 1; if (ShopConfigDetailModel.Picture.Length > 0) { ShopConfigDetailModel.PictureUri = _service.SaveFile(ShopConfigDetailModel.Picture, _backofficeSettings.WebNewsPictureFullPath, _backofficeSettings.WebNewsPictureUri, (lastShopDetailId).ToString(), true, 1600).PictureUri; ShopConfigDetailModel.PictureMobileUri = _service.SaveFile(ShopConfigDetailModel.Picture, _backofficeSettings.WebNewsPictureFullPath, _backofficeSettings.WebNewsPictureUri, "Mobile" + (lastShopDetailId).ToString(), true, 525).PictureUri; } if (ShopConfigDetailModel.PictureWebp?.Length > 0) { ShopConfigDetailModel.PictureWebpUri = _service.SaveFile(ShopConfigDetailModel.PictureWebp, _backofficeSettings.WebNewsPictureFullPath, _backofficeSettings.WebNewsPictureUri, (lastShopDetailId).ToString()).PictureUri; } _context.ShopConfigDetails.Add(_mapper.Map <ShopConfigDetail>(ShopConfigDetailModel)); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { await PopulateListAsync(); return(Page()); } if (!CatalogCategoryModel.Any(x => x.Selected)) { await PopulateListAsync(); ModelState.AddModelError("", "Selecciona pelo menos uma categoria"); return(Page()); } CatalogItem.Sku = _context.CatalogTypes.Find(CatalogItem.CatalogTypeId).Code; //Save Main Image if (Picture?.Length > 0) { var lastCatalogItemId = (await _context.CatalogItems.AnyAsync()) ? (await _context.CatalogItems.LastAsync()).Id : 0; CatalogItem.PictureUri = _service.SaveFile(Picture, _backofficeSettings.GroceryProductsPictureFullPath, _backofficeSettings.GroceryProductsPictureUri, (++lastCatalogItemId).ToString(), true, 500, 500).PictureUri; } //Catalog Catagories CatalogItem.CatalogCategories = new List <CatalogCategory>(); foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList()) { CatalogItem.CatalogCategories.Add(new CatalogCategory { CategoryId = item.CategoryId }); foreach (var child in item.Childs.Where(x => x.Selected).ToList()) { CatalogItem.CatalogCategories.Add(new CatalogCategory { CategoryId = child.CategoryId }); } } _context.CatalogItems.Add(CatalogItem); await _context.SaveChangesAsync(); //Update SKU CatalogItem.Sku += "-" + CatalogItem.Id; await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
private string SaveFile(IFormFile picture, bool resize, bool isMobile, int width = 0, int height = 0) { string addToFileName = isMobile ? "Mobile" + ShopConfigDetailModel.Id.ToString() : ShopConfigDetailModel.Id.ToString(); return(_service.SaveFile(picture, _backofficeSettings.WebNewsPictureFullPath, _backofficeSettings.WebNewsPictureUri, addToFileName, resize, width, height).PictureUri); }
public async Task <IActionResult> OnPostSaveAsync() { if (!ModelState.IsValid) { await PopulateLists(); return(Page()); } if (!ValidatePictures()) { await PopulateLists(); return(Page()); } //Fix Slug ProductModel.Slug = Utils.URLFriendly(ProductModel.Slug); if ((await SlugExistsAsync(ProductModel.Slug))) { ModelState.AddModelError("ProductModel.Slug", "Já existe um slug com o mesmo nome!"); await PopulateLists(); return(Page()); } ProductModel.Sku = await _service.GetSku(ProductModel.CatalogTypeId, ProductModel.CatalogIllustrationId); ProductModel.Price = ProductModel.Price == 0 ? default(decimal?) : ProductModel.Price; var prod = _mapper.Map <CatalogItem>(ProductModel); //Save Main Image if (ProductModel.Picture?.Length > 0) { var lastCatalogItemId = 0; if (_context.CatalogItems.Any()) { lastCatalogItemId = GetLastCatalogId(); } var info = _service.SaveFile(ProductModel.Picture, _backofficeSettings.WebProductsPictureV2FullPath, _backofficeSettings.WebProductsPictureV2Uri, (++lastCatalogItemId).ToString(), true, 700, 700); prod.UpdateMainPicture(info.PictureUri); prod.AddPicture(new CatalogPicture(true, true, info.PictureUri, 0, info.PictureHighUri)); } //Save other images if (ProductModel.OtherPictures?.Count > 0) { var lastCatalogPictureId = _context.CatalogPictures.Count() > 0 ? GetLastCatalogPictureId() : 0; var order = 0; foreach (var item in ProductModel.OtherPictures) { var info = _service.SaveFile(item, _backofficeSettings.WebProductsPictureV2FullPath, _backofficeSettings.WebProductsPictureV2Uri, (++lastCatalogPictureId).ToString(), true, 700, 700); prod.AddPicture(new CatalogPicture(true, false, info.PictureUri, ++order, info.PictureHighUri)); } } //Categories foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList()) { prod.AddCategory(item.CategoryId); foreach (var child in item.Childs.Where(x => x.Selected).ToList()) { prod.AddCategory(child.CategoryId); } } //Save Changes _context.CatalogItems.Add(prod); await _context.SaveChangesAsync(); //Update Sku prod.UpdateSku(prod.Sku + "_" + prod.Id); 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 async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { await PopulateLists(); return(Page()); } //Validade Pictures if (!ValidatePictures()) { await PopulateLists(); return(Page()); } //Fix Slug ProductModel.Slug = Utils.URLFriendly(ProductModel.Slug); if ((await SlugExistsAsync(ProductModel.Id, ProductModel.Slug))) { ModelState.AddModelError("ProductModel.Slug", "Já existe um slug com o mesmo nome!"); await PopulateLists(); return(Page()); } //Validate SKU ProductModel.Sku = await _service.GetSku(ProductModel.CatalogTypeId, ProductModel.CatalogIllustrationId) + "_" + ProductModel.Id; //Main Picture if (ProductModel.Picture != null && ProductModel.Picture.Length > 0) { if (!string.IsNullOrEmpty(ProductModel.PictureUri)) { _service.DeleteFile(_backofficeSettings.WebProductsPictureV2FullPath, Utils.GetFileName(ProductModel.PictureUri)); } var pictureInfo = _service.SaveFile(ProductModel.Picture, _backofficeSettings.WebProductsPictureV2FullPath, _backofficeSettings.WebProductsPictureV2Uri, ProductModel.Id.ToString(), true, 700, 700); ProductModel.PictureUri = pictureInfo.PictureUri; ProductModel.PictureHighUri = pictureInfo.PictureHighUri; } ////Update images //foreach (var item in ProductModel.CatalogPictures.Where(x => x.Picture != null && !x.ToRemove).ToList()) //{ // _service.DeleteFile(_backofficeSettings.WebProductsPictureFullPath, Utils.GetFileName(item.PictureUri)); // item.PictureUri = (await _service.SaveFileAsync(item.Picture, _backofficeSettings.WebProductsPictureFullPath, _backofficeSettings.WebProductsPictureUri, item.Id.ToString())).PictureUri; //} //Save other images if (ProductModel.OtherPictures != null) { var order = ProductModel.Pictures.Count == 0 ? 0 : ProductModel.Pictures.Max(x => x.Order); var lastCatalogPictureId = _context.CatalogPictures.Count() > 0 ? GetLastCatalogPictureId() : 0; foreach (var item in ProductModel.OtherPictures) { var info = _service.SaveFile(item, _backofficeSettings.WebProductsPictureV2FullPath, _backofficeSettings.WebProductsPictureV2Uri, (++lastCatalogPictureId).ToString(), true, 700, 700); ProductModel.Pictures.Add(new ProductPictureViewModel { IsActive = true, Order = ++order, IsMain = false, PictureUri = info.PictureUri, PictureHighUri = info.PictureHighUri }); } } //Save Changes ProductModel.Price = ProductModel.Price == 0 ? default(decimal?) : ProductModel.Price; //var prod = _mapper.Map<CatalogItem>(ProductModel); var prod = await _context.CatalogItems .Include(p => p.Categories) .Include(p => p.Pictures) .SingleOrDefaultAsync(m => m.Id == ProductModel.Id); prod.UpdateCatalogItem(ProductModel.Name, ProductModel.Slug, ProductModel.Description, ProductModel.CatalogIllustrationId, ProductModel.CatalogTypeId, ProductModel.PictureUri, ProductModel.Price, ProductModel.Discount, ProductModel.IsFeatured, ProductModel.IsNew, ProductModel.ShowOnShop, ProductModel.CanCustomize, ProductModel.IsUnavailable, ProductModel.Sku, ProductModel.Stock, ProductModel.MetaDescription, ProductModel.Title); //Main Picture if (!string.IsNullOrEmpty(ProductModel.PictureUri)) { var mainPic = prod.Pictures.SingleOrDefault(x => x.IsMain); if (mainPic == null) { prod.AddPicture(new CatalogPicture(true, true, ProductModel.PictureUri, 0, ProductModel.PictureHighUri)); } else { prod.Pictures.SingleOrDefault(x => x.IsMain).UpdatePictureUri(ProductModel.PictureUri); } } //Other pictutes foreach (var item in ProductModel.Pictures.Where(x => !x.IsMain).ToList()) { var otherPicture = item.Id != 0 ? prod.Pictures.SingleOrDefault(x => x.Id == item.Id) : null; if (item.ToRemove && otherPicture != null) { _context.Entry(otherPicture).State = EntityState.Deleted; _service.DeleteFile(_backofficeSettings.WebProductsPictureV2FullPath, Utils.GetFileName(item.PictureUri)); } else if (otherPicture != null) { otherPicture.UpdatePictureInfo(item.IsActive, item.Order, item.PictureUri, item.PictureHighUri); } else { prod.AddPicture(new CatalogPicture(item.IsActive, false, item.PictureUri, item.Order, item.PictureHighUri)); } } //Categorias dos Produtos var catalogCategoriesDb = await _context.CatalogCategories.Where(x => x.CatalogItemId == prod.Id).ToListAsync(); //Novos foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList()) { if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == item.CategoryId)) { prod.AddCategory(item.CategoryId); } foreach (var child in item.Childs.Where(x => x.Selected).ToList()) { if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == child.CategoryId)) { prod.AddCategory(child.CategoryId); } } } //Remover foreach (var item in CatalogCategoryModel.Where(x => x.Id != 0).ToList()) { if (!item.Selected) { _context.CatalogCategories.Remove(_context.CatalogCategories.Find(item.Id)); } foreach (var child in item.Childs.Where(x => x.Id != 0 && !x.Selected).ToList()) { _context.CatalogCategories.Remove(_context.CatalogCategories.Find(child.Id)); } } //_context.Attach(prod).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { await PopulateLists(); return(Page()); } if (!CatalogCategoryModel.Any(x => x.Selected)) { await PopulateLists(); ModelState.AddModelError("", "Selecciona pelo menos uma categoria"); return(Page()); } //Sku CatalogItem.Sku = $"{_context.CatalogTypes.Find(CatalogItem.CatalogTypeId).Code}-{CatalogItem.Id}"; //Save Image if (Picture?.Length > 0) { if (!string.IsNullOrEmpty(CatalogItem.PictureUri)) { _service.DeleteFile(_backofficeSettings.GroceryProductsPictureFullPath, Utils.GetFileName(CatalogItem.PictureUri)); } CatalogItem.PictureUri = _service.SaveFile(Picture, _backofficeSettings.GroceryProductsPictureFullPath, _backofficeSettings.GroceryProductsPictureUri, CatalogItem.Id.ToString(), true, 500, 500).PictureUri; } //CatalogCategories var catalogCategoriesDb = await _context.CatalogCategories .Include(x => x.Category) .Where(x => x.CatalogItemId == CatalogItem.Id) .ToListAsync(); //Novos foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList()) { if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == item.CategoryId)) { CatalogItem.CatalogCategories.Add(new CatalogCategory { CategoryId = item.CategoryId }); } foreach (var child in item.Childs.Where(x => x.Selected).ToList()) { if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == child.CategoryId)) { CatalogItem.CatalogCategories.Add(new CatalogCategory { CategoryId = child.CategoryId }); } } } //Remover foreach (var item in CatalogCategoryModel.ToList()) { if (!item.Selected) { var catalogCategory = catalogCategoriesDb.SingleOrDefault(x => x.CategoryId == item.CategoryId); if (catalogCategory != null) { _context.Entry(catalogCategory).State = EntityState.Deleted; } } foreach (var child in item.Childs.Where(x => !x.Selected).ToList()) { var catalogCategory = catalogCategoriesDb.SingleOrDefault(x => x.CategoryId == child.CategoryId); if (catalogCategory != null) { _context.Entry(catalogCategory).State = EntityState.Deleted; } } } _context.Attach(CatalogItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CatalogItemExists(CatalogItem.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }