public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Price,ImageFile,LastPurchase,LastSale,IsAvailable,Stock")] ProductDoModel view) { if (id != view.Id) { return NotFound(); } if (ModelState.IsValid) { try { var path = view.ImageUrl; if (view.ImageFile != null && view.ImageFile.Length > 0) { path = string.Empty; var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\images\\Products", file); using (var stream = new FileStream(path, FileMode.Create)) { await view.ImageFile.CopyToAsync(stream); } path = $"~/images/Products/{file}"; } var product = this.ToProduct(view, path); //TODO: Mudar para o user que depois tiver logado product.User = await _userHelper.GetUserByEmailAsync("*****@*****.**"); await _productrepository.UpdateAsync(product); } catch (DbUpdateConcurrencyException) { if (!await _productrepository.ExistAsync(view.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(view); }
private Product ToProduct(ProductDoModel view, string path) { return new Product { Id = view.Id, ImageUrl = path, IsAvailable = view.IsAvailable, LastPurchase = view.LastPurchase, LastSale = view.LastSale, Name = view.Name, Price = view.Price, Stock = view.Stock, User = view.User }; }