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)));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([Bind("Id,Name,Price,Size")] Pizza pizza)
        {
            if (ModelState.IsValid)
            {
                pizza.Id = Guid.NewGuid();
                _context.Add(pizza);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(pizza));
        }
Esempio n. 3
0
        private void GenMockData()
        {
            var uId = new Guid("ec04ee08-f434-41d0-a208-15bd2dcb3389");
            var pId = Guid.NewGuid();
            var ppc = new PizzaPriceCategory {
                Description = "LOL", Id = 1
            };
            var user = new ApplicationUser {
                Id = uId.ToString(), Email = "*****@*****.**", UserName = "******"
            };

            _context.Users.Add(user);
            _context.Users.Add(new ApplicationUser {
                Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", UserName = "******"
            });
            _context.PizzaPriceCategories.Add(ppc);
            _context.SaveChanges();
            _context.PizzaPriceInfo.Add(new PizzaPriceInfo {
                Id = 1, Price = 6.05m, PriceCategory = ppc, Size = 32
            });
            _context.Pizzas.Add(new Pizza {
                Id = pId, NameEn = "Havaian", PriceCategory = _context.PizzaPriceCategories.Single(c => c.Id == 1)
            });
            _context.SaveChangesAsync();
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("Amount")] PrepaidCardViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            _context.PrepaidCodes.Add(new PrepaidCode
            {
                Id           = Guid.NewGuid(),
                Amount       = model.Amount,
                CreationDate = DateTime.Now
            });
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 5
0
        public async Task <IActionResult> Assign()
        {
            var newOrder = await _context.Orders.OrderBy(o => o.PaymentDate)
                           .FirstOrDefaultAsync(o => o.Status == OrderStatus.Ready && o.DelivereeId == null);

            if (newOrder == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            newOrder.DelivereeId  = User.GetId();
            newOrder.DeliveryDate = DateTime.Now;
            newOrder.Status       = OrderStatus.Delivering;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 6
0
        public async Task <IActionResult> Assign()
        {
            var order = await _context.Orders.OrderBy(o => o.PaymentDate)
                        .FirstOrDefaultAsync(o => o.Status == OrderStatus.Queued && o.ChefId == null);

            if (order == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            order.ChefId      = User.GetId();
            order.CookingDate = DateTime.Now;
            order.Status      = OrderStatus.Cooking;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 7
0
        public async Task <IActionResult> Redeem([Bind("Id")] RedeemViewModel model)
        {
            var execRes = new ExecutionResult();

            if (!ModelState.IsValid)
            {
                return(View());
            }

            var code = _context.PrepaidCodes.SingleOrDefault(c => c.Id == Guid.Parse(model.Id));

            if (code == null)
            {
                execRes.AddError(_localizer["Provided code doesn't exist."]).PushTo(TempData);
                return(View());
            }
            if (code.RedemptionDate != null)
            {
                execRes.AddError(_localizer["Provided code has already been used."]).PushTo(TempData);
                return(View());
            }

            var user = _context.Users.Single(u => u.Id == User.GetId());

            code.Redeemer       = user ?? throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            code.RedemptionDate = DateTime.Now;

            user.Coins += code.Amount;

            _cache.Remove(user.UserName);

            if (await _context.SaveChangesAsync() > 0)
            {
                execRes.AddInfo(
                    _localizer["Code has been succesfully redeemed. Added {0} to the acccount balance. New balance: {1}!", code.Amount, user.Coins]);
            }
            else
            {
                execRes.AddError(_localizer["Failed to use the code. Try again later."]);
            }

            execRes.PushTo(TempData);
            return(View());
        }
Esempio n. 8
0
        public async Task <ViewResult> Index()
        {
            while (_context.News.Count() < 4)
            {
                _context.News.Add(new News());
                await _context.SaveChangesAsync();
            }

            var imgList = Directory.EnumerateFiles(Path.Combine(_env.WebRootPath, "uploads/img/gallery/"), "*.jpg")
                          .Concat(Directory.EnumerateFiles(Path.Combine(_env.WebRootPath, "uploads/img/gallery/"), "*.png"))
                          .Select(Path.GetFileName);

            return(View(new NewsViewModel
            {
                Slides = await _context.News.OrderBy(o => o.Id).Take(4).ToArrayAsync(),
                Pizzas = await _context.Pizzas.ToArrayAsync(),
                Images = imgList
            }));
        }
Esempio n. 9
0
        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"));
        }
Esempio n. 10
0
        public async Task <IActionResult> Create(PizzaFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var dbPizza = new Pizza
            {
                Id              = Guid.NewGuid(),
                NameLt          = model.NameLt,
                NameEn          = model.NameEn,
                PriceCategoryId = model.PriceCategoryId,
                DescriptionLt   = model.DescriptionLt,
                DescriptionEn   = model.DescriptionEn,
                ImagePath       = "~/uploads/img/gallery/" + model.ImagePath
            };

            _context.Add(dbPizza);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 11
0
        public async Task Currency_Delete_Code_Ok() // delete any code
        {
            var cId   = new Guid("ec047e08-4444-41d0-a208-15bd2dcb3389");
            var code1 = new PrepaidCode {
                Id = cId, Amount = 1, CreationDate = DateTime.Now
            };

            _context.PrepaidCodes.Add(code1);

            await _context.SaveChangesAsync();

            var beforeCount = _context.PrepaidCodes.Count();


            var result = await _controller.Delete(cId);

            Assert.Equal(beforeCount - 1, _context.PrepaidCodes.Count());

            Assert.IsType <RedirectToActionResult>(result);
            Assert.Equal("Index", ((RedirectToActionResult)result).ActionName);
        }
Esempio n. 12
0
        public async Task <IActionResult> Create(string id, OrderCreateViewModel model)
        {
            var execRes = new ExecutionResult();

            var pizza = _context.Pizzas.SingleOrDefault(i => i.NameEn == id);

            if (pizza == null)
            {
                execRes.AddError(_localizer["Requested pizza was not found. Please try again."]).PushTo(TempData);
                return(RedirectToAction(nameof(Index)));
            }

            if (!ModelState.IsValid)
            {
                model.Prices   = _context.PizzaPriceInfo.Where(info => info.PriceCategoryId == pizza.PriceCategoryId);
                model.MinPrice = model.Prices.Min(p => p.Price);
                return(View(model));
            }

            if (pizza.IsDepricated)
            {
                execRes.AddError(_localizer["Pizza no longer exists. Please try another pizza."]).PushTo(TempData);
                return(RedirectToAction(nameof(Index)));
            }

            var ppi = _context.PizzaPriceInfo.SingleOrDefault(g => g.Id == model.SizeId);

            if (ppi == null || pizza.PriceCategoryId != ppi.PriceCategoryId)
            {
                execRes.AddError(_localizer["Unexpected size was selected. Please try again."]).PushTo(TempData);
                return(RedirectToAction(nameof(Create), new { Id = id }));
            }

            var userId = User.GetId();

            if (string.IsNullOrEmpty(userId))
            {
                execRes.AddError(_localizer["You are logged out. Please log in to add the order."]).PushTo(TempData);
                return(RedirectToAction(nameof(Create), new { Id = id }));
            }

            // Check if there is an order in the basket already
            var order = await _context.Orders.SingleOrDefaultAsync(o => !o.IsPaid && o.PizzaId == pizza.Id && o.ClientId == userId && o.Size == ppi.Size);

            if (order != null)
            {
                order.Price         = ppi.Price;
                order.Quantity     += model.Quantity;
                order.PlacementDate = DateTime.Now;
                // Adds a new line to the comment with the new comment.
                order.CookingComment = string.IsNullOrEmpty(order.CookingComment)
                    ? model.Comment : string.IsNullOrEmpty(model.Comment)
                        ? order.CookingComment : $"{order.CookingComment}\n-----\n{model.Comment}";
            }
            else
            {
                order = new Order
                {
                    Id             = Guid.NewGuid(),
                    Pizza          = pizza,
                    ClientId       = userId,
                    CookingComment = model.Comment,
                    Quantity       = model.Quantity,
                    Price          = ppi.Price,
                    Size           = ppi.Size,
                    PlacementDate  = DateTime.Now
                };
                _context.Add(order);
            }

            if (await _context.SaveChangesAsync() > 0)
            {
                execRes.AddInfo(_localizer["Pizza was succesfully added to the cart."]);
            }
            else
            {
                execRes.AddError(_localizer["Pizza could not be added to the cart. Please try again."]);
            }

            execRes.PushTo(TempData);
            return(RedirectToAction(nameof(Index)));
        }