public async Task <bool> EditProduct(ActionProductViewModel editProduct) { try { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", editProduct.Picture.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await editProduct.Picture.CopyToAsync(stream); } var productId = await _context.Product.FirstOrDefaultAsync(x => x.Id == editProduct.Id); productId.ProductName = editProduct.ProductName; productId.BrandId = editProduct.BrandId; productId.ModelYear = editProduct.ModelYear; productId.ListPrice = editProduct.ListPrice; productId.Picture = editProduct.Picture.FileName; _context.Product.Update(productId); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
public async Task <bool> CreateProduct(ActionProductViewModel addProduct) { try { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", addProduct.Picture.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await addProduct.Picture.CopyToAsync(stream); } var product = new Product() { ProductName = addProduct.ProductName, BrandId = addProduct.BrandId, CategoryId = addProduct.CategoryId, ModelYear = addProduct.ModelYear, ListPrice = addProduct.ListPrice, Picture = addProduct.Picture.FileName }; _context.Product.Add(product); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
public async Task <ActionProductViewModel> FindId(int?id) { var productId = await _context.Product.FirstOrDefaultAsync(x => x.Id == id); var mapperUserViewModel = new ActionProductViewModel(productId); return(mapperUserViewModel); }
public async Task <IActionResult> Edit(ActionProductViewModel editProduct) { HttpContext.Session.GetString("fullname"); if (ModelState.IsValid) { if (await _productServices.EditProduct(editProduct)) { ViewBag.BrandId = new SelectList(_brandServices.GetBrands(), "Id", "BrandName", editProduct.BrandId); ViewBag.CategoryId = new SelectList(_categoryServices.GetCategories(), "Id", "CategoryName", editProduct.CategoryId); TempData["succcessMessage"] = _resourcesServices.GetLocalizedHtmlString("msg_EditProductSuccess").ToString(); return(RedirectToAction("Index")); } } ViewData["errorMessage"] = _resourcesServices.GetLocalizedHtmlString("msg_EditProductError"); ViewBag.BrandId = new SelectList(_brandServices.GetBrands(), "Id", "BrandName", editProduct.BrandId); ViewBag.CategoryId = new SelectList(_categoryServices.GetCategories(), "Id", "CategoryName", editProduct.CategoryId); return(View(editProduct)); }