public void AddImages(long foodId, string[] images, out bool result, out string message) { _foodImageRepository.RemoveMultiple(_foodImageRepository.FindAll(x => x.FoodId == foodId).ToList(), out result, out message); FoodImage foodImage; foreach (var image in images) { foodImage = new FoodImage(); foodImage.FoodId = foodId; foodImage.Path = image; foodImage.Caption = string.Empty; foodImage.Active = true; _foodImageRepository.Add(foodImage, out result, out message); if (!result) { break; } } if (result) { SaveChanges(); } else { Dispose(); } }
public IActionResult Add([FromForm(Name = ("Image"))] IFormFile file, [FromForm] FoodImage foodImage) { var result = _foodImageService.Add(file, foodImage); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public IResult Add(IFormFile file, FoodImage foodImage) { IResult result = BusinessRules.Run(CheckImageLimitExceeded(foodImage.FoodId)); if (result != null) { return(result); } foodImage.ImagePath = FileHelper.Add(file); foodImage.Date = DateTime.Now; _foodImageDal.Add(foodImage); return(new SuccessResult()); }
public IResult Update(IFormFile file, FoodImage foodImage) { IResult result = BusinessRules.Run(CheckImageLimitExceeded(foodImage.FoodId)); if (result != null) { return(result); } var oldPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\wwwroot")) + _foodImageDal.Get(f => f.FoodId == foodImage.FoodId).ImagePath; foodImage.ImagePath = FileHelper.Update(oldPath, file); foodImage.Date = DateTime.Now; _foodImageDal.Update(foodImage); return(new SuccessResult()); }
private void HandleSelection(object sender, EventArgs e) { selected = (FoodImage)sender; foreach (var im in allImages) { Border parent = (Border)im.Parent; if (selected.id != im.id) { parent.BorderBrush = Brushes.DarkSalmon; } else { parent.BorderBrush = Brushes.Black; } } }
public async Task <bool> EditFood(EditFoodViewModel foodEdit) { try { Food food = await GetFood(foodEdit.FoodId); _context.Foods.Attach(food); if (foodEdit.ImagesChange.Count > 0) { foreach (FoodImage img in food.FoodImages.ToList()) { string filePath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods", img.FileName); File.Delete(filePath); } food.FoodImages.Clear(); foreach (IFormFile img in foodEdit.ImagesChange) { string fileName = $"{Guid.NewGuid().ToString()}_{img.FileName}"; string filePath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods", fileName); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { await img.CopyToAsync(fs); } FoodImage newImage = new FoodImage() { FileName = fileName, ImagesId = Guid.NewGuid().ToString(), FoodId = foodEdit.FoodId }; food.FoodImages.Add(newImage); } } food.IsActive = foodEdit.IsActive; food.Name = foodEdit.FoodName; food.Price = foodEdit.Price; await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
public async Task <IActionResult> Create(CreateFoodViewModel model) { if (ModelState.IsValid) { Food newFood = new Food() { FoodId = Guid.NewGuid().ToString(), IsActive = model.IsActive, Name = model.FoodName, Price = model.Price }; string folderPath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods"); string fileName = String.Empty; string filePath = String.Empty; List <FoodImage> images = new List <FoodImage>(); if (model.Images.Count > 0) { foreach (IFormFile img in model.Images) { fileName = $"{Guid.NewGuid()}_{img.FileName}"; filePath = Path.Combine(path1: folderPath, path2: fileName); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { await img.CopyToAsync(fs); FoodImage image = new FoodImage() { ImagesId = Guid.NewGuid().ToString(), FoodId = newFood.FoodId, FileName = fileName, }; images.Add(image); } } newFood.FoodImages = new List <FoodImage>(images); } if (await _foodServices.CreateNewFood(newFood)) { return(RedirectToAction(actionName: "Manage", controllerName: "Food")); } ModelState.AddModelError("", "Something was wrong, please try again later"); } return(View(model)); }
private void Window_Loaded(object sender, RoutedEventArgs e) { DirectoryInfo directory = new DirectoryInfo(IMAGE_PATH); FileInfo[] imageFiles = directory.GetFiles(); int id = 0; foreach (FileInfo imageFile in imageFiles) { // read the bitmap BitmapImage bitmap = new BitmapImage(); using (FileStream imageReader = new FileStream(imageFile.FullName, FileMode.Open, FileAccess.Read)) { bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = imageReader; bitmap.EndInit(); bitmap.Freeze(); // optional } // contruct the image FoodImage image = new FoodImage(id, imageFile.Name); image.Source = bitmap; image.MaxHeight = 100; image.MaxWidth = 100; image.ImageSelected += HandleSelection; allImages.Add(image); id++; // image inside border Border border = new Border(); border.BorderBrush = Brushes.DarkSalmon; border.BorderThickness = new Thickness(2); border.Child = image; imagePanel.Children.Add(border); } }
public IResult Delete(FoodImage foodImage) { FileHelper.Delete(foodImage.ImagePath); _foodImageDal.Delete(foodImage); return(new SuccessResult()); }