public ProductDTO EditProduct(ProductEditBindingModel model) { var product = this.FindDomainProductById(model.Id); if (this.CheckIfProductIsNull(product)) { return(null); } product = this.FindAndSetProductReviews(product); var category = this.FindCategoryByName(model.Category); var brand = this.FindBrandByName(model.Brand); if (this.CheckIfCategoryOrBrandIsNull(category, brand)) { return(null); } product = this.EditProductMain(model, product, category, brand); this.dbContext.SaveChanges(); return(this.mapper.Map <ProductDTO>(product)); }
public async Task <IActionResult> Edit(string id, ProductEditBindingModel productEditBindingModel) { if (!this.ModelState.IsValid) { var allProductTypes = await this.productService.GetAllProductTypes().ToListAsync(); this.ViewData["types"] = allProductTypes .Select(productType => new ProductEditProductTypeViewModel { Name = productType.Name, }) .ToList(); return(this.View(productEditBindingModel)); } string pictureUrl = await this.cloudinaryService .UploadPictureAync(productEditBindingModel.Picture, productEditBindingModel.Name); ProductServiceModel productServiceModel = AutoMapper.Mapper.Map <ProductServiceModel>(productEditBindingModel); productServiceModel.Picture = pictureUrl; await this.productService.Edit(id, productServiceModel); return(this.Redirect($"/Product/Details/{id}")); }
private Product EditProductMain(ProductEditBindingModel model, Product product, Category category, Brand brand) { product.Name = model.Name; product.MiniDescription = model.MiniDescription; product.Price = decimal.Parse(model.Price); product.Category = category; product.Brand = brand; return(product); }
public async Task <IActionResult> Edit(int id, string slug, ProductEditBindingModel model) { if (!ModelState.IsValid) { return(View()); } await this.productService.EditProductAsync(id, model, this.User); this.TempData.Put("__Message", new MessageModel { Type = MessageType.Success, Message = "Product was edited successfully." }); return(RedirectToAction("Details", new { id = id, slug = slug })); }
public async Task EditProductAsync(int productId, ProductEditBindingModel model) { Validator.EnsureStringIsNotNullOrEmpty(model.Description, ValidationConstants.ProductDescriptionMessage); Validator.EnsureDoubleIsNotNegativeOrZero(model.Price, ValidationConstants.ProductPriceMessage); var product = await this.DbContext.Products.FindAsync(productId); if (product == null) { throw new NotFoundException(); } product.Description = model.Description; product.Price = model.Price; product.ProductImageUrl = model.ProductImageUrl; await this.DbContext.SaveChangesAsync(); }
public async Task <IActionResult> Edit(string id) { ProductEditBindingModel productEditInputModel = (await this.productService.GetById(id)).To <ProductEditBindingModel>(); if (productEditInputModel == null) { //TODO: Error Handling return(this.Redirect("/")); } var allProductTypes = await this.productService.GetAllProductTypes().ToListAsync(); this.ViewData["types"] = allProductTypes .Select(productType => new ProductEditProductTypeViewModel { Name = productType.Name, }) .ToList();; return(this.View(productEditInputModel)); }
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); }
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); }
public async Task EditProductAsync(int productId, ProductEditBindingModel model, ClaimsPrincipal user) { var userFromDb = this.userManager.GetUserAsync(user); if (user.IsInRole("Moderator") || user.IsInRole("Administrator")) { var product = await this.DbContext.Products.FindAsync(productId); if (product == null) { throw new NotFoundException(); } product.Description = model.Description; product.Price = model.Price; product.ProductImageUrl = model.ProductImageUrl; await this.DbContext.SaveChangesAsync(); } else { throw new UnauthorizedAccessException(); } }
public IActionResult Edit(ProductEditBindingModel productEditBindingModel) { ProductDTO product; if (!this.ModelState.IsValid) { product = this.productsService.FindProductById(productEditBindingModel.Id); } else { product = this.productsService.EditProduct(productEditBindingModel); } var productInfoViewModel = this.mapper.Map <ProductInfoViewModel>(product); var categories = this.categoriesService.FindAllCategories(); var brands = this.brandsService.FindAllBrands(); var productCategoryBrandViewModel = this.brandsService.CreateBrandCategoryViewModelByCategoriesAndBrands(categories, brands); var model = this.CreateProductEditViewModel(productInfoViewModel, productCategoryBrandViewModel); return(this.View(model)); }