public async Task <IActionResult> Create([Bind("DishId,Name,Price,Image,DishCategoryId")] Dish dish, IFormCollection collection, IFormFile file) { var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } dish.Image = LoadImage.GetPictureData(filePath); List <Ingredient> testList = new List <Ingredient>(); foreach (var item in collection.Keys.Where(m => m.StartsWith("ingredient-"))) { var listIngredient = _context.Ingredients.FirstOrDefault(d => d.IngredientId == Int32.Parse(item.Remove(0, 11))); testList.Add(listIngredient); DishIngredient di = new DishIngredient() { Dish = dish, Ingredient = listIngredient }; _context.DishIngredients.Add(di); } if (ModelState.IsValid) { _context.Add(dish); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DishCategoryId"] = new SelectList(_context.DishCategories, "DishCategoryId", "Description", dish.DishCategoryId); return(View(dish)); }
public async Task <IActionResult> Edit(int id, [Bind("DishId,Name,Price,DishCategoryId")] Dish dish, IFormCollection collection, IFormFile file) { if (file != null) { var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } dish.Image = LoadImage.GetPictureData(filePath); } else { dish.Image = await _context.Dishes.Where(x => x.DishId == dish.DishId).Select(x => x.Image).FirstOrDefaultAsync(); } _dishService.RemoveIngredients(id); List <Ingredient> testList = new List <Ingredient>(); foreach (var item in collection.Keys.Where(m => m.StartsWith("ingredient-"))) { var listIngredient = _context.Ingredients.FirstOrDefault(d => d.IngredientId == Int32.Parse(item.Remove(0, 11))); testList.Add(listIngredient); DishIngredient di = new DishIngredient() { Dish = dish, Ingredient = listIngredient }; _context.DishIngredients.Add(di); } if (id != dish.DishId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(dish); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DishExists(dish.DishId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["DishCategoryId"] = new SelectList(_context.DishCategories, "DishCategoryId", "Description", dish.DishCategoryId); return(View(dish)); }
public IActionResult Index() { // var base64 = Convert.ToBase64String(item.BusLogo); // var imagesrc = string.Format("data:image/png;base64,{0}", base64); //< img src = '@imagesrc' style = "max-width:100px;max-height:100px" /> #region MyRegion //File(new System.IO.MemoryStream(Convert.ToByte("hello world ")), "application/pdf", "filename.pdf") #endregion //F:\Publish\WebApplicationFaceBook\src\WebApplicationno\wwwroot\images\1ad6d6d772966fc112a68096dee8a315.png byte[] a = { 1, 3, 5 }; byte[] b = { 2, 6, 7 }; byte[] c = Microsoft.Azure.Devices.Client.SHA.computeHMAC_SHA256(a, b); string path = FileStrem.GetFilePath("wwwroot/images/1ad6d6d772966fc112a68096dee8a315.png"); byte[] imagebyte = LoadImage.GetPictureData(path); ViewBag.imagebyte = imagebyte; var base64 = Convert.ToBase64String(imagebyte); ViewBag.imagesrc = string.Format("data:image/png;base64,{0}", base64); ViewBag.imagelegth = base64.Length; return(View()); }
public static void Initialize(ApplicationDbContext context, UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager) { if (context.Users.ToList().Count == 0) { var aUser = new ApplicationUser(); aUser.UserName = "******"; aUser.Email = "*****@*****.**"; var r = userManager.CreateAsync(aUser, "Pa$$w0rd").Result; var adminUser = new ApplicationUser(); adminUser.UserName = "******"; adminUser.Email = "*****@*****.**"; adminUser.PhoneNumber = "0701111222"; adminUser.CustomerName = "Madoka"; adminUser.Street = "Kungsgatan 123"; adminUser.PostalCode = "12345"; adminUser.City = "Stockholm"; var adminUserResult = userManager.CreateAsync(adminUser, "Pa$$w0rd").Result; var adminRole = new IdentityRole { Name = "Admin" }; var roleResult = roleManager.CreateAsync(adminRole).Result; userManager.AddToRoleAsync(adminUser, "Admin").Wait(); } //Om det inte finns Dishes i databasen if (context.Dishes.ToList().Count == 0) { var pizzaImage = LoadImage.GetPictureData("wwwroot/images/pizza.jpg"); var carzonesImage = LoadImage.GetPictureData("wwwroot/images/carzone.jpg"); var dessertImage = LoadImage.GetPictureData("wwwroot/images/dessert.jpg"); var pizza = new DishCategory { Description = "Pizza" }; var carzone = new DishCategory { Description = "Calzone" }; var dessert = new DishCategory { Description = "Dessert" }; context.DishCategories.Add(pizza); context.DishCategories.Add(carzone); context.DishCategories.Add(dessert); var cheese = new Ingredient { Name = "Cheese", Price = 30 }; var tomato = new Ingredient { Name = "Tomato", Price = 10 }; var ham = new Ingredient { Name = "Ham", Price = 25 }; var pineapple = new Ingredient { Name = "Pineapple", Price = 10 }; var bacon = new Ingredient { Name = "Bacon", Price = 25 }; var onions = new Ingredient { Name = "Onions", Price = 10 }; var mushrooms = new Ingredient { Name = "Mushrooms", Price = 20 }; var apple = new Ingredient { Name = "Apple", Price = 20 }; var currySauce = new Ingredient { Name = "Curry sauce", Price = 15 }; var banana = new Ingredient { Name = "Banana", Price = 10 }; var capricciosa = new Dish { Name = "Capricciosa", Price = 79, Image = pizzaImage, DishCategory = pizza }; var margaritha = new Dish { Name = "Margaritha", Price = 69, Image = pizzaImage, DishCategory = pizza }; var hawaii = new Dish { Name = "Hawaii", Price = 85, Image = pizzaImage, DishCategory = pizza }; var tropical = new Dish { Name = "Tropical", Price = 75, Image = pizzaImage, DishCategory = pizza }; var veggie = new Dish { Name = "Veggie", Price = 95, Image = pizzaImage, DishCategory = pizza }; var calzone = new Dish { Name = "Calzone", Price = 100, Image = carzonesImage, DishCategory = carzone }; var calzoneSp = new Dish { Name = "Calzone SP", Price = 115, Image = carzonesImage, DishCategory = carzone }; var applePie = new Dish { Name = "Apple pie", Price = 70, Image = dessertImage, DishCategory = dessert }; var capricciosaHam = new DishIngredient { Dish = capricciosa, Ingredient = ham }; var capricciosaMushrooms = new DishIngredient { Dish = capricciosa, Ingredient = mushrooms }; capricciosa.DishIngredients = new List <DishIngredient>(); capricciosa.DishIngredients.Add(capricciosaHam); capricciosa.DishIngredients.Add(capricciosaMushrooms); var margarithaCheese = new DishIngredient { Dish = margaritha, Ingredient = cheese }; margaritha.DishIngredients = new List <DishIngredient>(); margaritha.DishIngredients.Add(margarithaCheese); var hawaiiHam = new DishIngredient { Dish = hawaii, Ingredient = ham }; var hawaiiPineapple = new DishIngredient { Dish = hawaii, Ingredient = pineapple }; hawaii.DishIngredients = new List <DishIngredient>(); hawaii.DishIngredients.Add(hawaiiHam); hawaii.DishIngredients.Add(hawaiiPineapple); var tropicalBanana = new DishIngredient { Dish = tropical, Ingredient = banana }; var tropicalCurrySauce = new DishIngredient { Dish = tropical, Ingredient = currySauce }; tropical.DishIngredients = new List <DishIngredient>(); tropical.DishIngredients.Add(tropicalBanana); tropical.DishIngredients.Add(tropicalCurrySauce); var veggieMushrooms = new DishIngredient { Dish = veggie, Ingredient = mushrooms }; var veggieOnions = new DishIngredient { Dish = veggie, Ingredient = onions }; var veggieTomato = new DishIngredient { Dish = veggie, Ingredient = tomato }; veggie.DishIngredients = new List <DishIngredient>(); veggie.DishIngredients.Add(veggieMushrooms); veggie.DishIngredients.Add(veggieOnions); veggie.DishIngredients.Add(veggieTomato); var calzoneHam = new DishIngredient { Dish = calzone, Ingredient = ham }; calzone.DishIngredients = new List <DishIngredient>(); calzone.DishIngredients.Add(calzoneHam); var calzoneSpHam = new DishIngredient { Dish = calzoneSp, Ingredient = ham }; var calzoneSpMushrooms = new DishIngredient { Dish = calzoneSp, Ingredient = mushrooms }; calzoneSp.DishIngredients = new List <DishIngredient>(); calzoneSp.DishIngredients.Add(calzoneSpHam); calzoneSp.DishIngredients.Add(calzoneSpMushrooms); var applePieApple = new DishIngredient { Dish = applePie, Ingredient = apple }; applePie.DishIngredients = new List <DishIngredient>(); applePie.DishIngredients.Add(applePieApple); context.Dishes.Add(capricciosa); context.Dishes.Add(margaritha); context.Dishes.Add(hawaii); context.Dishes.Add(tropical); context.Dishes.Add(veggie); context.Dishes.Add(calzone); context.Dishes.Add(calzoneSp); context.Dishes.Add(applePie); var visa = new Card { Name = "Visa" }; var masterCard = new Card { Name = "Master card" }; context.Cards.Add(visa); context.Cards.Add(masterCard); context.SaveChanges(); } }