Exemple #1
0
        public async Task <ActionResult <DTOProduct> > Post([FromBody] DTOProduct product)
        {
            var newProduct = _mapper.Map <Product>(product);
            await _slProduct.AddProductAsync(newProduct);

            return(Ok(new { Response = "El producto se ha agregado correctamente" }));
        }
Exemple #2
0
        public IActionResult Add()
        {
            DTOProduct model = new DTOProduct();

            model.ProductPrice = new DTOProductPrice();
            return(View(model));
        }
Exemple #3
0
        public DTOProduct GetProduct(int productId)
        {
            Product    product = _entities.Products.SingleOrDefault(s => s.ProductID.Equals(productId));
            DTOProduct dto     = Mapper.Map <Product, DTOProduct> (product);

            return(dto);
        }
        public DTOProduct GetByIdDto(int id)
        {
            Product product = _context.Products.FirstOrDefault(x => x.IsDeleted == false && x.Id == id);

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

            List <ProductAttribute> productAttributes = _context.ProductAttributes.Include(x => x.Attribute).Where(x => x.ProductId == product.Id).ToList();
            List <ProductShelf>     productShelves    = _context.ProductShelves.Where(x => x.ProductId == product.Id)
                                                        .Include(x => x.Shelf)
                                                        .ThenInclude(x => x.Department)
                                                        .ThenInclude(x => x.Inventory)
                                                        .ToList();
            ProductPrice  productPrice  = _context.ProductPrices.FirstOrDefault(x => x.ProductId == product.Id && x.IsDeleted == false);
            CategoryBrand categoryBrand = _context.CategoriesBrands.Include(x => x.Category)
                                          .Include(x => x.Brand)
                                          .FirstOrDefault(x => x.Id == product.CategoryBrandId);
            DTOProduct model = _mapper.Map <DTOProduct>(product);

            model.Attributes     = _mapper.Map <List <DTOProductAttribute> >(productAttributes);
            model.ProductPrice   = _mapper.Map <DTOProductPrice>(productPrice);
            model.ProductShelves = _mapper.Map <List <DTOProductShelf> >(productShelves);
            model.CategoryBrand  = _mapper.Map <DTOCategoryBrand>(categoryBrand);

            return(model);
        }
        public JsonResult UpdateProduct(DTOProduct dto)
        {
            var rp = new ProductRepository( );

            rp.UpdateProduct(dto);

            return(Json(true, JsonRequestBehavior.DenyGet));
        }
Exemple #6
0
        public IActionResult Details(int ProductId)
        {
            DTOProduct model = new DTOProduct();

            model = _unitOfWork.Products.GetDetailsByIdDto(ProductId);
            if (model != null)
            {
                return(PartialView("Details", model));
            }

            return(RedirectToAction(nameof(Index)));
        }
        public void Add(DTOProduct model)
        {
            try
            {
                Product product = _mapper.Map <Product>(model);
                if (model.BrandId > 0 && model.CategoryId > 0)
                {
                    product.CategoryBrandId = _context.CategoriesBrands.Where(x => x.BrandId == model.BrandId && x.CategoryId == model.CategoryId).Select(x => x.Id).FirstOrDefault();
                }

                ProductPrice            productPrice      = _mapper.Map <ProductPrice>(model.ProductPrice);
                List <ProductAttribute> productAttributes = _mapper.Map <List <ProductAttribute> >(model.ProductAttributeList);
                List <ProductShelf>     productShelves    = _mapper.Map <List <ProductShelf> >(model.productInventoryList);


                _context.Products.Add(product);
                _context.SaveChanges();
                productPrice.ProductId = product.Id;

                if (productPrice != null)
                {
                    _context.ProductPrices.Add(productPrice);
                }

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

                    _context.ProductAttributes.AddRange(productAttributes);
                }

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

                    _context.ProductShelves.AddRange(productShelves);
                }

                _context.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #8
0
        public void Test1()
        {
            var Products = new DTOProduct("A")
            {
                Price = 20
            };
            List <DTOProduct> listp = new List <DTOProduct>();

            listp.Add(Products);
            var mock   = new Mock <IPromotionService>();
            var result = mock.Setup(x => x.GetTotalPriceService(listp));

            Assert.NotNull(result);
        }
Exemple #9
0
        public DTOProduct ToDTO(Product product)
        {
            DTOProduct dTOProduct = new DTOProduct();

            dTOProduct.ID           = product.ID;
            dTOProduct.Name         = product.Name;
            dTOProduct.SellingPrice = product.SellingPrice;
            dTOProduct.Stock        = product.Stock;
            dTOProduct.Tax          = product.Tax;
            dTOProduct.BuyingPrice  = product.BuyingPrice;
            dTOProduct.Code         = product.Code;
            dTOProduct.Barcode      = product.Barcode;
            return(dTOProduct);
        }
        public IHttpActionResult Post(DTOProduct dtoProduct)
        {
            // Create a new Product
            var NewProduct = new Product();

            NewProduct.Name  = dtoProduct.ProductName;
            NewProduct.Price = Convert.ToDecimal(dtoProduct.ProductPrice);
            // Save the Product
            db.Products.Add(NewProduct);
            db.SaveChanges();
            // Populate the ID that was created and pass it back
            dtoProduct.Id = NewProduct.Id;
            // Return the Product
            return(Created(dtoProduct));
        }
Exemple #11
0
        public IActionResult Edit(int Id)
        {
            if (Id == 0)
            {
                return(BadRequest());
            }
            DTOProduct model = new DTOProduct();

            model = _unitOfWork.Products.GetByIdDto(Id);
            if (model != null)
            {
                return(View("Edit", model));
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemple #12
0
        public IActionResult Add(DTOProduct model)
        {
            if (!IsSet(model.Name))
            {
                ModelState.AddModelError(nameof(model.Name), _localizer.ErrNameIsRequired);
            }

            if (model.ProductPrice != null && model.ProductPrice.Price < 0)
            {
                ModelState.AddModelError(nameof(model.ProductPrice.Price), "Cijena je obavezna");
            }

            if (model.BrandId <= 0)
            {
                ModelState.AddModelError(nameof(model.BrandId), "Brend je obavezan");
            }

            if (model.CategoryId <= 0)
            {
                ModelState.AddModelError(nameof(model.CategoryId), "Kategorija je obavezna");
            }

            if (!ModelState.IsValid)
            {
                return(View("Add", model));
            }

            try
            {
                _unitOfWork.Products.Add(model);

                var n = new Notification
                {
                    DateTime = DateTime.Now,
                    Text     = $"New product - {model.Name}",
                    UserId   = int.Parse(_userManager.GetUserId(HttpContext.User))
                };
                _notification.Create(n);
            }
            catch
            {
                return(RedirectToAction(nameof(Index)));
            }


            return(RedirectToAction(nameof(Index)));
        }
Exemple #13
0
        public void UpdateProduct(DTOProduct dto)
        {
            var product = _entities.Products.SingleOrDefault(w => w.ProductID.Equals(dto.Id));

            if (product == null)
            {
                return;
            }

            product.Color        = dto.Color;
            product.ListPrice    = dto.ListPrice;
            product.Name         = dto.Name;
            product.ProductModel = _entities.ProductModels.SingleOrDefault(w => w.Name.Equals(dto.ProductModel));
            var inventory = _entities.ProductInventory.SingleOrDefault(w => w.ProductID.Equals(dto.Id));

            inventory.Quantity = dto.Quantity;

            _entities.SaveChanges( );
        }
        public DTOProduct GetDetailsByIdDto(int productId)
        {
            Product product = _context.Products.FirstOrDefault(x => x.Id == productId && x.IsDeleted == false);

            if (product == null)
            {
                return(null);
            }
            List <ProductAttribute> productAttributes = _context.ProductAttributes.Where(x => x.ProductId == product.Id).Include(x => x.Attribute).ToList();
            List <ProductShelf>     productShelves    = _context.ProductShelves.Where(x => x.ProductId == product.Id).ToList();
            ProductPrice            productPrice      = _context.ProductPrices.FirstOrDefault(x => x.ProductId == product.Id && x.IsDeleted == false);

            DTOProduct model = _mapper.Map <DTOProduct>(product);

            model.Attributes     = _mapper.Map <List <DTOProductAttribute> >(productAttributes);
            model.ProductPrice   = _mapper.Map <DTOProductPrice>(productPrice);
            model.ProductShelves = _mapper.Map <List <DTOProductShelf> >(productShelves);

            return(model);
        }
        public IHttpActionResult Put([FromODataUri] int key, DTOProduct dtoProduct)
        {
            // Get the existing Product using the key that was passed
            Product ExistingProduct = db.Products.Find(key);

            // Did we find a Product?
            if (ExistingProduct == null)
            {
                // If not return NotFound
                return(StatusCode(HttpStatusCode.NotFound));
            }
            // Update the Product
            ExistingProduct.Name  = dtoProduct.ProductName;
            ExistingProduct.Price = Convert.ToDecimal(dtoProduct.ProductPrice);
            // Save changes
            db.Entry(ExistingProduct).State = EntityState.Modified;
            db.SaveChanges();
            // Return the Updated Product
            // Return that the Product was Updated
            return(Updated(ExistingProduct));
        }
Exemple #16
0
        public IActionResult Edit(DTOProduct model)
        {
            if (!IsSet(model.Name))
            {
                ModelState.AddModelError(nameof(model.Name), _localizer.ErrNameIsRequired);
            }

            if (model.ProductPrice != null && model.ProductPrice.Price < 0)
            {
                ModelState.AddModelError(nameof(model.ProductPrice.Price), "Cijena je obavezna");
            }

            if (model.CategoryBrand.BrandId <= 0)
            {
                ModelState.AddModelError(nameof(model.BrandId), "Brend je obavezan");
            }

            if (model.CategoryBrand.CategoryId <= 0)
            {
                ModelState.AddModelError(nameof(model.CategoryId), "Kategorija je obavezna");
            }

            if (!ModelState.IsValid)
            {
                return(View("Edit", model));
            }

            try
            {
                _unitOfWork.Products.Edit(model);
            }
            catch
            {
                return(RedirectToAction(nameof(Index)));
            }
            return(RedirectToAction(nameof(Index)));
        }
        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);
            }
        }