Beispiel #1
0
        public async Task CreateProduct_WithProperProduct_ShouldAddCorrectly()
        {
            const string productName = "New Product Name";
            const string productSlug = "new-product-name";
            const double price       = 20;
            const string description = "Some Description";


            var productModel = new ProductCreationBindingModel()
            {
                ModelName       = productName,
                Slug            = productSlug,
                Price           = price,
                Description     = description,
                ProductImageUrl = picLink
            };

            //Act
            await this.service.CreateProductAsync(productModel);

            //Assert
            var product = this.dbContext.Products.First();

            Assert.AreEqual(1, this.dbContext.Products.Count());
            Assert.AreEqual(productName, product.ModelName);
            Assert.AreEqual(productSlug, product.Slug);
            Assert.AreEqual(price, product.Price);
            Assert.AreEqual(description, product.Description);
            Assert.AreEqual(picLink, product.ProductImageUrl);
        }
Beispiel #2
0
        public async Task AddProduct_WithNullProduct_ShouldThrowException()
        {
            //Arrange

            ProductCreationBindingModel productModel = null;

            //Act
            Func <Task> addProduct = () => this.service.CreateProductAsync(productModel);

            //Assert
            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addProduct);

            Assert.AreEqual(ValidationConstants.ProductIsDefinedMessage, exception.Message);
        }
        public async Task <ProductCreationBindingModel> PrepareProductForCreationAsync(int categoryId)
        {
            var category = await this.DbContext.Categories.FindAsync(categoryId);

            if (category == null)
            {
                throw new NotFoundException();
            }
            var model = new ProductCreationBindingModel()
            {
                CategoryId = category.Id
            };

            return(model);
        }
Beispiel #4
0
 public async Task <int> AddProductAsync(ProductCreationBindingModel model)
 {
     try
     {
         var product = this.Mapper.Map <Product>(model);
         this.DbContext.Products.Add(product);
         await this.DbContext.SaveChangesAsync();
     }
     catch
     {
         //failure
         return(0);
     }
     //success
     return(1);
 }
Beispiel #5
0
        public async Task <int> CreateProductAsync(ProductCreationBindingModel model)
        {
            Validator.EnsureNotNull(model, ValidationConstants.ProductIsDefinedMessage);
            Validator.EnsureStringIsNotNullOrEmpty(model.ModelName, ValidationConstants.ProductNameMessage);
            Validator.EnsureStringIsNotNullOrEmpty(model.Slug, ValidationConstants.ProductSlugMessage);
            Validator.EnsureStringIsNotNullOrEmpty(model.Description, ValidationConstants.ProductDescriptionMessage);
            Validator.EnsureDoubleIsNotNegativeOrZero(model.Price, ValidationConstants.ProductPriceMessage);

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

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

            await this.DbContext.SaveChangesAsync();

            return(product.Id);
        }
        public async Task <IActionResult> Create(ProductCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var productId = await this.productService.CreateProductAsync(model, this.User);

            this.TempData.Put("__Message", new MessageModel
            {
                Type    = MessageType.Success,
                Message = "Product was added successfully."
            });

            return(RedirectToAction("Details", new { id = productId, slug = model.Slug }));
        }
        public async Task <int> CreateProductAsync(ProductCreationBindingModel model, ClaimsPrincipal user)
        {
            var userFromDb = this.userManager.GetUserAsync(user);

            if (user.IsInRole("Moderator") || user.IsInRole("Administrator"))
            {
                var product = this.Mapper.Map <Product>(model);
                await this.DbContext.Products.AddAsync(product);

                await this.DbContext.SaveChangesAsync();

                return(product.Id);
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
Beispiel #8
0
        public async Task <IActionResult> AddProduct(ProductCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var result = await this.storageService.AddProductAsync(model);

            if (result == 0)
            {
                this.TempData[WebConstants.BadMessage] = string.Format(Messages.AddingFailureMessage, typeof(Product).Name, model.Name);
            }
            else
            {
                this.TempData[WebConstants.GoodMessage] = string.Format(Messages.AddingSuccessMessage, typeof(Product).Name, model.Name);
            }
            return(RedirectToAction("AddProduct", "Storage", new { Area = WebConstants.AdminArea }));
        }
Beispiel #9
0
        public async Task AddProduct_WithProductWithNegativePrice_ShouldThrowException()
        {
            //Arrange

            ProductCreationBindingModel productModel = new ProductCreationBindingModel()
            {
                ModelName       = "Null",
                Slug            = "null",
                Price           = -20,
                Description     = "null",
                ProductImageUrl = picLink
            };

            //Act
            Func <Task> addProduct = () => this.service.CreateProductAsync(productModel);

            //Assert
            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addProduct);

            Assert.AreEqual(ValidationConstants.ProductPriceMessage, exception.Message);
        }
Beispiel #10
0
        public async Task AddProduct_WithProductWithNullDescription_ShouldThrowException()
        {
            //Arrange

            const string productName = "New Product Name";
            const string productSlug = "new-product-name";
            const double price       = 20;
            const string description = "Some Description";


            const double priceEdited       = 50;
            const string descriptionEdited = null;
            const string picLinkEdited     = "https://exampleEdit.com";

            var productModel = new ProductCreationBindingModel()
            {
                ModelName       = productName,
                Slug            = productSlug,
                Price           = price,
                Description     = description,
                ProductImageUrl = picLink
            };

            await this.service.CreateProductAsync(productModel);

            var editedProductModel = new ProductEditBindingModel()
            {
                Price           = priceEdited,
                Description     = descriptionEdited,
                ProductImageUrl = picLinkEdited
            };
            var product = this.dbContext.Products.First();
            //Act
            Func <Task> editProduct = () => this.service.EditProductAsync(product.Id, editedProductModel);

            //Assert
            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(editProduct);

            Assert.AreEqual(ValidationConstants.ProductDescriptionMessage, exception.Message);
        }
Beispiel #11
0
        public async Task EditProduct_WithProperProduct_ShouldEditCorrect()
        {
            const string productName = "New Product Name";
            const string productSlug = "new-product-name";
            const double price       = 20;
            const string description = "Some Description";


            const double priceEdited       = 50;
            const string descriptionEdited = "Some Edited Description";
            const string picLinkEdited     = "https://exampleEdit.com";

            var productModel = new ProductCreationBindingModel()
            {
                ModelName       = productName,
                Slug            = productSlug,
                Price           = price,
                Description     = description,
                ProductImageUrl = picLink
            };

            await this.service.CreateProductAsync(productModel);

            var editedProductModel = new ProductEditBindingModel()
            {
                Price           = priceEdited,
                Description     = descriptionEdited,
                ProductImageUrl = picLinkEdited
            };
            var product = this.dbContext.Products.First();

            await this.service.EditProductAsync(product.Id, editedProductModel);

            Assert.AreEqual(1, this.dbContext.Products.Count());
            Assert.AreEqual(priceEdited, product.Price);
            Assert.AreEqual(descriptionEdited, product.Description);
            Assert.AreEqual(picLinkEdited, product.ProductImageUrl);
        }