Ejemplo n.º 1
0
        public async Task ModifyProductAsync(EditProductOutDto dto)
        {
            Product product = await productsRepository.All()
                              .Include(x => x.Votes)
                              .Include(x => x.Characteristics)
                              .Include(x => x.ProductPictures)
                              .Include(x => x.ProductComments)
                              .FirstOrDefaultAsync(x => x.Id == dto.Id);

            product.Name           = dto.Name;
            product.CategoryId     = dto.CategoryId;
            product.ManufacturerId = dto.ManufacturerId;
            product.Price          = dto.Price;
            product.Discount       = dto.Discount;
            product.Quantity       = dto.Quantity;
            product.Weight         = dto.Weight;
            product.MonthsWarranty = dto.MonthsWarranty;
            product.MainPicURL     = dto.MainPicURL.Contains("res.cloudinary.com") ? dto.MainPicURL : cloudineryService.RelocateImgToCloudinary(product.Name + "mainPic", dto.MainPicURL, info: Guid.NewGuid().ToString());

            product.ReviewURL   = dto.ReviewURL;
            product.Description = dto.Description;
            product.IsDeleted   = dto.IsDeleted;

            if (!dto.VotesAny && product.Votes.Any())
            {
                product.Votes = new HashSet <ProductVote>();
            }

            product.Characteristics = new HashSet <ProductCharacteristic>();
            foreach (NewProductCharacteristicDto dtoChar in dto.Characteristics)
            {
                product.Characteristics.Add(mapper.Map <ProductCharacteristic>(dtoChar));
            }

            for (int i = 0; i < dto.ProductPictures.Count(); i++)
            {
                NewProductPictureDto pictureDto = dto.ProductPictures[i];
                if (!pictureDto.PictureURL.Contains("res.cloudinary.com"))
                {
                    pictureDto.PictureURL = cloudineryService.RelocateImgToCloudinary(product.Id + dto.Id + dto.Name + "altPic" + i, pictureDto.PictureURL, Guid.NewGuid().ToString());
                }
            }
            product.ProductPictures = dto.ProductPictures.Select(x => mapper.Map <ProductPicture>(x)).ToList();

            foreach (var dtoComment in dto.ProductComments)
            {
                var productComment = product.ProductComments.FirstOrDefault(x => x.Id == dtoComment.Id);
                if (productComment is null)
                {
                    continue;
                }
                productComment.IsDeleted = dtoComment.IsDeleted;
                productComment.Comment   = dtoComment.Comment;
            }

            //lock (ConcurencyMaster.LockProductsObj)
            //{
            productsRepository.SaveChangesAsync().GetAwaiter().GetResult();
            //}
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(int id)
        {
            EditProductOutDto productDto = await productsService.GetEditableProductAsync(id);

            ViewData["Categories"]    = categoryService.GetAllMinified();
            ViewData["Manufacturers"] = manufacturerService.GetAllMinified();
            return(View(productDto));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(EditProductOutDto dto)
        {
            if (!string.IsNullOrEmpty(dto.ChangeCount))
            {
                if (dto.ChangeCount == "pic++")
                {
                    dto.ProductPictures.Add(new NewProductPictureDto());
                }
                if (dto.ChangeCount == "char++")
                {
                    dto.Characteristics.Add(new NewProductCharacteristicDto());
                }
                if (dto.ChangeCount.StartsWith("com++"))
                {
                    dto.ProductComments[GetTargetIndex(dto.ChangeCount)].IsDeleted = false;
                }

                if (dto.ChangeCount.StartsWith("pic--"))
                {
                    dto.ProductPictures.RemoveAt(GetTargetIndex(dto.ChangeCount));
                }
                if (dto.ChangeCount.StartsWith("char--"))
                {
                    dto.Characteristics.RemoveAt(GetTargetIndex(dto.ChangeCount));
                }
                if (dto.ChangeCount.StartsWith("com--"))
                {
                    dto.ProductComments[GetTargetIndex(dto.ChangeCount)].IsDeleted = true;
                }
                bool validState = ModelState.IsValid;
                var  errors     = ModelState.SelectMany(x => x.Value.Errors).Select(x => x.ErrorMessage).ToArray();

                ViewData["Categories"] = categoryService.GetAllMinified();

                ViewData["Manufacturers"] = manufacturerService.GetAllMinified();
                return(View(dto));
            }

            if (ModelState.IsValid)
            {
                memoryCache.Remove(GlobalConstants.CasheCategoriesInButtonName);
                memoryCache.Remove(GlobalConstants.CasheManufactorersInButtonName);
                await productsService.ModifyProductAsync(dto);

                return(RedirectToAction("Details", "Products", new { area = "", id = dto.Id }));
            }
            ViewData["Categories"]    = categoryService.GetAllMinified();
            ViewData["Manufacturers"] = manufacturerService.GetAllMinified();
            return(this.View(dto));
        }