public async Task <int> EditProductAsync(AddAndEditProductBindingModel model)
        {
            var product = this.Mapper.Map <Product>(model);

            product.Category = await this.DbContext.Categories.FindAsync(int.Parse(model.CategoryId));

            this.DbContext.Products.Update(product);
            await this.DbContext.SaveChangesAsync();

            return(product.Id);
        }
Exemple #2
0
        public async Task AdminProductsService_AddProductAsync()
        {
            AddAndEditProductBindingModel model = new AddAndEditProductBindingModel()
            {
                Price = 2, Name = "Ring", CategoryId = "1", PictureUrl = "https://media.tiffany.com/is/image/Tiffany/1X/20180403_CB_Necklaces_and_Pendants_Tile2_3x2Promo_US_paloma_picasso_something_to_love.jpg?v=20180322135418"
            };

            dbContext.SaveChanges();
            var service = new AdminProductsService(dbContext, this.mapper);
            await service.AddProductAsync(model);

            Assert.IsNotNull(dbContext.Products.FirstOrDefault(c => c.Id == 1 && c.Name == "Ring"));
            Assert.IsNotNull(dbContext.Products.FirstOrDefault(c => c.Id == 1 && c.Name == "Ring").PictureUrl);
        }
        public async Task <IActionResult> Add(AddAndEditProductBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                this.TempData["__MessageType"] = MessageType.Warning;
                this.TempData["__MessageText"] = this.localizer[ErrorConstants.InvalidFormData];
                return(View(model));
            }

            var id = await this.adminProductsService.AddProductAsync(model);

            this.TempData["__MessageType"] = MessageType.Success;
            this.TempData["__MessageText"] = this.localizer[SuccessConstants.ProductAdded].ToString();
            return(this.RedirectToPage("/Products/Details", new { id }));
        }
        public async Task <AddAndEditProductBindingModel> GetProductAsync()
        {
            var model = new AddAndEditProductBindingModel()
            {
                Categories = await this.DbContext.Categories
                             .Select(b => new SelectListItem()
                {
                    Text  = b.Name,
                    Value = b.Id.ToString()
                })
                             .ToListAsync()
            };

            return(model);
        }