Пример #1
0
        public IActionResult UpdateFood(ManageFoodViewModel model)
        {
            if (ModelState.IsValid)
            {
                var original = _context.Matratts.SingleOrDefault(m => m.MatrattId == model.CurrentMatratt.MatrattId);
                _context.Entry(original).CurrentValues.SetValues(model.CurrentMatratt);
                _context.SaveChanges();

                var originalIngredients = _context.MatrattProdukts.Where(m => m.MatrattId == model.CurrentMatratt.MatrattId).Select(y => y.ProduktId).ToList();

                foreach (var item in model.ingredients)
                {
                    if (item.IsChecked && !originalIngredients.Contains(item.ProduktId))
                    {
                        MatrattProdukt newItem = new MatrattProdukt(original.MatrattId, item.ProduktId);
                        _context.Add(newItem);
                    }
                    else if (item.IsChecked == false && originalIngredients.Contains(item.ProduktId))
                    {
                        MatrattProdukt newItem = new MatrattProdukt(original.MatrattId, item.ProduktId);
                        _context.MatrattProdukts.Remove(newItem);
                    }
                }
                TempData["EditFood"] = "Maträtt uppdaterad";
                _context.SaveChanges();
            }
            else
            {
                TempData["EditFood"] = "Maträtt kunde inte uppdateras. Har du inte fyllt i alla uppgifter?";
            }

            return(RedirectToAction("ManageFood"));
        }
Пример #2
0
        public IActionResult EditProfile(ViewModelFood model)
        {
            var id = User.FindFirstValue(ClaimTypes.NameIdentifier);

            Customer cust = new Customer();

            cust.Name       = model.RegisterUser.Name;
            cust.Address    = model.RegisterUser.Address;
            cust.PostalCode = model.RegisterUser.PostalCode;
            cust.Phone      = model.RegisterUser.Phone;
            cust.City       = model.RegisterUser.City;
            cust.Username   = model.RegisterUser.Username;
            cust.Email      = model.RegisterUser.Email;

            var old = _context.Customer.SingleOrDefault(c => c.UserId == id);

            cust.Points = old.Points;

            cust.CustomerId = old.CustomerId;
            cust.UserId     = old.UserId;

            _context.Entry(old).CurrentValues.SetValues(cust);
            _context.SaveChanges();

            model.CurrentCustomer = cust;

            return(View("ViewProfile", model));
        }
Пример #3
0
        public async Task <bool> UpdateDishAsync(Matratt dish)
        {
            _context.Entry(dish).State = EntityState.Modified;
            var result = await _context.SaveChangesAsync();

            return(result == 1);
        }
Пример #4
0
        public async Task <Kund> UpdateUserAsync(Kund kund)
        {
            _context.Entry(kund).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(kund);
        }
        public void Update(Kund updatedCustomer)
        {
            var currentCustomer = _context.Kund.FirstOrDefault(k => k.UserId == updatedCustomer.UserId);

            _context.Entry(currentCustomer).CurrentValues.SetValues(updatedCustomer);
            _context.SaveChanges();
        }
Пример #6
0
        public void SaveOrder(Bestallning order, List <BestallningMatratt> orderList)
        {
            order.BestallningMatratt = null;

            if (order.BestallningId == 0)
            {
                // Add new product
                _context.Bestallning.Add(order);
            }
            else
            {
                // Edit existing product
                var p = Orders.FirstOrDefault(x => x.BestallningId == order.BestallningId);

                if (p != null)
                {
                    _context.Entry(p).CurrentValues.SetValues(order);
                }
            }

            _context.SaveChanges();

            foreach (var c in orderList)
            {
                _context.BestallningMatratt.Add(new BestallningMatratt()
                {
                    Antal         = c.Antal,
                    BestallningId = order.BestallningId,
                    MatrattId     = c.MatrattId
                });
            }

            _context.SaveChanges();
        }
        public void Update(Bestallning order)
        {
            var currentOrder = _context.Bestallning.FirstOrDefault(o => o.BestallningId == order.BestallningId);

            _context.Entry(currentOrder).CurrentValues.SetValues(order);
            _context.SaveChanges();
        }
        public void Update(Produkt produkt)
        {
            var currentProdukt = _context.Produkt.FirstOrDefault(p => p.ProduktId == produkt.ProduktId);

            _context.Entry(currentProdukt).CurrentValues.SetValues(produkt);
            _context.SaveChanges();
        }
        public void Update(Matratt updatedDish)
        {
            var currentDish = _context.Matratt.FirstOrDefault(x => x.MatrattId == updatedDish.MatrattId);

            if (updatedDish != null)
            {
                _context.Entry(currentDish).CurrentValues.SetValues(updatedDish);
                _context.SaveChanges();
            }
        }
Пример #10
0
        /// <summary>
        /// Adds the order to the database. PremiumUser will get the calculated price based on the discount and bonus points.
        /// </summary>
        public async Task <Bestallning> AddOrderAsync(string userId, ShoppingCart cart, UserRole role)
        {
            var kund = await _context.Kund.FirstOrDefaultAsync(u => u.UserId == userId);

            if (kund == null)
            {
                return(null);
            }

            if (role == UserRole.PremiumUser)
            {
                // instantiates the int if it's null, meaning this user hasn't had any points before.
                // Add the points to the Kund, 10 points per item in the cart.
                if (kund.Bonuspoäng == null)
                {
                    kund.Bonuspoäng = 0;
                }
                if (kund.Bonuspoäng >= 100)
                {
                    kund.Bonuspoäng -= 100;
                }
                kund.Bonuspoäng += (cart.Products.Count * 10);
            }

            // Group the products by ID and counting them into new BestallningMatratt objects
            var orders = cart.Products
                         .GroupBy(p => p.MatrattId)
                         .Select(g => new BestallningMatratt
            {
                MatrattId = g.First().MatrattId,
                Antal     = g.Count()
            }).ToList();

            // Puts the order together
            var order = new Bestallning
            {
                BestallningDatum   = DateTime.Now,
                KundId             = kund.KundId,
                Levererad          = false,
                Totalbelopp        = role == UserRole.PremiumUser ? cart.TotalSum(UserRole.PremiumUser) : cart.TotalSum(),
                BestallningMatratt = orders
            };

            // Calculates the points and saves to database
            _context.Add(order);
            _context.Entry(kund).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(await _context.Bestallning
                   .Include(b => b.BestallningMatratt)
                   .Include(b => b.Kund)
                   .FirstOrDefaultAsync(x => x.KundId == kund.KundId));
        }
Пример #11
0
        public IActionResult EditMenu(ViewModelFood edited)
        {
            var old = GetViewModel();

            old.CurrentFood = _context.Food.SingleOrDefault(f => f.FoodId == edited.CurrentFood.FoodId);
            edited.CurrentFood.Ingredients = new List <Ingredient>();

            foreach (var item in edited.AllIngredients)
            {
                if (item.Selected == true)
                {
                    if (old.CurrentFood.Ingredients.FirstOrDefault(i => i.IngredientId == item.IngredientId) == null)
                    {
                        FoodIngredient foodIng = new FoodIngredient();
                        foodIng.FoodId       = edited.CurrentFood.FoodId;
                        foodIng.IngredientId = item.IngredientId;

                        _context.Add(foodIng);
                        _context.SaveChanges();
                    }
                }
                else
                {
                    if (old.CurrentFood.Ingredients.FirstOrDefault(i => i.IngredientId == item.IngredientId) != null)
                    {
                        var foodIng = _context.FoodIngredient.FirstOrDefault(i => i.IngredientId == item.IngredientId &&
                                                                             i.FoodId == edited.CurrentFood.FoodId);

                        _context.Remove(foodIng);
                        _context.SaveChanges();
                    }
                }
            }


            _context.Entry(old.CurrentFood).CurrentValues.SetValues(edited.CurrentFood);
            _context.SaveChanges();

            return(RedirectToAction("ManageMenu"));
        }
Пример #12
0
        public void UpdateKund(Kunder K, Kund JSon)
        {
            var Model = new Kunder();

            Model.Kund = JSon;

            var Original = _Repository.Kund.SingleOrDefault(O => O.KundId == Model.Kund.KundId);

            K.Kund.KundId = Original.KundId;

            if (Original != null)
            {
                _Repository.Entry(Original).CurrentValues.SetValues(K.Kund);
                _Repository.SaveChanges();
            }
        }
Пример #13
0
        public IActionResult SaveChanges(Kund values)
        {
            if (ModelState.IsValid)
            {
                var kundDb = _context.Kund.SingleOrDefault(k => k.KundId == values.KundId);
                _context.Entry(kundDb).CurrentValues.SetValues(values);
                _context.SaveChanges();

                var updatedValues = JsonConvert.SerializeObject(values);
                HttpContext.Session.SetString("Users", updatedValues);

                return(RedirectToAction("Account"));
            }

            ViewBag.Message = "Error";
            return(RedirectToAction("Account"));
        }
Пример #14
0
        public void SaveCustomer(Kund customer)
        {
            if (customer.KundId == 0)
            {
                // Add new product
                _context.Kund.Add(customer);
            }
            else
            {
                // Edit existing product
                var p = Customers.FirstOrDefault(x => x.KundId == customer.KundId);

                if (p != null)
                {
                    _context.Entry(p).CurrentValues.SetValues(customer);
                }
            }
            _context.SaveChanges();
        }
        public void SaveIngredient(Produkt ingredient)
        {
            if (ingredient.ProduktId == 0)
            {
                // Add new product
                _context.Produkt.Add(ingredient);
            }
            else
            {
                // Edit existing product
                var p = _context.Produkt.FirstOrDefault(x => x.ProduktId == ingredient.ProduktId);

                if (p != null)
                {
                    _context.Entry(p).CurrentValues.SetValues(ingredient);
                }
            }
            _context.SaveChanges();
        }
Пример #16
0
        public void SaveProduct(Matratt product)
        {
            if (product.MatrattId == 0)
            {
                // Add new product
                _context.Matratt.Add(product);
            }
            else
            {
                // Edit existing product
                var p = _context.Matratt.FirstOrDefault(x => x.MatrattId == product.MatrattId);

                if (p != null)
                {
                    _context.Entry(p).CurrentValues.SetValues(product);
                }
            }
            _context.SaveChanges();
        }
Пример #17
0
        public IActionResult SaveChanges(Kund values)
        {
            if (ModelState.IsValid)
            {
                // hämta kund fr DB
                var kundDb = _context.Kund.SingleOrDefault(k => k.KundId == values.KundId);
                // sätt samtliga prop till modified och uppdatera till db
                _context.Entry(kundDb).CurrentValues.SetValues(values);
                _context.SaveChanges();

                // uppdatera sessionvariabeln
                var updatedValues = JsonConvert.SerializeObject(values);
                HttpContext.Session.SetString("Users", updatedValues);

                return(RedirectToAction("AccountSide"));
            }
            else
            {
                ViewBag.Message = "Något gick fel";
                return(RedirectToAction("AccountSide"));
            }
        }
 public Task Update(T entity)
 {
     _context.Entry(entity).State = EntityState.Modified;
     return(_context.SaveChangesAsync());
 }