public string[] InsertandgetBlackList(string category, Prj4databaseContext context) { if (!context.Blacklist.Any(b => b.Category.Equals(category))) { context.Blacklist.Add(new Blacklist { Category = category }); context.SaveChanges(); } var blacklist = (from c in context.Blacklist select c.Category).ToArray(); return(blacklist); }
public async Task <string> GenerateShoppingCart(string words, string stores, Prj4databaseContext context) { string initString = "" + "<html>"; string endString = "</html>"; string bodystring = ""; string[] storeSplit = new string[8]; if (stores != null) { storeSplit = stores.Split(';', StringSplitOptions.RemoveEmptyEntries); } RecipeQuery query = new RecipeQuery { LoadIngredientList = true, LoadRecipeCategory = true, SearchRecipe = words, NumberOfRecipes = 1, Stores = storeSplit }; RecipeRepository recipeRepository = new RecipeRepository(context); var result = await recipeRepository.Get(query); foreach (var recipe in result) { bodystring = ""; string ingrediensstring = ""; bodystring += "<h3><strong>Indkøbsliste</strong></h3>"; bodystring += "<ul>"; foreach (var ingredient in recipe.IngredientList.Ingredient) { ingrediensstring += "<li class='p6'>" + ingredient.Name + " - " + " Købes i " + ingredient.Product.RetailChain.Name + " for " + ingredient.Product.Price + " kr. " + "</li>"; } bodystring += ingrediensstring; bodystring += "</ul>" + "</div>" + "<br style='clear:both' />" + "<ul>" + "</div>"; } return(initString + bodystring + endString); }
public void SetUp() { string ConnectionString = "Data Source=prj4-server.database.windows.net;Initial Catalog=prj4-database;User ID=maloudt;Password=Mldt1160"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); var options = new DbContextOptionsBuilder <Prj4databaseContext>() .UseSqlServer(conn).Options; _context = new Prj4databaseContext(options); _uut = new RecipeRepository(_context); }
public void Setup() { ProductQuery = Substitute.For <IQuery <Product> >(); RecipeQuesry = Substitute.For <IQuery <Recipe> >(); Product = Substitute.For <Product>(); _uut = new HtmlCalculator(); string ConnectionString = "Data Source=prj4-server.database.windows.net;Initial Catalog=prj4-database;User ID=maloudt;Password=Mldt1160"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); var options = new DbContextOptionsBuilder <Prj4databaseContext>() .UseSqlServer(conn).Options; _context = new Prj4databaseContext(options); }
public async Task <IEnumerable <Product> > Execute(Prj4databaseContext context) { List <RetailChain> irrelevantStores = new List <RetailChain>(); foreach (var store in Stores) { var dbStore = await context.Set <RetailChain>() .Where(rc => rc.Name.Contains(store)) .Take(1) .ToListAsync(); if (dbStore.Any()) { irrelevantStores.Add(dbStore.First()); } } if (LoadProductCategory == true && LoadRetailChain == false) { return(await context.Set <Product>() .Where(r => r.Name.Contains(SearchName) && r.ValidTo.ToString().Contains(ValidToDate)) .OrderBy(p => p.Price) .Include(r => r.ProductCategory) .Take(NumberOfProducts) .ToListAsync()); } else if (LoadRetailChain == true && LoadProductCategory == false) { return(await context.Set <Product>() .Where(r => r.Name.Contains(SearchName) && r.ValidTo.ToString().Contains(ValidToDate) && !irrelevantStores.Contains(r.RetailChain)) .OrderBy(p => p.Price) .Include(r => r.RetailChain) .Take(NumberOfProducts) .ToListAsync()); } else if (LoadProductCategory == true && LoadRetailChain == true) { return(await context.Set <Product>() .Where(r => r.Name.Contains(SearchName) && r.ValidTo.ToString().Contains(ValidToDate) && !irrelevantStores.Contains(r.RetailChain)) .OrderBy(p => p.Price) .Include(r => r.RetailChain) .Include(r => r.ProductCategory) .Take(NumberOfProducts) .ToListAsync()); } else { return(await context.Set <Product>() .Where(r => r.Name.Contains(SearchName) && r.ValidTo.ToString().Contains(ValidToDate)) .OrderBy(p => p.Price) .Take(NumberOfProducts) .ToListAsync()); } }
public static void InsertVare(string json, Prj4databaseContext context) { JObject jo = JObject.Parse(json); var array = (JArray)jo["adverts"]; foreach (JObject data in array.Children <JObject>()) { Product vare = new Product(); string kategori = ""; foreach (JProperty prop in data.Properties()) { string propname = prop.Name; switch (propname) { case "title": string str = (string)prop.Value; if (str.Length >= 49) { vare.Name = str.Substring(0, 49); } else { vare.Name = str; } break; case "price": vare.Price = (double)prop.Value; break; case "customerName": string retailChain = (string)prop.Value; if (context.RetailChain.Any(r => r.Name.Equals(retailChain))) { var dbRetail = context.RetailChain.Where(r => r.Name.Equals(retailChain)).First(); vare.RetailChainId = dbRetail.RetailChainId; } break; case "validFrom": vare.ValidFrom = DateTime.ParseExact((string)prop.Value, "MM/dd/yyyy HH:mm:ss", null); break; case "validTo": vare.ValidTo = DateTime.ParseExact((string)prop.Value, "MM/dd/yyyy HH:mm:ss", null); break; case "volumePrice": vare.VolumePrice = (double)prop.Value; if (vare.VolumePrice != 0) { vare.Volume = Math.Round((double)vare.Price / (double)vare.VolumePrice, 2); } else { vare.Volume = 0; } break; case "imageUrl": vare.ImgSrc = (string)prop.Value; break; case "category": kategori = (string)prop.Value["name"]; break; } } //FILTERING //******************// //Check if name and store already exists //filter based on Black list if (context.Blacklist.Where(b => b.Category.Equals(kategori)).Any()) { continue; } if (!context.Product.Any(v => v.Name == vare.Name && v.RetailChainId == vare.RetailChainId)) { context.Product.Add(vare); if (!context.Category.Any(k => k.CategoryName.Equals(kategori))) { context.Category.Add(new Category() { CategoryName = kategori }); } context.SaveChanges(); var vk = new ProductCategory { ProductId = vare.ProductId, CategoryName = kategori }; context.ProductCategory.Add(vk); context.SaveChanges(); } // Check valid date } }
public IngredientRepository(Prj4databaseContext context) { this._context = context; }
public async Task <string> CreateRecipeToDatabase(string name, int prepareTime, string description, string ingridientName, string ingridientAmount, string ingridientUnit, string imgUrl, Prj4databaseContext context) { RecipeQuery recipeQuery = new RecipeQuery { SearchRecipe = name }; var queryResult = await recipeQuery.Execute(context); if (queryResult.Any()) { return("Opskrift findes allerede"); } Recipe recipe = new Recipe { Name = name, CookTime = prepareTime, ImgSrc = imgUrl, Servings = 4 }; RecipeRepository recipeRepository = new RecipeRepository(context); recipeRepository.Insert(recipe); recipeRepository.Save(); string[] descriptionData = description.Split(';', StringSplitOptions.RemoveEmptyEntries); int sizeDescription = descriptionData.Length; var directions = new Directions[sizeDescription]; for (int i = 0; i < sizeDescription; i++) { directions[i] = new Directions { Description = descriptionData[i], RecipeId = recipe.RecipeId }; } DirectionsRepository directionsRepository = new DirectionsRepository(context); foreach (var direc in directions) { directionsRepository.Insert(direc); } directionsRepository.Save(); var ingredientLists = new IngredientList { RecipeId = recipe.RecipeId, }; IngredientListRepository ingredientListRepository = new IngredientListRepository(context); ingredientListRepository.Insert(ingredientLists); ingredientListRepository.Save(); string[] nameSplit = ingridientName.Split(';', StringSplitOptions.RemoveEmptyEntries); string[] amountSplit = ingridientAmount.Split(';', StringSplitOptions.RemoveEmptyEntries); string[] unitSplit = ingridientUnit.Split(';', StringSplitOptions.RemoveEmptyEntries); int sizeIngridient = nameSplit.Length; var ingridients = new Ingredient[sizeIngridient]; var produkt = await context.Set <Product>().Where(p => p.Name.Contains("a")).Take(1).ToListAsync(); for (int i = 0; i < sizeIngridient; i++) { double.TryParse(amountSplit[i], out var amountDouble); ingridients[i] = new Ingredient { Name = nameSplit[i], Amount = amountDouble, AmountUnit = unitSplit[i], ProductId = produkt[0].ProductId, IngredientListId = ingredientLists.IngredientListId }; } IngredientRepository ingredientRepository = new IngredientRepository(context); foreach (var ingridient in ingridients) { ingredientRepository.Insert(ingridient); } ingredientRepository.Save(); return(HtmlToRecipe(name, prepareTime, description, ingridientName, ingridientAmount, ingridientUnit, imgUrl)); }
public async Task <IEnumerable <Recipe> > Execute(Prj4databaseContext context) { var relevantRecipes = await context.Set <Ingredient>() .Where(i => i.Name.Contains(SearchIngredient)) .Select(i => i.IngredientList.RecipeId) .ToListAsync(); if (LoadIngredientList && LoadRecipeCategory) { var recipes = await context.Set <Recipe>() .Where(r => r.Name.Contains(SearchRecipe) && relevantRecipes.Contains(r.RecipeId)) .Include(r => r.IngredientList.Ingredient) .ThenInclude(i => i.Product) .Include(r => r.RecipeCategory) .Include(r => r.Directions) .OrderBy(r => r.SavingsAbsolute) .Take(NumberOfRecipes) .ToListAsync(); IngredientRepository ingredientRepository = new IngredientRepository(context); foreach (var recipe in recipes) { foreach (var ingredient in recipe.IngredientList.Ingredient) { ProductQuery productQuery = new ProductQuery { SearchName = ingredient.Name, NumberOfProducts = 1, LoadRetailChain = true, Stores = Stores, ValidToDate = ValidToDate }; var product = await productQuery.Execute(context); if (product.Any()) { ingredient.ProductId = product.First().ProductId; ingredientRepository.Update(ingredient); } } } ingredientRepository.Save(); return(recipes); } else if (LoadIngredientList && !LoadRecipeCategory) { var recipes = await context.Set <Recipe>() .Where(r => r.Name.Contains(SearchRecipe) && relevantRecipes.Contains(r.RecipeId)) .Include(r => r.IngredientList.Ingredient) .ThenInclude(i => i.Product) .Include(r => r.Directions) .OrderBy(r => r.SavingsAbsolute) .Take(NumberOfRecipes) .ToListAsync(); IngredientRepository ingredientRepository = new IngredientRepository(context); foreach (var recipe in recipes) { foreach (var ingredient in recipe.IngredientList.Ingredient) { ProductQuery productQuery = new ProductQuery { SearchName = $"{ingredient.Name}", NumberOfProducts = 1, LoadRetailChain = true, Stores = Stores, ValidToDate = ValidToDate }; var product = await productQuery.Execute(context); if (product.Any()) { ingredient.ProductId = product.First().ProductId; ingredientRepository.Update(ingredient); } } } ingredientRepository.Save(); return(recipes); } else if (!LoadIngredientList && LoadRecipeCategory) { var recipes = await context.Set <Recipe>() .Where(r => r.Name.Contains(SearchRecipe) && relevantRecipes.Contains(r.RecipeId)) .Include(r => r.RecipeCategory) .Include(r => r.Directions) .OrderBy(r => r.SavingsAbsolute) .Take(NumberOfRecipes) .ToListAsync(); return(recipes); } else { var recipes = await context.Set <Recipe>() .Where(r => r.Name.Contains(SearchRecipe) && relevantRecipes.Contains(r.RecipeId)) .Include(r => r.Directions) .OrderBy(r => r.SavingsAbsolute) .Take(NumberOfRecipes) .ToListAsync(); return(recipes); } }
public RetailChainRepository(Prj4databaseContext context) { this._context = context; }
public ProductRepository(Prj4databaseContext context) { this._context = context; }
public async Task <string> ShowRecipeSmallViewSearchAsync(string word, string stores, Prj4databaseContext context) { string initString = "" + "<html>"; string style = "<head>" + "<style>" + ".viewOfRecipe{" + "width: 60%;" + "height: 200px;" + "border: 2px solid;" + "padding: 2px;" + "margin: 20px;}" + ".img1{" + "display: block;" + "position: absolute;" + "width: 350px;" + "height: 200px;}" + ".textForPrice{" + "display: block;" + "position: relative;" + "float: left;" + "margin-left: 355px;" + "font-size: 20px;}" + "</style>" + "</head> "; string endString = "</html>"; string bodystring = ""; HtmlCalculator calculator = new HtmlCalculator(); string[] storeSplit = new string[8]; string[] storeSplitfake = new string[8]; if (stores != null) { storeSplit = stores.Split(';', StringSplitOptions.RemoveEmptyEntries); } RecipeQuery query = new RecipeQuery { SearchRecipe = word, NumberOfRecipes = 5, LoadIngredientList = true, }; RecipeRepository recipeRepository = new RecipeRepository(context); var recipeQuery = await recipeRepository.Get(query); if (recipeQuery.Count() == 0) { return("Ingen opskrift fundet"); } foreach (var recipe in recipeQuery) { double originalPrice = 0; double salePrice = 0; double lowestPrice = 0; if (stores == null) { if (recipe.Price != null) { originalPrice = (double)recipe.Price; } if (recipe.SavingsAbsolute != null) { salePrice = (double)recipe.SavingsAbsolute; lowestPrice = (double)recipe.SavingsAbsolute; } } else { originalPrice = await calculator.NormalPrice(recipe, recipe.Name, storeSplit, context); salePrice = await calculator.TotalPrice(recipe, recipe.Name, storeSplit, context); lowestPrice = await calculator.TotalPrice(recipe, recipe.Name, storeSplitfake, context); } bodystring += "<div class='viewOfRecipe'>" + "<div class='imageOfRecipe'>" + "<a href='/#/Recipe/" + recipe.Name.Replace(" ", string.Empty).Replace("æ", string.Empty).Replace("ø", string.Empty).Replace("å", string.Empty) + "'>" + "<img class='img1' src='" + recipe.ImgSrc + "' alt='recpieImg'></a>" + "</div>" + "<div class='textForPrice'>" + "<div style='font-size: 25px;'>" + "<a href='/#/Recipe/" + recipe.Name.Replace(" ", string.Empty).Replace("æ", string.Empty).Replace("ø", string.Empty).Replace("å", string.Empty) + "'>" + recipe.Name + "</a>" + "<br />" + "</div>" + "Original pris: " + originalPrice + "kr." + " <br />" + "Pris med rabat: " + salePrice + "kr." + "<br />" + "Laveste mulige pris: " + lowestPrice + "kr." + "<br />" + "</div>" + "</div>"; } return(initString + style + bodystring + endString); }
public CategoryRepository(Prj4databaseContext context) { this._context = context; }
public static void Update(Prj4databaseContext context) { context.ProductCategory.RemoveRange(context.ProductCategory.Where(pc => pc.Product.ValidTo < DateTime.UtcNow)); context.Product.RemoveRange(context.Product.Where(p => p.ValidTo < DateTime.UtcNow)); context.SaveChanges(); }
public async Task <double> NormalPrice(Recipe recipe, string word, string[] stores, Prj4databaseContext context) { double normalPrice = 0; ProductRepository productRepository = new ProductRepository(context); //take all ingredients in the ingredientlist foreach (var ingredient in recipe.IngredientList.Ingredient) { ProductQuery productQuery = new ProductQuery { ValidToDate = "2050", NumberOfProducts = 1, Stores = stores, SearchName = ingredient.Name, LoadRetailChain = true }; var listProduct = await productRepository.Get(productQuery); if (listProduct.Any()) { foreach (var product in listProduct) { if (product.Name != null) { normalPrice += product.Price; } } } else { normalPrice += ingredient.Product.Price; } } normalPrice = Math.Round(normalPrice); return(normalPrice); }
public TilbudController(Prj4databaseContext context) { _context = context; }
public DirectionsRepository(Prj4databaseContext context) { this._context = context; }
public RecipeController(Prj4databaseContext context) { _context = context; }
public RecipeRepository(Prj4databaseContext context) { this._context = context; }
public OpenHoursRepository(Prj4databaseContext context) { this._context = context; }
public async Task <double> TotalPrice(Recipe recipe, string word, string[] stores, Prj4databaseContext context) { double totalPrice = 0; ProductRepository productRepository = new ProductRepository(context); //take all ingredients in the ingredientlist foreach (var ingredient in recipe.IngredientList.Ingredient) { ProductQuery productQuery = new ProductQuery { NumberOfProducts = 1, Stores = stores, SearchName = ingredient.Name, LoadRetailChain = true }; var listProduct = await productRepository.Get(productQuery); if (listProduct.Any()) { totalPrice += listProduct.First().Price; } else { totalPrice += ingredient.Product.Price; } } totalPrice = Math.Round(totalPrice); return(totalPrice); }
public async Task <string> ShowRecipeFullView(string words, double antal, Prj4databaseContext context) { string initString = "" + "<html>"; string endString = "</html>"; string bodystring = ""; antal /= 4; RecipeQuery query = new RecipeQuery { LoadIngredientList = true, LoadRecipeCategory = true, SearchRecipe = words, NumberOfRecipes = 1, }; RecipeRepository recipeRepository = new RecipeRepository(context); var result = await recipeRepository.Get(query); foreach (var recipe in result) { bodystring = ""; string ingrediensstring = ""; string directionsstring = ""; bodystring += "<h1>" + recipe.Name + "</h1>" + "<div class='recipe'>" + "<div class='ingredienser'>" + "<p class='p2'><span class='s1'>" + recipe.CookTime + " min tilberednings tid" + "<div class='image'>" + "<img src = '" + recipe.ImgSrc + "' height='400' width='700'/>" + "</div>" + "<br style='clear: both' />" + "<h3 class='p3'><strong>Fremgangsmåde</strong></h3>" + "<div class='i1'>" + "<ul>"; foreach (var direction in recipe.Directions) { directionsstring += "<li class='p5'>" + direction.Description + "</li>"; } bodystring += directionsstring; bodystring += "</ul>" + "<h3><strong>Ingredienser</strong></h3>" + "<ul>"; foreach (var ingredient in recipe.IngredientList.Ingredient) { ingrediensstring += "<li class='p6'>" + (ingredient.Amount * antal) + ingredient.AmountUnit + " " + ingredient.Name + "</li>"; } bodystring += ingrediensstring; bodystring += "</ul>" + "</div>" + "<br style='clear:both' />" + "<ul>" + "</div>"; } return(initString + bodystring + endString); }