Exemple #1
0
        public void ChangeCategoryEntryState(Category category, EntityState entityState)
        {
            if (category == null)
            {
                throw new ArgumentNullException("category");
            }

            using (var context = new DamaContext())
            {
                context.Entry(category).State = entityState;
                context.SaveChanges();
            }
        }
Exemple #2
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"));
        }
Exemple #3
0
 public virtual void Update(User entityToUpdate)
 {
     dbSet.Attach(entityToUpdate);
     context.Entry(entityToUpdate).State = EntityState.Modified;
 }