public async Task <List <Image> > UpdateFromForm(Dictionary <int, ImageFormVM> imageForms) { var keys = imageForms.Keys.ToArray(); var images = await _db.Images .Include(i => i.Category) .Include(i => i.ImageTags).ThenInclude(it => it.Tag) .Where(i => keys.Contains(i.Id)).ToListAsync(); // TODO: Handle renaming foreach (var imageFormEntry in imageForms) { var imgForm = imageFormEntry.Value; var img = images.Find(i => i.Id == imgForm.Id); if (img == null) { continue; } img.Name = imgForm.Name; await SetCategory(img, imgForm); await SetTags(img, imgForm); if (string.IsNullOrWhiteSpace(imgForm.Tags) && img.ImageTags != null) { _db.ImageTags.RemoveRange(img.ImageTags); } } await _db.SaveChangesAsync(); PendingCategories = new List <Category>(); PendingTags = new List <Tag>(); return(images); }
public async Task <IActionResult> AddDirectory() { var directory = HttpContext.Request.Form["directory"]; var files = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories); foreach (var file in files) { var match = await _db.Images.FirstOrDefaultAsync(i => i.Path == file); if (match == null) { _db.Images.Add(new Image { Path = file }); } } await _db.SaveChangesAsync(); return(RedirectToAction("Index")); }