public async Task <IActionResult> Edit(int id, int size, decimal price) { var execRes = new ExecutionResult(); if (id == 0 || size == 0 || price == 0) { execRes.AddError("Invalid size or price was given. Please try again.").PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppi = await _context.PizzaPriceInfo.SingleOrDefaultAsync(p => p.Id == id); ppi.Size = size; ppi.Price = price; _context.PizzaPriceInfo.Update(ppi); if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Price information was successfully updated."); } else { execRes.AddError("Database error occured. Price information could not be added."); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Create(string description) { var execRes = new ExecutionResult(); if (string.IsNullOrWhiteSpace(description)) { execRes.AddError("Invalid description was given. Please try again.").PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppc = new PizzaPriceCategory { Description = description }; await _context.PizzaPriceCategories.AddAsync(ppc); if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Price category was successfully added."); } else { execRes.AddError("Database error occured. Price category could not be added."); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Add(int id, int size, decimal price) { var execRes = new ExecutionResult(); if (id == 0 || size == 0 || price == 0) { execRes.AddError("Invalid size or price was given. Please try again.").PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppi = new PizzaPriceInfo { PriceCategoryId = id, Price = price, Size = size }; await _context.PizzaPriceInfo.AddAsync(ppi); if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Price information was successfully added."); } else { execRes.AddError("Database error occured. Price information could not be added."); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Delete(int?id) { var execRes = new ExecutionResult(); if (id == null) { execRes.AddError("Invalid id was provided. Please try again.").PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppc = await _context.PizzaPriceCategories.SingleAsync(p => p.Id == id); _context.PizzaPriceCategories.Remove(ppc); try { if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Price category was sucessfully removed."); } else { execRes.AddError("Invalid request."); } } catch (DbUpdateException) { execRes.AddError("Price category is still in use by some pizzas. Make sure to unlink all pizzas before proceeding."); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Remove(int?id) { var execRes = new ExecutionResult(); if (id == null) { execRes.AddError("Invalid id was provided. Please try again.").PushTo(TempData); return(RedirectToAction(nameof(Index))); } var ppi = await _context.PizzaPriceInfo.SingleAsync(p => p.Id == id); _context.PizzaPriceInfo.Remove(ppi); if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Price information was sucessfully removed."); } else { execRes.AddError("Invalid request."); } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Remove(Guid id) { var execRes = new ExecutionResult(); var order = _context.Orders .Where(o => o.Client.Id == User.GetId()) .Single(o => o.Id == id); if (order != null) { _context.Orders.Remove(order); if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess(_localizer["Pizza was successfuly removed from the cart."]); } else { execRes.AddError(_localizer["Pizza was not able to be removed from the cart. Please try again."]); } } else { execRes.AddError(_localizer["Requested pizza was not found. Please try again."]); } execRes.PushTo(TempData); return(RedirectToAction("Index")); }
public async Task <IActionResult> RemoveLogin(RemoveLoginViewModel model) { var execRes = new ExecutionResult(); var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var result = await _userManager.RemoveLoginAsync(user, model.LoginProvider, model.ProviderKey); if (!result.Succeeded) { throw new ApplicationException($"Unexpected error occurred removing external login for user with ID '{user.Id}'."); } await _signInManager.SignInAsync(user, isPersistent : false); execRes.AddSuccess("The external login was removed.").PushTo(TempData); return(RedirectToAction(nameof(ExternalLogins))); }
public async Task <IActionResult> Edit(string id, int qty) { var execRes = new ExecutionResult(); var order = _context.Orders.Single(o => o.Id.Equals(Guid.Parse(id))); if (order == null || order.ClientId != User.GetId()) { execRes.AddError(_localizer["Pizza you were trying to edit was not found. Please try again."]).PushTo(TempData); return(RedirectToAction("Index")); } if (qty < 1) { execRes.AddError(_localizer["Quantity must be a positive whole number"]).PushTo(TempData); return(RedirectToAction(nameof(Index))); } var oq = order.Quantity; if (qty == oq) { return(RedirectToAction(nameof(Index))); } order.Quantity = qty; if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess(_localizer["Pizza amount was successfully changed from {0} to {1}.", oq, qty]); } else { execRes.AddError(_localizer["Order could not be processed. Please try again."]); } execRes.PushTo(TempData); return(RedirectToAction("Index")); }
public async Task <IActionResult> DeleteConfirmed(string id) { var pizza = await _context.Pizzas.SingleOrDefaultAsync(m => m.NameLt == id); _context.Pizzas.Remove(pizza); var execRes = new ExecutionResult(); try { if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess("Pizza deleted successfully."); } else { execRes.AddError("Database error. Pizza was not deleted."); } } catch (DbUpdateException) { _context.Entry(pizza).State = EntityState.Unchanged; pizza.IsDepricated = true; if (await _context.SaveChangesAsync() > 0) { execRes.AddInfo("Pizza could not be fully deleted. Changed to depricated."); } else { execRes.AddError("Database error. Pizza was not deleted."); } } execRes.PushTo(TempData); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Continue(ConfirmCheckoutViewModel model) { var execRes = new ExecutionResult(); var clientId = User.GetId(); var user = _context.Users.Single(u => u.Id == clientId); if (!ModelState.IsValid) { var requestCulture = HttpContext.Features.Get <IRequestCultureFeature>(); var cultCode = requestCulture.RequestCulture.UICulture.Name; model.CheckoutList = GetCheckoutOrders(clientId, cultCode); model.Price = model.CheckoutList.Sum(o => o.Price * o.Quantity); return(View(model)); } var orders = await _context.Orders.Include(o => o.Pizza) .Where(o => o.Client.Id == clientId) .Where(o => o.Status == OrderStatus.Unpaid) .Where(o => o.PlacementDate > DateTime.Now.AddDays(-7)) // Each item is only valid for 7 days .ToArrayAsync(); var price = orders.Sum(o => o.Price * o.Quantity); if (price > user.Coins) { // insufficient PizzaCoins execRes.AddError(_localizer["Insufficient amount of coins in the balance."]).PushTo(TempData); return(RedirectToAction("Index")); } // looks good, go ahead foreach (var checkoutEntry in orders) { var order = _context.Orders.Single(o => o.Id == checkoutEntry.Id); order.Status = OrderStatus.Queued; order.IsPaid = true; order.DeliveryAddress = model.ConfirmAddress; order.DeliveryComment = model.Comment; order.PaymentDate = DateTime.Now; order.PhoneNumber = model.PhoneNumber; } user.Coins -= price; if (await _context.SaveChangesAsync() > 0) { execRes.AddSuccess(_localizer["Pizza was ordered successfully."]); } else { execRes.AddError(_localizer["Order could not be processed. Please try again."]).PushTo(TempData); return(RedirectToAction("Index")); } if (user.EmailSendUpdates) { await SendEmail(user, orders.ToArray()); execRes.AddInfo(_localizer["Email was sent to {0}", user.Email]); } _cache.Remove(user.UserName); execRes.PushTo(TempData); return(RedirectToAction("Index", "Order")); }