public async Task <IActionResult> Update(BusinessViewModel vm) { var user = await _userManager.GetUserAsync(User); var bFromDb = _db.Businesses.Single(x => x.UserId == user.Id); if (ModelState.IsValid) { bFromDb.Name = vm.Name; bFromDb.Address = vm.Address; bFromDb.About = vm.About; bFromDb.BusinessPhoneContact = vm.BusinessPhoneContact; bFromDb.BusinessEmailContact = vm.BusinessEmailContact; IFormFileCollection files = HttpContext.Request.Form.Files; IFormFile formImage = files.FirstOrDefault(); if (formImage != null) { var image = formImage.FormImageToResizedPng(400, 250); if (bFromDb.Image != null) { bFromDb.Image.Bytes = image; } else { bFromDb.Image = new Image() { Bytes = image, }; } } bFromDb.Saturday = vm.Saturday; bFromDb.Friday = vm.Friday; bFromDb.WeekDays = vm.WeekDays; if (vm.Site != null && !vm.Site.ToLower().Contains("http://")) { vm.Site = $"http://{vm.Site}"; } bFromDb.Site = vm.Site; await _db.SaveChangesAsync(); TempData["Success"] = "פרטי העסק נשמרו בהצלחה!"; } else { TempData["Error"] = "התרחשה שגיאת, הפרטים לא נשמרו."; } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(SaleViewModel vm) { var user = await _signInManager.UserManager.GetUserAsync(User); if (!ModelState.IsValid) { return(View()); } IFormFileCollection files = HttpContext.Request.Form.Files; IFormFile formImage = files.FirstOrDefault(); byte[] image = new byte[0]; var sale = new Sale() { SalePrice = vm.SalePrice, BeforePrice = vm.BeforePrice, Name = vm.Name, User = user, Description = vm.Description }; if (formImage != null) { image = formImage.FormImageToResizedPng(400, 400); sale.Image = new Image() { Bytes = image, }; } db.Sales.Add(sale); await db.SaveChangesAsync(); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(int id, SaleViewModel vm) { if (ModelState.IsValid) { var saleFromDb = db.Sales.Single(x => x.Id == id); IFormFileCollection files = HttpContext.Request.Form.Files; IFormFile formImage = files.FirstOrDefault(); if (formImage != null) { var image = formImage.FormImageToResizedPng(400, 400); if (saleFromDb.Image != null) { saleFromDb.Image.Bytes = image; } else { saleFromDb.Image = new Image() { Bytes = image, }; } } saleFromDb.Name = vm.Name; saleFromDb.BeforePrice = vm.BeforePrice; saleFromDb.SalePrice = vm.SalePrice; saleFromDb.LastEdited = DateTime.Now; saleFromDb.Description = vm.Description; await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(vm)); }