Esempio n. 1
0
        public void Edit(DTOExport model)
        {
            Export export = _mapper.Map <Export>(model);
            List <ExportDetail> newDetails = _mapper.Map <List <ExportDetail> >(model.ExportProductList);
            List <ExportDetail> oldDetails = _context.ExportDetails
                                             .Where(x => x.ExportId == model.Id).ToList();

            _context.Update(export);

            if (newDetails != null && newDetails.Count > 0)
            {
                _context.RemoveRange(oldDetails);
                if (newDetails != null)
                {
                    foreach (var nd in newDetails)
                    {
                        nd.ExportId = export.Id;
                        double productPrice = _context.ProductPrices
                                              .Where(x => x.ProductId == nd.ProductId && !x.IsDeleted)
                                              .FirstOrDefault().Price;
                        nd.Price = nd.Quantity * productPrice - (nd.Quantity * productPrice * nd.Discount / 100);
                    }
                    _context.AddRange(newDetails);
                }
            }
            _context.SaveChanges();
        }
        public bool Edit(DTOProduct model)
        {
            try
            {
                if (model.Id == 0)
                {
                    return(false);
                }

                Product product = _context.Products.Find(model.Id);

                if (product == null)
                {
                    return(false);
                }


                product.Name        = model.Name;
                product.Description = model.Description;
                if (model.CategoryBrand.BrandId > 0 && model.CategoryBrand.CategoryId > 0)
                {
                    product.CategoryBrandId = _context.CategoriesBrands.Where(x => x.BrandId == model.CategoryBrand.BrandId && x.CategoryId == model.CategoryBrand.CategoryId).Select(x => x.Id).FirstOrDefault();
                }


                ProductPrice productPrice = _context.ProductPrices.FirstOrDefault(x => x.ProductId == product.Id && x.IsDeleted == false);

                if (productPrice.Price != model.ProductPrice.Price) // Ukoliko se cijena ne slaze sa postojecom cijenom to znaci da je doslo do promjene cijene. Staru cijenu postavljamo na IsDeleted a dodajemo novu
                {
                    productPrice.IsDeleted = true;
                    ProductPrice newProductPrice = _mapper.Map <ProductPrice>(model.ProductPrice);

                    newProductPrice.ProductId = product.Id;
                    if (newProductPrice != null)
                    {
                        _context.ProductPrices.Add(newProductPrice);
                        productPrice.IsDeleted = true;
                    }
                }

                List <ProductAttribute> oldProductAttributes = _context.ProductAttributes.Where(x => x.ProductId == product.Id).ToList();
                _context.RemoveRange(oldProductAttributes);

                List <ProductAttribute> newProductAttributes = _mapper.Map <List <ProductAttribute> >(model.ProductAttributeList);

                if (newProductAttributes != null)
                {
                    foreach (var attr in newProductAttributes)
                    {
                        attr.ProductId = product.Id;
                    }

                    _context.ProductAttributes.AddRange(newProductAttributes);
                }



                List <ProductShelf> oldProductShelves = _context.ProductShelves.Where(x => x.ProductId == product.Id).ToList();
                _context.RemoveRange(oldProductShelves);

                List <ProductShelf> newProductShelves = _mapper.Map <List <ProductShelf> >(model.productInventoryList);

                if (newProductShelves != null)
                {
                    foreach (var attr in newProductShelves)
                    {
                        attr.ProductId = product.Id;
                    }

                    _context.ProductShelves.AddRange(newProductShelves);
                }

                _context.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }