public async Task <IActionResult> Create(Page page) { if (ModelState.IsValid) { page.Slug = page.Title.ToLower().Replace(" ", "-"); page.Sorting = 100; var slug = await context.Pages.FirstOrDefaultAsync(x => x.Slug == page.Slug); if (slug != null) { ModelState.AddModelError("", "Karta o podanym tytule już istnieje"); //Page already exists return(View(page)); } context.Add(page); await context.SaveChangesAsync(); TempData["Success"] = "Karta została utworzona"; //Page has been created return(RedirectToAction("Index")); } return(View(page)); }
public async Task <IActionResult> Create(Genre genre) { if (ModelState.IsValid) { genre.Slug = genre.Name.ToLower().Replace(" ", "-"); genre.Sorting = 100; var slug = await context.Genres.FirstOrDefaultAsync(x => x.Slug == genre.Slug); if (slug != null) { ModelState.AddModelError("", "Gatunek o podanej nazwie już istnieje"); //Genre already exists return(View(genre)); } context.Add(genre); await context.SaveChangesAsync(); TempData["Success"] = "Gatunek został dodany"; //Genre has been added return(RedirectToAction("Index")); } return(View(genre)); }
public async Task <IActionResult> EditAddress(DeliveryAddress address) { AppUser appUser = await userManager.FindByNameAsync(User.Identity.Name); if (ModelState.IsValid) { DeliveryAddress newAddress = await context.DeliveryAddresses.FirstOrDefaultAsync(x => x.UserId == appUser.Id); if (newAddress == null) { address.UserId = appUser.Id; context.Add(address); TempData["Success"] = "Adres został dodany"; //Address added } else { newAddress.City = address.City; newAddress.Street = address.Street; newAddress.ZIPCode = address.ZIPCode; context.Update(newAddress); TempData["Success"] = "Adres został zmieniony"; //Address changed } await context.SaveChangesAsync(); } return(RedirectToAction("Details")); }
public async Task <IActionResult> SaveOrder() { List <CartItem> cart = HttpContext.Session.GetJson <List <CartItem> >("Cart"); AppUser appUser = await userManager.FindByNameAsync(User.Identity.Name); if (context.DeliveryAddresses.FirstOrDefault(x => x.UserId == appUser.Id) == null) { TempData["Error"] = "Wprowadź adres dostawy"; return(RedirectToAction("Details", "Account")); } Order recentOrder = new Order(cart, cart.Sum(x => x.Total), DateTime.Now, appUser.Id); context.Add(recentOrder); await context.SaveChangesAsync(); CartViewModel cartVM = new CartViewModel { CartItems = cart, GrandTotal = cart.Sum(x => x.Total), Function = "FinalizeOrder()" }; return(View("Index", cartVM)); }
public async Task <IActionResult> Create(Book book) { ViewBag.GenreId = new SelectList(context.Genres.OrderBy(x => x.Sorting), "Id", "Name"); if (ModelState.IsValid) { book.Slug = book.Title.ToLower().Replace(" ", "-"); var slug = await context.Books.FirstOrDefaultAsync(x => x.Slug == book.Slug); if (slug != null) { ModelState.AddModelError("", "Książka o podanej nazwie już istnieje"); //Book already exists return(View(book)); } string imageName = "noimage.png"; //Create a new, unique name for photo and save it if (book.ImageUpload != null) { string uploadDir = Path.Combine(webHostEnvironment.WebRootPath, "media/books"); imageName = Guid.NewGuid().ToString() + "_" + book.ImageUpload.FileName; string filePath = Path.Combine(uploadDir, imageName); FileStream fs = new FileStream(filePath, FileMode.Create); await book.ImageUpload.CopyToAsync(fs); fs.Close(); } book.Image = imageName; context.Add(book); await context.SaveChangesAsync(); TempData["Success"] = "Książka została dodana"; //Book has been added return(RedirectToAction("Index")); } return(View(book)); }