public void MyTestMethod()
        {
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                //ARRANGE
                var IngredientToUseInTest = new IngredientTO
                {
                    Id         = 1,
                    IsAllergen = true,
                    Name       = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL")
                };

                IRepositoryDO_NOT_USE <IngredientTO, int> ingredientRepository2 = new IngredientRepository2(memoryCtx);

                //ACT
                ingredientRepository2.Create(IngredientToUseInTest);
                memoryCtx.SaveChanges();
                //MealToUseInTest.Id = 1;
                IngredientToUseInTest.IsAllergen = false;
                ingredientRepository2.Edit(IngredientToUseInTest);
                var qtdEntriesToAssert = memoryCtx.SaveChanges();

                var IngredientToAssert = ingredientRepository2.GetById(1);
                //ASSERT
                Assert.AreEqual(1, qtdEntriesToAssert);
                Assert.AreEqual(1, ingredientRepository2.GetAll().Count());
                Assert.AreEqual(1, IngredientToAssert.Id);
                Assert.IsFalse(IngredientToAssert.IsAllergen);
                Assert.AreEqual("Ingr1EN", IngredientToAssert.Name.English);
            }
        }
Esempio n. 2
0
        public ActionResult Edit()
        {
            var id           = int.Parse(HttpContext.Request.Form["ItemId"]);
            var _currentItem = context.Items.Where(c => c.ItemId == id).FirstOrDefault();

            if (_currentItem == null)
            {
                _currentItem = new Item();
            }
            _currentItem.Name            = HttpContext.Request.Form["Name"];
            _currentItem.Description     = HttpContext.Request.Form["Description"];
            _currentItem.CategoryId      = int.Parse(HttpContext.Request.Form["CategoryId"]);
            _currentItem.Price           = double.Parse(HttpContext.Request.Form["Price"], CultureInfo.InvariantCulture);
            _currentItem.DiscountAmount  = double.Parse(HttpContext.Request.Form["DiscountAmount"], CultureInfo.InvariantCulture);
            _currentItem.DiscountPercent = double.Parse(HttpContext.Request.Form["DiscountPercent"], CultureInfo.InvariantCulture);

            if (_currentItem.ItemId == default(int))
            {
                context.Items.Add(_currentItem);
            }
            context.SaveChanges();
            if (Request.Files.Count > 0)
            {
                if (!string.IsNullOrEmpty(Request.Files[0].FileName))
                {
                    var newfile = Path.Combine(Server.MapPath("~"), "imgs", _currentItem.ItemId + ".jpg");
                    Request.Files[0].SaveAs(newfile);
                }
            }
            return(Redirect("/Admin"));
        }
Esempio n. 3
0
        public void IRepositoryDeleteByTransfertObject_ShouldDelete_WhenValidTOIsProvided()
        {
            //ASSERT
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                //ARRANGE
                var MealToUseInTest1 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal1EN", "Meal1FR", "Meal1NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 1,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var MealToUseInTest2 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal2EN", "Meal2FR", "Meal2NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 2,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var mealRepository = new MealRepository(memoryCtx);

                //ACT
                mealRepository.Add(MealToUseInTest1);
                mealRepository.Add(MealToUseInTest2);
                memoryCtx.SaveChanges();
                MealToUseInTest2.Id = 2;
                mealRepository.Remove(MealToUseInTest2);
                memoryCtx.SaveChanges();

                var retrievedMeals = mealRepository.GetAll();

                Assert.AreEqual(1, retrievedMeals.Count());
                Assert.IsFalse(retrievedMeals.Any(x => x.Id == 2));
            }
        }
Esempio n. 4
0
        public void IRepositoryDeleteByTranfertObject_ShouldThrowException_WhenDeletingANonExistantMeal()
        {
            //ASSERT
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                //ARRANGE
                var MealToUseInTest1 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal1EN", "Meal1FR", "Meal1NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 1,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var MealToUseInTest2 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal2EN", "Meal2FR", "Meal2NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 2,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var mealRepository = new MealRepository(memoryCtx);

                //ACT
                mealRepository.Add(MealToUseInTest1);
                mealRepository.Add(MealToUseInTest2);
                memoryCtx.SaveChanges();
                MealToUseInTest2.Id = 2;
                mealRepository.Remove(MealToUseInTest2);
                memoryCtx.SaveChanges();

                //ASSERT
                Assert.ThrowsException <Exception>(() => mealRepository.Remove(MealToUseInTest2));
            }
        }
Esempio n. 5
0
 public ActionResult Create(Meal meal)
 {
     if (ModelState.IsValid)
     {
         db.Meals.Add(meal);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(meal));
 }
Esempio n. 6
0
        public ActionResult Create([Bind(Include = "ID,type")] MealType mealType)
        {
            if (ModelState.IsValid)
            {
                db.MealTypes.Add(mealType);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(mealType));
        }
        public ActionResult Create([Bind(Include = "ID,name,validTo,validSince,mealTypeID")] Meal meal)
        {
            if (ModelState.IsValid)
            {
                db.Meals.Add(meal);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.mealTypeID = new SelectList(db.MealTypes, "ID", "type", meal.mealTypeID);
            return(View(meal));
        }
Esempio n. 8
0
        public void IRepositoryUpdate_ShouldUpdateInDb_WhenValidMealIsProvided()
        {
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                //ARRANGE
                var MealToUseInTest = new MealTO
                {
                    Id          = 1,
                    Name        = new MultiLanguageString("Meal1EN", "Meal1FR", "Meal1NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 2,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Salad
                };

                var mealRepository = new MealRepository(memoryCtx);

                //ACT
                mealRepository.Add(MealToUseInTest);
                memoryCtx.SaveChanges();
                //MealToUseInTest.Id = 1;
                MealToUseInTest.MealType = MealType.Sandwich;
                mealRepository.Update(MealToUseInTest);
                memoryCtx.SaveChanges();

                var MealToAssert = mealRepository.GetById(1);
                //ASSERT
                Assert.AreEqual(1, mealRepository.GetAll().Count());
                Assert.AreEqual(1, MealToAssert.Id);
                Assert.AreEqual(2, MealToAssert.Ingredients.FirstOrDefault().Id);
                Assert.AreEqual("Meal1EN", MealToAssert.Name.English);
                Assert.AreEqual(MealType.Sandwich, MealToAssert.MealType);
            }
        }
Esempio n. 9
0
        public void IRepositoryGetAll_ShouldRetrieveMeals_WhenCalled()
        {
            //ASSERT
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                //ARRANGE
                var MealToUseInTest1 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal1EN", "Meal1FR", "Meal1NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 1,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var MealToUseInTest2 = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal2EN", "Meal2FR", "Meal2NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 2,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var mealRepository = new MealRepository(memoryCtx);

                //ACT
                mealRepository.Add(MealToUseInTest1);
                mealRepository.Add(MealToUseInTest2);
                memoryCtx.SaveChanges();

                var retrievedMeals = mealRepository.GetAll();

                Assert.AreEqual(2, retrievedMeals.Count());
                Assert.AreEqual(1, retrievedMeals.FirstOrDefault().Id);
            }
        }
        public MealsController(MealContext context)
        {
            _context = context;

            if (_context.MealItems.Count() == 0)
            {
                _context.MealItems.Add(new MealItem {
                    Name = "Item1"
                });
                _context.SaveChanges();
            }
        }
Esempio n. 11
0
        public ActionResult review(int id, int qty)
        {
            var cart = Utils.GetCart(context, Request, Response);

            if (cart != null)
            {
                var item = cart.Items.Where(c => c.CartRowId == id).FirstOrDefault();
                if (item != null)
                {
                    if (qty <= 0)
                    {
                        cart.Items.Remove(item);
                        context.Entry(item).State = EntityState.Deleted;
                    }
                    else
                    {
                        item.Quantity = qty;
                    }
                    context.SaveChanges();
                }
            }
            LoadDeliveries();
            return(View("Index", cart));
        }
Esempio n. 12
0
        private static void CreateNewMeal(MealContext db)
        {
            Meal AMeal = new Meal();

            Console.Write("Enter Meat: ");
            AMeal.meat = Console.ReadLine();
            Console.Write("Enter Vegetable: ");
            AMeal.vegetable = Console.ReadLine();
            Console.Write("Other Item: ");
            AMeal.otherItem = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Adding Meal");
            db.Meals.Add(AMeal);
            Console.WriteLine("Meal Added, now Saving Changes");
            db.SaveChanges();
            Console.WriteLine("Changes Saved");
            Console.ReadLine();
            Console.Clear();
            SelectOption(db);
        }
Esempio n. 13
0
        public void IRepositoryGetByID_ShouldRetrieveMeal_WhenValidIdIsProvided()
        {
            //ARRANGE
            var options = new DbContextOptionsBuilder <MealContext>()
                          .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name)
                          .Options;

            using (var memoryCtx = new MealContext(options))
            {
                var MealToUseInTest = new MealTO
                {
                    Id          = 0,
                    Name        = new MultiLanguageString("Meal1EN", "Meal1FR", "Meal1NL"),
                    Ingredients = new List <IngredientTO> {
                        new IngredientTO {
                            Id   = 1,
                            Name = new MultiLanguageString("Ingr1EN", "Ingr1FR", "Ingr1NL"), IsAllergen = false
                        }
                    },
                    Supplier = new SupplierTO {
                        Name = "Fournisseur1"
                    },
                    MealType = MealType.Sandwich
                };

                var mealRepository = new MealRepository(memoryCtx);

                //ACT
                mealRepository.Add(MealToUseInTest);
                memoryCtx.SaveChanges();
                var retrivedMeal = mealRepository.GetById(1);

                //ASSERT
                Assert.AreEqual(1, retrivedMeal.Id);
                Assert.AreEqual("Meal1EN", mealRepository.GetById(1).Name.English);
            }
        }
Esempio n. 14
0
        public ActionResult AddToCart(int?id)
        {
            var ret = View("Index", context.Items.ToList());

            if (id == null)
            {
                return(ret);
            }
            var item = context.Items.Where(c => c.ItemId == id).FirstOrDefault();

            if (item == null)
            {
                return(ret);
            }
            var      meal_id  = Request.Cookies["meal_id"]?.Value;
            CartHead cartHead = null;

            if (string.IsNullOrEmpty(meal_id))
            {
                Guid g = Guid.NewGuid();
                meal_id = g.ToString();
                var cook = new HttpCookie("meal_id");
                cook.Expires = DateTime.Now.AddDays(1);
                cook.Value   = meal_id;
                Response.Cookies.Add(cook);
                cartHead = new CartHead
                {
                    CreationDate = DateTime.Now,
                    meal_id      = meal_id,
                    Items        = new List <CartRows>()
                };
                context.Cart.Add(cartHead);
            }
            else
            {
                cartHead = context.Cart
                           .Where(c => c.meal_id == meal_id)
                           .Include(c => c.Items)
                           .FirstOrDefault();
            }
            if (cartHead == null)
            {
                return(ret);
            }
            CartRows cartrow = null;

            cartrow = cartHead.Items?.Where(c => c.ItemId == item.ItemId).FirstOrDefault();
            if (cartrow == null)
            {
                cartrow = new CartRows
                {
                    Quantity        = 1,
                    Item            = item,
                    ItemDescription = item.Description,
                    ItemName        = item.Name,
                    ItemPrice       = Utils.GetPrice(item)
                };
                cartHead.Items.Add(cartrow);
            }
            else
            {
                cartrow.Quantity += 1;
            }
            context.SaveChanges();
            return(ret);
        }