public async Task <IActionResult> Create(int FlavorTreatId, int quantity)
        {
            FlavorTreat ft = await _db.FlavorTreat.Include(x => x.Treat).SingleOrDefaultAsync(flavorTreat => flavorTreat.FlavorTreatId == FlavorTreatId);

            var price       = ft.Treat.Price * quantity;
            var userId      = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var currentUser = await _userManager.FindByIdAsync(userId);

            Order order = new Order {
                Price = price
            };

            order.User = currentUser;
            await _db.Orders.AddAsync(order);

            await _db.SaveChangesAsync();

            if (FlavorTreatId != 0)
            {
                await _db.OrderFlavorTreat.AddAsync(new OrderFlavorTreat()
                {
                    OrderId = order.OrderId, FlavorTreatId = FlavorTreatId, Quantity = quantity
                });
            }
            await _db.SaveChangesAsync();

            return(RedirectToAction("Details", new { id = order.OrderId }));
        }
        public ActionResult UpdateFlavors(List <int> Treats, int FlavorId)
        {
            List <FlavorTreat> thisTreatFlavors = _db.FlavorTreats.Where(ft => ft.FlavorId == FlavorId).ToList();
            List <int>         treatIds         = thisTreatFlavors.Select(ft => ft.TreatId).ToList();

            foreach (int treatId in treatIds)
            {
                if (!Treats.Contains(treatId))
                {
                    var join = thisTreatFlavors.FirstOrDefault(tf => tf.TreatId == treatId);
                    _db.FlavorTreats.Remove(join);
                }
            }
            foreach (int newTreatId in Treats)
            {
                if (!treatIds.Contains(newTreatId))
                {
                    var join = new FlavorTreat()
                    {
                        TreatId = newTreatId, FlavorId = FlavorId
                    };
                    _db.FlavorTreats.Add(join);
                }
            }
            _db.SaveChanges();

            return(RedirectToAction("Details"));
        }
        public async Task <IActionResult> Edit(int id, int FlavorTreatId, int quantity)
        {
            Order order = await _db.Orders.SingleOrDefaultAsync();

            FlavorTreat ft = await _db.FlavorTreat.SingleOrDefaultAsync(flavorTreat => flavorTreat.FlavorTreatId == FlavorTreatId);

            var treat = await _db.Treats.SingleOrDefaultAsync(treat => treat.TreatId == ft.TreatId);

            double priceOfTreat = treat.Price;

            order.Price = (quantity * priceOfTreat);

            var userId      = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var currentUser = await _userManager.FindByIdAsync(userId);

            order.User             = currentUser;
            _db.Entry(order).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            if (FlavorTreatId != 0)
            {
                await _db.OrderFlavorTreat.AddAsync(new OrderFlavorTreat()
                {
                    OrderId = order.OrderId, FlavorTreatId = FlavorTreatId, Quantity = quantity
                });
            }
            await _db.SaveChangesAsync();

            return(RedirectToAction("Details", new { id = order.OrderId }));
        }
Ejemplo n.º 4
0
        public ActionResult DeleteTreat(int id)
        {
            FlavorTreat thisLink = _db.FlavorTreats.FirstOrDefault(flavortreat => flavortreat.FlavorTreatId == id);

            _db.FlavorTreats.Remove(thisLink);
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult DeleteTreat(int FlavorTreatId)
        {
            FlavorTreat joinEntry = _db.FlavorTreat.FirstOrDefault(entry => entry.FlavorTreatId == FlavorTreatId);

            _db.FlavorTreat.Remove(joinEntry);
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Remove(int flavortreatid)
        {
            FlavorTreat flavortreat = _db.FlavorTreats.FirstOrDefault(x => x.Id == flavortreatid);
            int         details     = flavortreat.TreatId;

            _db.FlavorTreats.Remove(flavortreat);
            _db.SaveChanges();
            return(RedirectToAction("Details", new { id = details }));
        }
Ejemplo n.º 7
0
        public ActionResult DeleteFlavor(int joinId, int treatId)
        {
            FlavorTreat joinEntry = _db.FlavorTreat.FirstOrDefault(entry => entry.FlavorTreatId == joinId);

            _db.FlavorTreat.Remove(joinEntry);
            _db.SaveChanges();

            return(RedirectToAction("Details", "Treats", new { id = treatId }));
        }
Ejemplo n.º 8
0
        public ActionResult DeleteConfirm(int id)
        {
            Console.WriteLine(id);
            Treat       treatToBeDeleted       = _db.Treats.FirstOrDefault(t => t.TreatID == id);
            FlavorTreat flavorTreatToBeDeleted = _db.FlavorTreats.FirstOrDefault(t => t.Treat.TreatID == id);

            _db.Treats.Remove(treatToBeDeleted);
            _db.FlavorTreats.Remove(flavorTreatToBeDeleted);
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> Create(FlavorTreat newTreat, int FlavorID)
        {
            Treat foundTreat = _db.Treats.Where(t => t.TreatName == newTreat.Treat.TreatName).FirstOrDefault();

            Console.WriteLine(foundTreat.TreatName);
            if (foundTreat != null)
            {
                newTreat.Treat.TreatID = foundTreat.TreatID;
            }
            else
            {
                var userID      = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var currentUser = await _userManager.FindByIdAsync(userID);

                newTreat.Treat.CreatedBy = currentUser;
            }
            Flavor foundFlavor = _db.Flavors.FirstOrDefault(t => t.FlavorID == FlavorID);

            newTreat.Flavor = foundFlavor;
            _db.FlavorTreats.Add(newTreat);
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> AddFlavor(int TreatID, int flavorID)
        {
            // Treat foundTreat = _db.Treats.FirstOrDefault(t => t.TreatID == TreatID);
            Flavor      foundFlavor    = _db.Flavors.FirstOrDefault(t => t.FlavorID == flavorID);
            Treat       foundTreat     = _db.Treats.Where(t => t.TreatID == TreatID).FirstOrDefault();
            FlavorTreat newFlavorTreat = new FlavorTreat();

            // conditional isnt really needed it should be able to find a treat
            if (foundTreat != null)
            {
                newFlavorTreat.Treat = foundTreat;
            }
            else
            {
                var userID      = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var currentUser = await _userManager.FindByIdAsync(userID);
            }

            newFlavorTreat.Flavor = foundFlavor;
            newFlavorTreat.Treat  = foundTreat;
            _db.FlavorTreats.Add(newFlavorTreat);
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }