Exemple #1
0
        public async Task <IActionResult> Update([FromBody] UpdateProductViewModel model)
        {
            string strRuta = _config["ProductAvatar"];

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model.Id <= 0)
            {
                return(BadRequest());
            }

            var modelo = await _context.Products.Where(x => x.Id == model.Id).Include(x => x.ProductPriceLists).Include(x => x.ProductProviders).FirstOrDefaultAsync();

            if (modelo == null)
            {
                return(BadRequest("Producto inexistente"));
            }

            modelo.Awaiting           = model.Awaiting;
            modelo.BrandId            = model.BrandId;
            modelo.CategoryId         = model.CategoryId;
            modelo.CheckStock         = model.CheckStock;
            modelo.Codigo             = model.Codigo;
            modelo.Cost               = model.Cost;
            modelo.Description        = model.Description;
            modelo.Discount           = model.Discount;
            modelo.ExchangeCurrencyId = model.ExchangeCurrencyId;
            modelo.Gain               = model.Gain;
            modelo.InStock            = model.InStock;
            modelo.LocationId         = model.LocationId;
            modelo.Name               = model.Name;
            modelo.NameShort          = model.NameShort;
            modelo.OutOfStock         = model.OutOfStock;
            modelo.Price              = model.Price;
            modelo.Stock              = model.Stock;
            modelo.StockMin           = model.StockMin;
            modelo.SubCategoryId      = model.SubCategoryId;



            var providerToBeAdded = model.Providers.Where(a => modelo.ProductProviders.All(
                                                              b => b.ProviderId != a));


            var providerToBeDeleted = modelo.ProductProviders.Where(a => model.Providers.All(
                                                                        b => b != a.ProviderId));


            foreach (var prov in providerToBeDeleted)
            {
                modelo.ProductProviders.Remove(prov);
            }

            foreach (var prov in providerToBeAdded)
            {
                ProductProviders productProviders = new ProductProviders
                {
                    ProviderId = prov,
                    ProductId  = modelo.Id
                };

                modelo.ProductProviders.Add(productProviders);
            }


            foreach (var price in model.ProductPriceLists)
            {
                //Nuevos
                if (price.IsNew && price.IsRemoved == false)
                {
                    ProductPriceLists productPriceLists = new ProductPriceLists
                    {
                        PriceListId = price.PriceList,
                        ProductId   = modelo.Id,
                        Price       = price.Price
                    };
                    modelo.ProductPriceLists.Add(productPriceLists);
                }
                else
                {
                    if (!price.IsNew)
                    {
                        ProductPriceLists productPriceLists = modelo.ProductPriceLists.Where(x => x.Id == price.Id).FirstOrDefault();
                        //Borrados

                        if (price.IsNew == false && price.IsRemoved)
                        {
                            modelo.ProductPriceLists.Remove(productPriceLists);
                        }
                        else
                        {
                            //Actualizo
                            productPriceLists.Price       = price.Price;
                            productPriceLists.PriceListId = price.PriceList;
                        }
                    }
                }
            }

            ////Guardo el avatar
            if (!(string.IsNullOrEmpty(model.LogoName)) && (!string.IsNullOrEmpty(model.Logo)))
            {
                strRuta = strRuta + "//" + modelo.Id.ToString() + "//" + model.LogoName;
                System.IO.FileInfo file = new System.IO.FileInfo(strRuta);
                file.Directory.Create();
                System.IO.File.WriteAllBytes(strRuta, Convert.FromBase64String(model.Logo.Substring(model.Logo.LastIndexOf(',') + 1)));
                modelo.Logo = strRuta;
            }


            _context.Entry(modelo).State = EntityState.Modified;

            if (modelo == null)
            {
                return(NotFound());
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                //Guardar Exception
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }
Exemple #2
0
        public async Task <IActionResult> Create([FromBody] CreateProductViewModel model)
        {
            string strRuta = _config["ProductAvatar"];


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!string.IsNullOrEmpty(model.Codigo))
            {
                //Verifico si el codigo existe
                if (_context.Products.Where(x => x.Codigo == model.Codigo.ToUpper().Trim()).Any())
                {
                    return(BadRequest(new { Message = "Código repetido" }));
                }
            }



            Product modelo = new Product
            {
                Awaiting           = model.Awaiting ?? false,
                BrandId            = model.BrandId,
                CategoryId         = model.CategoryId,
                Codigo             = model.Codigo?.ToUpper().Trim(),
                CompanyId          = model.CompanyId,
                Cost               = model.Cost,
                DateInitial        = DateTime.Now,
                Description        = model.Description,
                Discount           = model.Discount ?? 0,
                Enabled            = true,
                ExchangeCurrencyId = model.ExchangeCurrencyId,
                Gain               = model.Gain,
                InStock            = model.InStock ?? false,
                LocationId         = model.LocationId,
                Name               = model.Name?.Trim(),
                NameShort          = model.NameShort?.Trim(),
                OutOfStock         = model.OutOfStock ?? false,
                Price              = model.Price ?? 0,
                Stock              = model.Stock,
                StockMin           = model.StockMin,
                SubCategoryId      = model.SubCategoryId,
                CheckStock         = model.CheckStock ?? false,
                ProductProviders   = new List <ProductProviders>(),
                ProductPriceLists  = new List <ProductPriceLists>()
            };

            foreach (var prov in model.Providers)
            {
                ProductProviders productProviders = new ProductProviders
                {
                    ProviderId = prov,
                    ProductId  = modelo.Id
                };

                modelo.ProductProviders.Add(productProviders);
            }

            foreach (var price in model.ProductPriceLists)
            {
                ProductPriceLists productPriceLists = new ProductPriceLists
                {
                    PriceListId = price.PriceList,
                    ProductId   = modelo.Id,
                    Price       = price.Price
                };
                modelo.ProductPriceLists.Add(productPriceLists);
            }

            _context.Products.Add(modelo);
            try
            {
                await _context.SaveChangesAsync();



                if (modelo.Id > 0)
                {
                    //Codigo
                    if (string.IsNullOrEmpty(modelo.Codigo))
                    {
                        modelo.Codigo = modelo.Id.ToString();
                    }

                    //Guardo el avatar
                    if (!(string.IsNullOrEmpty(model.LogoName)) && (!string.IsNullOrEmpty(model.Logo)))
                    {
                        strRuta = strRuta + "//" + modelo.Id.ToString() + "//" + model.LogoName;
                        System.IO.FileInfo file = new System.IO.FileInfo(strRuta);
                        file.Directory.Create();
                        System.IO.File.WriteAllBytes(strRuta, Convert.FromBase64String(model.Logo.Substring(model.Logo.LastIndexOf(',') + 1)));
                        modelo.Logo = strRuta;
                    }
                }

                _context.Entry(modelo).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }