public BackedUpDirectory GetDirectory(int id, bool includeChildren = false, bool includeParent = false) { BackedUpDirectory dir = null; using (var context = new AppDbContext(_dbConnectionConfig)) { dir = context.Directories.FirstOrDefault(d => d.Id == id); if (dir != null) { if (includeChildren) { context.Files.Where(f => f.ParentId == dir.Id).Load(); context.Directories.Where(d => d.ParentId == dir.Id).Load(); } if (includeParent) { context.Directories.Where(d => d.Id == dir.ParentId).Load(); } var history = context.DirectoryHistory.OrderByDescending(h => h.LastSeen) .FirstOrDefault(h => (h.DirectoryId == dir.Id)); dir.Modified = history.Modified; dir.Deleted = history.Deleted; } } return(dir); }
public void AddDirectory(BackedUpDirectory directory) { using (var context = new AppDbContext(_dbConnectionConfig)) { context.Directories.Add(directory); context.DirectoryHistory.Add(new DirectoryHistory() { DirectoryId = directory.Id, LastSeen = DateTime.Now, Modified = directory.Modified }); context.SaveChanges(); } }
public void UpdateDirectory(BackedUpDirectory directory) { using (var context = new AppDbContext(_dbConnectionConfig)) { //var dir = context.Directories.FirstOrDefault(d => d == directory); //if (dir == null) return false; //if (directory.Files != null) //{ // dir.Files = directory.Files; //} //dir.Modified = directory.Modified; //context.SaveChanges(); //return true; context.DirectoryHistory.Add(new DirectoryHistory() { DirectoryId = directory.Id, Modified = directory.Modified, LastSeen = DateTime.UtcNow }); context.SaveChanges(); } }
//public void AddDirectories(IEnumerable<BackedUpDirectory> directories) //{ // using (var context = new AppDbContext(_dbConnectionConfig)) // { // context.Directories.AddRange(directories); // context.SaveChanges(); // } //} /// <summary> /// Get a directory from an absolute path. Always loads the parent /// </summary> /// <param name="path"></param> /// <param name="includeChildren"></param> /// <returns></returns> public BackedUpDirectory GetDirectory(string path, bool includeChildren = false) { var dirNames = path.Split(Path.AltDirectorySeparatorChar); BackedUpDirectory dir = null; using (var context = new AppDbContext(_dbConnectionConfig)) { int? parentId = null; bool saveRequired = false; foreach (var dirName in dirNames) { dir = context.Directories.FirstOrDefault(d => ((d.Name == dirName) && (d.ParentId == parentId))); if (dir == null) { dir = new BackedUpDirectory() { Name = dirName, ParentId = parentId }; context.Directories.Add(dir); var hist = new DirectoryHistory() { DirectoryId = dir.Id, LastSeen = DateTime.Now, Modified = dir.Modified }; context.DirectoryHistory.Add(hist); saveRequired = true; } parentId = dir.Id; } if (saveRequired) { context.SaveChanges(); } var history = context.DirectoryHistory.OrderByDescending(h => h.LastSeen) .FirstOrDefault(h => (h.DirectoryId == dir.Id)); dir.Modified = history.Modified; dir.Deleted = history.Deleted; if ((dir != null) && includeChildren) { context.Files.Where(f => f.ParentId == dir.Id).Load(); foreach (var file in dir.Files) { var hist = (from h in context.FileHistory orderby h.Modified descending where h.FileId == file.Id select h).First(); file.Modified = hist.Modified; } context.Directories.Include(d => d.History) .Where(d => d.ParentId == dir.Id).Load(); foreach (var subdir in dir.Directories) { var hist = (from h in context.DirectoryHistory orderby h.Modified descending where h.DirectoryId == subdir.Id select h).First(); subdir.Modified = hist.Modified; } } } return(dir); }