Пример #1
0
        public ActionResult Create(CreateProductViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var productEntity = new Product()
            {
                Name  = viewModel.Name,
                Price = viewModel.Price,
                Type  = viewModel.Type
            };

            productEntity.Image = ImageHelper.GetBytesFromFile(viewModel.Picture);

            if (productEntity.Image == null)
            {
                this.AddNotification("Невалиден файл: Може да качите само файлове от тип: jpg, jpeg, png, gif", NotificationType.ERROR);
                return(View(viewModel));
            }

            productEntity.ImageUrl = ImageHelper.GetImageUrl(productEntity.Image);

            AddIngredientsToProduct(productEntity, viewModel.Ingredients);

            _productsRepository.Add(productEntity);
            _productsRepository.Save();

            this.AddNotification("Продуктът е създаден успешно.", NotificationType.SUCCESS);
            return(RedirectToAction("Products", "Administrators"));
        }
        public ActionResult Add(ProductsAddViewModel viewModel)
        {
            // ValidateProduct(viewModel.Product);

            if (ModelState.IsValid)
            {
                var product   = viewModel.Product;
                var inventory = viewModel.Inventory;


                _inventoriesRepository.Add(inventory);
                var inventoryId = _inventoriesRepository.GetLast(includeRelatedEntoties: true).Id;

                product.InventoryId = inventoryId;

                _productsRepository.Add(product);


                TempData["Message"] = "Votre produit a été ajouté a la liste.";

                return(RedirectToAction("Index"));
            }

            viewModel.Init(_productTypesRepository, _suppliersRepository, _unitsRepository, User.Identity.GetUserId());

            return(View(viewModel));
        }
Пример #3
0
        public IActionResult Post([FromBody] Product value)
        {
            var repository = new ProductsRepository();

            repository.Add(value);

            return(CreatedAtAction(nameof(Get), new { id = value.ID }, value));
        }
Пример #4
0
 public bool Add(Products products)
 {
     if (products != null)
     {
         productsRepository.Add(products);
         return(true);
     }
     return(false);
 }
Пример #5
0
        static void Main(string[] args)
        {
            Product p = new Product();

            p.Name = "Matias";
            ProductsRepository pr = new ProductsRepository();

            pr.Add(p);
            Console.Write(p.Name + p.Id);
        }
Пример #6
0
        public ActionResult Add(Product product)
        {
            var viewModel = new ProductsViewModel();

            _productsRepository.Add(product);

            viewModel.Products = _productsRepository.GetAll();

            return(View("List", viewModel));
        }
 public void WhenAProduct_IsCreated()
 {
     var productsRepository = new ProductsRepository();
     var currencyGenerator = new CurrencyGenerator();
     var product = new Product
     {
         Name = NameGenerator.GenerateName(),
         Price = currencyGenerator.GenerateAmount()
     };
     var results = productsRepository.Add(product).Result;
     Console.WriteLine(results);
 }
Пример #8
0
        public async Task <ActionResult> Create([FromForm] ProductForm productForm)
        {
            var productRepsitory = new ProductsRepository(_context, _environment);

            var prod = await productRepsitory.Add(productForm);

            if (prod == null)
            {
                return(BadRequest(new { status = false, message = "Failed !" }));
            }
            return(CreatedAtAction(nameof(GetByID), new{ id = prod.Id }, prod));
        }
        public void WhenAProduct_IsCreated()
        {
            var productsRepository = new ProductsRepository();
            var currencyGenerator  = new CurrencyGenerator();
            var product            = new Product
            {
                Name  = NameGenerator.GenerateName(),
                Price = currencyGenerator.GenerateAmount()
            };
            var results = productsRepository.Add(product).Result;

            Console.WriteLine(results);
        }
Пример #10
0
        public JsonResult SaveProducts(Products data)
        {
            if (data.ProductID == 0)
            {
                productrepo.Add(data);
            }
            else
            {
                productrepo.Update(data);
            }

            return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
        }
        public async Task <IActionResult> Create([FromBody] Product product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            var productEntity = _mapper.Map <Domain.Products.Product>(product);

            productEntity = await _repository.Add(productEntity);

            return(CreatedAtAction(nameof(GetProduct),
                                   new { id = productEntity.Id },
                                   _mapper.Map <Product>(productEntity)));
        }
 public void When_10Products_AreCreated()
 {
     var productsRepository = new ProductsRepository();
     var currencyGenerator = new CurrencyGenerator(maximumValue: 15000.0);
     for (var iCtr = 0; iCtr < 1000; iCtr++)
     {
         var product = new Product
         {
             Name = NameGenerator.GenerateName(),
             Price = currencyGenerator.GenerateAmount()
         };
         var results = productsRepository.Add(product).Result;
         Console.WriteLine(results);
     }
 }
        public void When_10Products_AreCreated()
        {
            var productsRepository = new ProductsRepository();
            var currencyGenerator  = new CurrencyGenerator(maximumValue: 15000.0);

            for (var iCtr = 0; iCtr < 1000; iCtr++)
            {
                var product = new Product
                {
                    Name  = NameGenerator.GenerateName(),
                    Price = currencyGenerator.GenerateAmount()
                };
                var results = productsRepository.Add(product).Result;
                Console.WriteLine(results);
            }
        }
Пример #14
0
        public void GetProductById_Returns_Product()
        {
            var productsRepository = new ProductsRepository();
            var added = productsRepository.Add(new Product
            {
                Name        = "xUnit",
                Description = "xUnit",
                Price       = 10m,
                ImagePath   = "/default.png",
                Stock       = 10
            });
            var product = productsRepository.GetById(added.Id);

            Assert.IsType <Product>(product);
            Assert.Equal(added.Id, product.Id);
        }
Пример #15
0
        public void AddProduct_Adds_Product()
        {
            var productsRepository = new ProductsRepository();
            var product            = productsRepository.Add(new Product
            {
                Name        = "xUnit",
                Description = "xUnit",
                Price       = 10m,
                ImagePath   = "/default.png",
                Stock       = 10
            });

            //assumes GetById works
            var dbProduct = productsRepository.GetById(product.Id);

            Assert.Equal(product, dbProduct);
        }
        public void InitializeDb()
        {
            var connectionString   = ConfigurationManager.AppSettings["DbConnectionString"];
            var apiUrl             = ConfigurationManager.AppSettings["ApiUrl"];
            var productsRepository = new ProductsRepository(connectionString);
            var barcodesRepository = new BarcodesRepository(connectionString);
            var productsApiService = new ProductsApiService(apiUrl);
            var allProducts        = productsApiService.GetAllProducts();
            var context            = new RegisterDbContext(connectionString);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            var barcodes = allProducts.Data.SelectMany(s => s.BarCodes);

            productsRepository.Add(allProducts.Data);
            barcodesRepository.Add(barcodes);
        }
Пример #17
0
        public int?Create(NewProductViewModel product)
        {
            if (!ModelState.IsValid)
            {
                return(null);
            }
            var prod = new Product();

            prod.Title            = product.Title;
            prod.ShortDescription = product.ShortDescription;
            prod.Description      = HttpUtility.UrlDecode(product.Description, System.Text.Encoding.Default);
            prod.BrandId          = product.Brand;
            prod.ProductGroupId   = product.ProductGroup;
            prod.Rate             = product.Rate;
            prod.ShortDescription = product.ShortDescription;
            var addProduct = _repo.Add(prod);

            #region Adding Product Features

            foreach (var feature in product.ProductFeatures)
            {
                if (feature.IsMain)
                {
                    var model = new ProductMainFeature();
                    model.ProductId    = addProduct.Id;
                    model.FeatureId    = feature.FeatureId;
                    model.SubFeatureId = feature.SubFeatureId;
                    model.Value        = feature.Value;
                    model.Quantity     = feature.Quantity ?? 0;
                    model.Price        = feature.Price ?? 0;
                    _repo.AddProductMainFeature(model);
                }
                else
                {
                    var model = new ProductFeatureValue();
                    model.ProductId    = addProduct.Id;
                    model.FeatureId    = feature.FeatureId;
                    model.SubFeatureId = feature.SubFeatureId;
                    model.Value        = feature.Value;
                    _repo.AddProductFeature(model);
                }
            }
            #endregion
            return(addProduct.Id);
        }
Пример #18
0
        public void RemoveById_Removes_Product()
        {
            var productsRepository = new ProductsRepository();
            // add a product to remove
            var product = productsRepository.Add(new Product
            {
                Name        = "xUnit",
                Description = "xUnit",
                Price       = 10m,
                ImagePath   = "/default.png",
                Stock       = 10
            });

            productsRepository.RemoveById(product.Id);
            // assumes GetAll works
            var products = productsRepository.GetAll();

            Assert.DoesNotContain(products, p => p.Id == product.Id);
        }
Пример #19
0
        public void Add_CorrectProduct_Success()
        {
            //arrange
            EFDbContext        context    = new EFDbContext(_options);
            ProductsRepository repository = new ProductsRepository(context);

            Product product = new Product
            {
                Name        = "Testowy",
                Description = "LoremipsumLoremipsumLoremipsumLoremipsumLoremipsum"
            };

            //act
            repository.Add(product);

            var result = context.Products.Where(p => p.Name == "Testowy").FirstOrDefault();

            //assert
            Assert.AreEqual(result.Name, product.Name);
        }
Пример #20
0
        public void ChangeStock_Changes_ProductStock()
        {
            var productsRepository = new ProductsRepository();
            var added = productsRepository.Add(new Product
            {
                Name        = "xUnit",
                Description = "xUnit",
                Price       = 10m,
                ImagePath   = "/default.png",
                Stock       = 10
            });

            //change and test twice with different values to make sure the stock actually changed
            productsRepository.ChangeStock(added.Id, 5);
            var product = productsRepository.GetById(added.Id);

            Assert.Equal(5, product.Stock);

            productsRepository.ChangeStock(added.Id, 4);
            product = productsRepository.GetById(added.Id);
            Assert.Equal(4, product.Stock);
        }
        public ActionResult AddingProduct(MallOfElectronics.Models.DataBase.Product product, HttpPostedFileBase UploadImage)
        {
            if (ModelState.IsValid)
            {
                string physicalPath = "", ImageName = "Images/ProductsImages/";
                if (UploadImage != null)
                {
                    physicalPath = Server.MapPath("~") + "Images\\ProductsImages\\" + UploadImage.FileName;
                    UploadImage.SaveAs(physicalPath);
                    ImageName += UploadImage.FileName;
                }
                else
                {
                    ImageName += "EmptyProductsImage.jpg";
                }
                product.Image = ImageName;

                ProductsRepository productRepository = new ProductsRepository();
                if (productRepository.Add(product))
                {
                    ViewBag.Message = "This Product Has Been Added";
                }
                else
                {
                    if (UploadImage != null)
                    {
                        System.IO.File.Delete(physicalPath);
                    }
                    ViewBag.ErrorMessage = "This Product Not Has Been Added";
                }
            }
            else
            {
                ViewBag.ErrorMessage = "This Product Not Has Been Added";
            }
            return(View());
        }
        public IActionResult CreateProduct(Products product)
        {
            _repo.Add(product);

            return(Created($"/api/products/{product.ProductId}", product));
        }
Пример #23
0
 public bool Add(Product product)
 {
     return(productsRepository.Add(product));
 }