Пример #1
0
        public IActionResult Delete(string id)
        {
            var viewModel = new ProductBindingModel()
            {
                Id = id,
            };

            return(this.View(viewModel));
        }
        public ProductBindingModel PrepareProductModelForAdding()
        {
            var model = new ProductBindingModel()
            {
                AddedDate          = DateTime.Now,
                DiscountPercentage = 0
            };

            return(model);
        }
Пример #3
0
 public ActionResult Create()
 {
     if (Session["Product"] == null)
     {
         var product = new ProductBindingModel();
         product.ProductMaterials = new List <ProductMaterialBindingModel>();
         Session["Product"]       = product;
     }
     return(View((ProductBindingModel)Session["Product"]));
 }
Пример #4
0
        public async Task <IActionResult> Create(ProductBindingModel model)
        {
            await _adminProductsService.SaveProduct(model);

            TempData[AdminConstants.MessageType] = AdminConstants.Success;
            TempData[AdminConstants.Message]     = string.Format(AdminConstants.SuccessfullyAdd,
                                                                 AdminConstants.Product);

            return(RedirectToAction(nameof(Index)));
        }
Пример #5
0
        public async Task <IActionResult> Edit(int id, ProductBindingModel model)
        {
            await _adminProductsService.EditProduct(id, model);

            TempData[AdminConstants.MessageType] = AdminConstants.Success;
            TempData[AdminConstants.Message]     = string.Format(AdminConstants.SuccessfullyEdit,
                                                                 AdminConstants.Product);

            return(RedirectToAction(AdminConstants.ActionDetails, new { id }));
        }
        public async Task AddProductAsync(ProductBindingModel model)
        {
            var dbModel = this.mapper.Map <Product>(model);

            await this.MapPhotos(dbModel, model.Photos);

            await this.DbContext.Products.AddAsync(dbModel);

            await this.DbContext.SaveChangesAsync();
        }
        public ProductBindingModel GetBindingModel()
        {
            var allmanufacturers = DbContext.Manufacturers.ToList();

            var productBindingModel = new ProductBindingModel
            {
                Manufacturers = allmanufacturers
            };

            return(productBindingModel);
        }
Пример #8
0
        public ProductBindingModel GetBindingModel()
        {
            var allteams = this.DbContext.Teams.ToList();

            var productBindingModel = new ProductBindingModel
            {
                Teams = allteams
            };

            return(productBindingModel);
        }
Пример #9
0
        public async Task CreateProductAsync_WithEmptyModel_ShouldReturnTrue()
        {
            var categoriesService = new DataOperatorCategoriesService(this.dbContext, this.mapper);
            var service           = new DataOperatorProductsService(this.dbContext, this.mapper, categoriesService);

            var model = new ProductBindingModel();

            var result = await service.CreateProductAsync(model);

            Assert.IsTrue(result);
        }
Пример #10
0
        public ActionResult Edit(int id)
        {
            var viewModel    = _service.GetElement(id);
            var bindingModel = new ProductBindingModel
            {
                Id   = id,
                Name = viewModel.Name
            };

            return(View(bindingModel));
        }
        public async Task SaveProduct(ProductBindingModel model)
        {
            var manufacturer = await DbContext.Manufacturers.FindAsync(model.ManufacturerId);

            CheckIfManufacturerExist(manufacturer);

            var product = Mapper.Map <Product>(model);
            await DbContext.Products.AddAsync(product);

            await DbContext.SaveChangesAsync();
        }
        public IActionResult Create(ProductBindingModel productBindingModel)
        {
            if (!this.User.IsAuthenticated)
            {
                return(RedirectToAction("/user/login"));
            }

            if (userIsAdmin())
            {
                this.Model["isAdmin"] = "block";
            }
            else
            {
                this.Model["isAdmin"] = "none";
            }

            if (productBindingModel.Name == "" ||
                productBindingModel.Price == "" ||
                productBindingModel.Type == "")
            {
                this.Model["error"] = "Empty input fields !";
                return(View());
            }

            if (!this.IsValidModel(productBindingModel))
            {
                this.Model["error"] = "Something went wrong !";
                return(View());
            }

            User currentUser = this.Context.Users.FirstOrDefault(u => u.Username == this.User.Name);

            Models.Type type = this.Context.Types.FirstOrDefault(t => t.Name == productBindingModel.Type);

            if (type == null)
            {
                this.Model["error"] = "The Type is invalid!";
                return(View());
            }

            Product product = new Product()
            {
                TypeId      = type.Id,
                Description = productBindingModel.Description,
                Name        = productBindingModel.Name,
                Orders      = new List <Order>(),
                Price       = decimal.Parse(productBindingModel.Price)
            };

            this.Context.Products.Add(product);
            this.Context.SaveChanges();

            return(this.RedirectToAction("/"));
        }
Пример #13
0
        public async Task EditProductAsync_WithNoValidId_ShouldThrowArgumentNullException()
        {
            var bindingModel = new ProductBindingModel
            {
                Title = string.Format(TestsConstants.EditTeam, 1),
                Brand = string.Format(TestsConstants.EditBrand, 1)
            };

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(() =>
                                                                      this.service.EditProduct(2, bindingModel));
        }
        public IActionResult Edit(ProductBindingModel model)
        {
            if (this.ModelState.IsValid == false)
            {
                return(this.View());
            }

            this.productsService.EditProduct(model, model.Id);

            return(this.RedirectToAction($"/Products/Details?Id={model.Id}"));
        }
Пример #15
0
        public async Task EditProductAsync_WithNoValidId_ShouldThrowArgumentNullException()
        {
            var bindingModel = new ProductBindingModel
            {
                Name = string.Format(TestsConstants.EditManufacturer, 1),
                Type = string.Format(TestsConstants.EditManufacturer, 1)
            };

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(() =>
                                                                      this._service.EditProduct(2, bindingModel));
        }
Пример #16
0
        public ActionResult Edit(ProductBindingModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                service.EditProduct(model);

                return(RedirectToAction("All", "Products", routeValues: new { area = "" }));
            }

            return(this.View());
        }
        public IActionResult Create(ProductBindingModel model)
        {
            if (this.ModelState.IsValid == false)
            {
                return(this.View());
            }

            this.productsService.CreateProduct(model);

            return(this.RedirectToAction("/"));
        }
Пример #18
0
        public void Delete(ProductBindingModel model)
        {
            var element = _productStorage.GetElement(new ProductBindingModel {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Продукт не найден");
            }
            _productStorage.Delete(model);
        }
Пример #19
0
        public async Task WithModel_ShouldAddNewProductToDatabase()
        {
            var dbContext = this.GetDbContext();
            var model     = new ProductBindingModel();

            await this.CallAddProductAsync(dbContext, model);

            var dbProducts      = dbContext.Products;
            var dbProductsCount = dbProducts.Count();

            Assert.Equal(1, dbProductsCount);
        }
Пример #20
0
        public async Task SaveProduct(ProductBindingModel model)
        {
            var team = await this.DbContext.Teams.FindAsync(model.TeamId);

            this.CheckIfTeamExist(team);

            var product = this.Mapper.Map <Product>(model);

            await this.DbContext.Products.AddAsync(product);

            await this.DbContext.SaveChangesAsync();
        }
Пример #21
0
        public async Task <IActionResult> Post([FromBody] ProductBindingModel model)
        {
            var user = await _userService.GetUser(User);

            var requestProduct = Mapper.Map <Product>(model);

            requestProduct.UserId = user.Id;

            var product = await _productService.AddProduct(requestProduct);

            return(Ok(Mapper.Map <ProductBindingModel>(product)));
        }
Пример #22
0
        public async Task WithModelWithoutPromoPrice_ShouldAddNewProductToDatabaseWithNullPromoPrice()
        {
            var dbContext = this.GetDbContext();
            var model     = new ProductBindingModel();

            await this.CallAddProductAsync(dbContext, model);

            var dbFirstProduct      = dbContext.Products.First();
            var dbProductPromoPrice = dbFirstProduct.PromoPrice;

            Assert.Null(dbProductPromoPrice);
        }
Пример #23
0
        public IActionResult Create(ProductBindingModel productVM)
        {
            if (ModelState.IsValid)
            {
                var productDTO = _mapper.Map <ProductBindingModel, ProductDTO>(productVM);
                _productService.Add(productDTO);

                return(RedirectToAction(nameof(Items)));
            }

            return(View(productVM));
        }
        public IActionResult Create(ProductBindingModel productModel)
        {
            if (this.ModelState.IsValid != true)
            {
                this.Model.Data["Error"] = ProductNotCreated1;
                return(this.View());
            }

            this.productsService.CreateProduct(productModel);

            return(this.RedirectToAction("/Products/All"));
        }
Пример #25
0
        public async Task GetProductAsync_WithId_ShouldReturnTypeofProductBindingModel()
        {
            var productFromService = await this._service.GetProduct(1);

            var bindingModelTest = new ProductBindingModel()
            {
                Name = string.Format(TestsConstants.Test, 1),
                Type = string.Format(TestsConstants.Type, 1)
            };

            Assert.IsInstanceOfType(productFromService, typeof(ProductBindingModel));
        }
Пример #26
0
        private Product CreateModel(ProductBindingModel model, Product product)
        {
            using (var context = new ElectronicsShopDatabase())
            {
                product.Name            = model.Name;
                product.Desc            = model.Desc;
                product.Price           = model.Price;
                product.ProductCategory = model.ProductCategory;

                return(product);
            }
        }
Пример #27
0
        public void CreateOrUpdate(ProductBindingModel model)
        {
            Product element = source.Products.FirstOrDefault(rec => rec.ProductName == model.ProductName && rec.Id != model.Id);

            if (element != null)
            {
                throw new Exception("Уже есть кондитерское изделие с таким названием");
            }
            if (model.Id.HasValue)
            {
                element = source.Products.FirstOrDefault(rec => rec.Id == model.Id);
                if (element == null)
                {
                    throw new Exception("Элемент не найден");
                }
            }
            else
            {
                int maxId = source.Products.Count > 0 ? source.Products.Max(rec => rec.Id) : 0;
                element = new Product {
                    Id = maxId + 1
                };
                source.Products.Add(element);
            }
            element.ProductName = model.ProductName;
            element.Price       = model.Price;
            // удалили те, которых нет в модели
            source.ProductIngredients.RemoveAll(rec =>
                                                rec.ProductId == model.Id && !model.ProductIngredients.ContainsKey(rec.IngredientId));
            // обновили количество у существующих записей
            var updateIngredients = source.ProductIngredients.Where(rec =>
                                                                    rec.ProductId == model.Id && model.ProductIngredients.ContainsKey(rec.IngredientId));

            foreach (var updateIngredient in updateIngredients)
            {
                updateIngredient.Count = model.ProductIngredients[updateIngredient.IngredientId].Item2;
                model.ProductIngredients.Remove(updateIngredient.IngredientId);
            }
            // добавили новые
            int maxPCId = source.ProductIngredients.Count > 0 ? source.ProductIngredients.Max(rec => rec.Id) : 0;

            foreach (var pi in model.ProductIngredients)
            {
                source.ProductIngredients.Add(new ProductIngredient
                {
                    Id           = ++maxPCId,
                    ProductId    = element.Id,
                    IngredientId = pi.Key,
                    Count        = pi.Value.Item2
                });
            }
        }
Пример #28
0
        private async Task <bool> CallEditAsync(Product product, ProductBindingModel model)
        {
            var dbContext = this.GetDbContext();

            dbContext.Products.Add(product);
            dbContext.SaveChanges();

            var service = this.GetService(dbContext);

            var result = await service.EditAsync(model, product.Id);

            return(result);
        }
Пример #29
0
        public async Task GetProductAsync_WithId_ShouldReturnThisProduct()
        {
            var productFromService = await this._service.GetProduct(1);

            var bindingModelTest = new ProductBindingModel()
            {
                Name = string.Format(TestsConstants.Product, 1),
                Type = string.Format(TestsConstants.Type, 1)
            };

            Assert.AreEqual(bindingModelTest.Name, productFromService.Name);
            Assert.AreEqual(bindingModelTest.Type, productFromService.Type);
        }
        public void CreateProduct(ProductBindingModel productModel)
        {
            var product = new Product
            {
                Name    = productModel.Name,
                Picture = productModel.Picture,
                Price   = productModel.Price,
                Barcode = productModel.Barcode
            };

            this.Db.Products.Add(product);
            this.Db.SaveChanges();
        }