// edit post product public async Task <bool> EditProductAsync(ProductViewsModels editProduct) { try { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", editProduct.PictureFile.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await editProduct.PictureFile.CopyToAsync(stream); } var product = await _context.Products.FindAsync(editProduct.Id); product.BrandId = editProduct.BrandId; product.CategoryId = editProduct.CategoryId; product.ProductName = editProduct.ProductName; product.Picture = editProduct.PictureFile.FileName; product.ModelYear = editProduct.ModelYear; product.ListPrice = editProduct.ListPrice; _context.Products.Update(product); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
//create product public async Task <bool> AddProductAsync(ProductViewsModels addProduct) { try { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", addProduct.PictureFile.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await addProduct.PictureFile.CopyToAsync(stream); } var product = new Product() { ProductName = addProduct.ProductName, BrandId = addProduct.BrandId, CategoryId = addProduct.CategoryId, ModelYear = addProduct.ModelYear, ListPrice = addProduct.ListPrice, Picture = addProduct.PictureFile.FileName }; _context.Products.Add(product); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
public async Task <IActionResult> Create(ProductViewsModels product) { if (ModelState.IsValid) { var addProduct = await _productService.AddProductAsync(product); if (addProduct) { TempData["Successfuly"] = _localizer.GetLocalizedString("msg_AddSuccessfuly").ToString(); return(RedirectToAction("Index")); } TempData["Failure"] = _localizer.GetLocalizedString("msg_AddFailure").ToString(); ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", product.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", product.BrandId); return(View(product)); } ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", product.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", product.BrandId); return(View(product)); }
public async Task <IActionResult> Edit(ProductViewsModels editProduct) { if (ModelState.IsValid) { var product = await _productService.EditProductAsync(editProduct); if (product) { TempData["Successfuly"] = _localizer.GetLocalizedString("msg_EditSuccessfuly").ToString(); return(RedirectToAction("Index")); } ViewData["Failure"] = _localizer.GetLocalizedString("err_EditFailure"); ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", editProduct.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", editProduct.BrandId); Log.Error("Edit Product error "); return(View(editProduct)); } ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", editProduct.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", editProduct.BrandId); Log.Error("Edit Product error "); return(View(editProduct)); }
public async Task <IActionResult> Edit(int id, ProductViewsModels editProduct) { if (ModelState.IsValid) { if (id == editProduct.Id) { await _productService.EditProduct(id, editProduct); TempData["EditSuccessfuly"] = _localizer.GetLocalizedString("msg_EditSuccessfuly").ToString(); return(RedirectToAction("Index")); } ViewData["EditFailure"] = _productLocalizer.GetLocalizedString("err_EditFailure"); ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", editProduct.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", editProduct.BrandId); return(BadRequest()); } ViewData["EditFailure"] = _productLocalizer.GetLocalizedString("err_EditFailure"); ViewBag.CategoryId = new SelectList(_categoryService.GetCategory(), "Id", "CategoryName", editProduct.CategoryId); ViewBag.BrandId = new SelectList(_brandService.Getbrand(), "Id", "BrandName", editProduct.BrandId); return(View()); }