Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.CatalogItems
                          .Include(x => x.Pictures)
                          .SingleOrDefaultAsync(x => x.Id == id);

            if (product != null)
            {
                _context.CatalogItems.Remove(product);
                await _context.SaveChangesAsync();
            }

            //Delete the images of the product
            if (!string.IsNullOrEmpty(product.PictureUri))
            {
                _service.DeleteFile(_backofficeSettings.WebProductsPictureFullPath, Utils.GetFileName(product.PictureUri));
            }

            if (product.Pictures != null)
            {
                foreach (var item in product.Pictures)
                {
                    if (!string.IsNullOrEmpty(item.PictureUri))
                    {
                        _service.DeleteFile(_backofficeSettings.WebProductsPictureFullPath, Utils.GetFileName(item.PictureUri));
                    }
                }
            }

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 2
0
        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"));
        }
Ejemplo n.º 3
0
        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"));
        }
Ejemplo n.º 4
0
        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"));
        }
Ejemplo n.º 5
0
        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"));
        }