public async Task <IActionResult> Edit(ProductEditInputModel productEditInputModel) { if (!this.ModelState.IsValid) { return(this.View()); } ProductServiceModel productServiceModel = productEditInputModel.To <ProductServiceModel>(); if (productEditInputModel.ImageFormFile != null) { string pictureUrl = await this.cloudinaryService .UploadPictureAsync(productEditInputModel.ImageFormFile, productEditInputModel.Name); ProductServiceModel productFromDb = await this.productService.EditAsync(productServiceModel); await this.imageService.CreateWithProductAsync(pictureUrl, productFromDb.Id); return(this.RedirectToAction("All", "Products")); } await this.productService.EditAsync(productServiceModel); return(this.RedirectToAction("All", "Products")); }
public IActionResult Edit(ProductEditInputModel productEditInputModel) { if (!ModelState.IsValid) { var productEditModel = this.productsService.GetById(productEditInputModel.Id).To <ProductEditInputModel>(); var childCategories = this.childCategoriesService.GetAllChildCategories(); this.ViewData["categories"] = childCategories.Select(childCategory => new ProductCreateChildCategoryViewModel { Id = childCategory.Id, ParentCategoryName = childCategory.ParentCategory.Name, Name = childCategory.Name, }) .ToList(); return(this.View(productEditModel)); } string pictureUrl = this.cloudinaryService.UploadPicture( productEditInputModel.Image, productEditInputModel.Name); var productServiceModel = AutoMapper.Mapper.Map <ProductServiceModel>(productEditInputModel); productServiceModel.Image = pictureUrl; this.productsService.Edit(productServiceModel); return(this.Redirect("All")); }
public async Task <IActionResult> Edit(string id, ProductEditInputModel productEditInputModel) { if (!this.ModelState.IsValid) { var allProductTypes = await this.productService.GetAllProductTypes().ToListAsync(); this.ViewData["types"] = allProductTypes.Select(productType => new ProductCreateProductTypeViewModel { Name = productType.Name }) .ToList();; return(this.View(productEditInputModel)); } string pictureUrl = await this.cloudinaryService.UploadPictureAsync( productEditInputModel.Picture, productEditInputModel.Name); ProductServiceModel productServiceModel = AutoMapper.Mapper.Map <ProductServiceModel>(productEditInputModel); productServiceModel.Picture = pictureUrl; await this.productService.Edit(id, productServiceModel); return(this.Redirect("/")); }
public IActionResult Edit(string id) { try { var productViewModel = this._productRepository.Get(id); var productEditModel = new ProductEditInputModel() { Id = productViewModel.Id, Description = productViewModel.Description, Name = productViewModel.Name, Price = productViewModel.Price, CategoryId = productViewModel.CategoryId, CoverUrl = productViewModel.CoverUrl, Categories = this._categoryRepository.GetAll() .ToSelectList(category => category.Name, category => category.Id) }; return(this.View(productEditModel)); } catch (ArgumentException) { return(this.NotFound()); } }
public async Task UpdateAsync(ProductEditInputModel model) { var currentProduct = productRepository.All().FirstOrDefault(x => x.Id == model.Id); currentProduct.Name = model.Name; currentProduct.Price = model.Price; currentProduct.CategoryId = model.CategoryId; currentProduct.Description = model.Description; await productRepository.SaveChangesAsync(); }
public async Task <IActionResult> Edit(ProductEditInputModel input, int id) { if (!this.ModelState.IsValid) { input.Categories = categoryService.GetAllCategories <CategoryDropDownViewModel>(); return(this.View(input)); } input.Id = id; await this.productService.UpdateAsync(input); return(this.RedirectToAction(nameof(this.Details), new { input.Id })); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } HttpClient client = _api.Initial(); var product = new Product(); HttpResponseMessage res = await client.GetAsync("api/Products/" + id + "/GetAdminProductDetails"); if (res.IsSuccessStatusCode) { var result = res.Content.ReadAsStringAsync().Result; product = JsonConvert.DeserializeObject <Product>(result); } res = await client.GetAsync("api/Categories/GetActiveCategories"); if (res.IsSuccessStatusCode) { var result = res.Content.ReadAsStringAsync().Result; IEnumerable <Category> activeCategories = JsonConvert.DeserializeObject <IEnumerable <Category> >(result); object[] selectedValues = new object[product.ProductCategories.Count]; ushort i = 0; foreach (var item in product.ProductCategories) { selectedValues[i++] = item.Category.ID; } SelectedCategories = new MultiSelectList(activeCategories, "ID", "Name", selectedValues); } if (product == null) { return(NotFound()); } InputModel = new ProductEditInputModel(); InputModel.ID = product.ID; InputModel.IsActive = product.IsActive; InputModel.Price = product.Price; InputModel.CreatedAt = product.CreatedAt; InputModel.Description = product.Description; InputModel.ProductName = product.ProductName; InputModel.ProductImages = product.ProductImages; InputModel.Variants = product.Variants; return(Page()); }
public async Task <IActionResult> Edit(ProductEditInputModel productEditInputModel) { if (!this.ModelState.IsValid) { return(this.View(productEditInputModel)); } if (!await this.productService.EditProductAsync(productEditInputModel)) { this.TempData["Error"] = ValidationMessages.ProductNameNotUniqueErrorMessage; return(this.RedirectToAction("Edit", "Products", new { productId = productEditInputModel.Id })); } return(this.RedirectToAction("All")); }
public async Task <IActionResult> Edit(ProductEditInputModel inputModel) { try { await this._productRepository.Edit(inputModel); this.TempData.AddSerialized <Alert>("Alerts", new Alert(AlertType.Success, "Successfully edited product.")); return(this.RedirectToAction(nameof(this.All))); } catch (ArgumentException e) { this.ModelState.AddModelError(string.Empty, e.Message); return(this.View(inputModel)); } }
public async Task <IActionResult> Edit(string id) { ProductEditInputModel productEditInputModel = (await this.productService.GetById(id) ).To <ProductEditInputModel>(); if (productEditInputModel == null) { // TODO: Error Handling return(this.Redirect("/")); } var allProductTypes = await this.productService.GetAllProductTypes().ToListAsync(); this.ViewData["types"] = allProductTypes.Select(productType => new ProductCreateProductTypeViewModel { Name = productType.Name }).ToList(); return(this.View(productEditInputModel)); }
public async Task EditProductAsync_ReturnsTrueOrFalse_DependingOnTheInputData() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var productRepository = new EfDeletableEntityRepository <Product>(context); var productsTestSeeder = new ProductTestSeeder(); var productsService = this.GetProductsService(productRepository, context); await productsTestSeeder.SeedProducts(context); var validProductEditInputModel = new ProductEditInputModel() { Id = "1", Price = 1, }; var inValidProductIdEditInputModel = new ProductEditInputModel() { Id = "invalid", Price = 1, }; var inValidProductNameEditInputModel = new ProductEditInputModel() { Id = "1", Name = "testProduct2", Price = 1, }; var resultShouldBeTrue = await productsService.EditProductAsync(validProductEditInputModel); var resultShouldBeFalse = await productsService.EditProductAsync(inValidProductNameEditInputModel); Assert.True(resultShouldBeTrue); Assert.False(resultShouldBeFalse); await Assert.ThrowsAsync <ArgumentNullException>(async() => { await productsService.EditProductAsync(inValidProductIdEditInputModel); }); }
public async Task <bool> EditProductAsync(ProductEditInputModel productEditInputModel) { var product = await this.productRepository.All().SingleOrDefaultAsync(x => x.Id == productEditInputModel.Id); if (product == null || productEditInputModel.Price <= 0) { throw new ArgumentNullException("Product was null or price was equal or less than zero !"); } if (product.Name != productEditInputModel.Name && await this.ProductNameIsNotUnique(productEditInputModel.Name)) { return(false); } if (productEditInputModel.PictureFile != null) { var pictureUrl = await this.cloudinaryService.UploadPictureAsync( productEditInputModel.PictureFile, productEditInputModel.Name, GlobalConstants.CloudinaryProductPictureFolder); product.Picture = pictureUrl; } if (productEditInputModel.Price < product.Price) { product.OldPrice = product.Price; } productEditInputModel.To(product); await this.productRepository.SaveChangesAsync(); return(true); }