public async Task <IActionResult> Create(MadicinaViewModel view) { if (ModelState.IsValid) { var path = string.Empty; if (view.ImageFile != null && view.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot//ima//Product", file); using (var stream = new FileStream(path, FileMode.Create)) { await view.ImageFile.CopyToAsync(stream); } path = $"~/ima/Product/{file}"; } var product = this.ToProduct(view, path); product.User = await this.userHelper.GetUserByEmailAsync(this.User.Identity.Name); await this.productRepository.CreateAsync(product); return(RedirectToAction(nameof(Index))); } return(View(view)); }
private Medicina ToProduct(MadicinaViewModel view, string path) { return(new Medicina { Id = view.Id, ImageUrl = path, IsAvailabe = view.IsAvailabe, LastPurchase = view.LastPurchase, LastSale = view.LastSale, Name = view.Name, Price = view.Price, stock = view.stock, User = view.User }); }
public async Task <IActionResult> Edit(MadicinaViewModel view) { if (ModelState.IsValid) { try { var path = view.ImageUrl; if (view.ImageFile != null && view.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot//ima//Product", file); using (var stream = new FileStream(path, FileMode.Create)) { await view.ImageFile.CopyToAsync(stream); } path = $"~/ima/Product/{file}"; } var product = this.ToProduct(view, path); product.User = await this.userHelper.GetUserByEmailAsync(this.User.Identity.Name); await this.productRepository.UpdateAsync(product); } catch (DbUpdateConcurrencyException) { if (!await this.productRepository.ExistAsync(view.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(view)); }