// add a single folder to the database public static Response AddFolder(Folder folder, Boolean overwrite) { // add a folder to the database, or overwrites an existing folder // locate the video file if it exists Folder databaseFolder = GetFolder(folder.Id); Console.Write(databaseFolder); string[] rows = { "name", "parent_id" }; string[] data = { folder.Name, folder.ParentId.ToString() }; if (databaseFolder == null) { // insert a new file if none exists Boolean success = Database.SimpleInsertQuery(_table, rows, data); return (success) ? Response.Success : Response.FailedDatabase; } else if (overwrite) { // overwrite the old file if overwrite true Boolean success = Database.SimpleUpdateQuery(_table, "id", folder.Id, rows, data); return (success) ? Response.Success : Response.FailedDatabase; } else { return Response.FailedOverwrite; } }
public void Folder_EmptyFolderConstructor_Accept() { var folder1 = new Folder(1, "Folder"); //folder1 should have the parent 1 and the title "Title" Assert.True(folder1.ParentId == 1); Assert.True(folder1.Name == "Folder"); }
public void Folder_FolderWithFilesConstructor_Accept() { var folder1 = new Folder(1,0,"Folder",null); //folder1 should have parent 1, Id 0, title Folder and //contain an empty list of video files Assert.True(folder1.Id == 1); Assert.True(folder1.ParentId == 0); Assert.True(folder1.Name == "Folder"); Assert.True(folder1.Files == null); }
// Recursively traverses a directory mapping video file and folder locations private void PeruseDirectory(DirectoryInfo dir, Folder folder) { // Get all immediately contained subdirectories DirectoryInfo[] children = GetImmediateSubDirectories(dir); // For each subdirectory: for (int i = 0; i < children.Length; i++) { // Map its location and contents in the database Folder childFolder = MapDirectoryAndContents(children[i], folder.Id); if (childFolder != null) { // Continue the library traversal within PeruseDirectory(children[i], childFolder); } } }
// Adds a directory in the library structure as a folder object in the database private Folder MapDirectoryAndContents(DirectoryInfo dir, int parentId) { // Try to add this directory as a folder in the database Folder folder = new Folder(parentId, dir.Name); Response response = Folders.AddFolder(folder, true); // If the folder was added successfully: if (response == Response.Success) { // Get folder with updated info from DB (for id mostly) folder = GetAddedFolder(dir.Name, parentId) ?? folder; // Add all the contained video files to the database folder.Files = MapContainedVideoFiles(dir, folder.Id); // Return the successfully added folder return folder; } // Otherwise stop digging in this directory return null; }
// Set new library root reference to the given directory private static Folder SetRootReference(DirectoryInfo root) { // Make a folder object to use as the new root Folder rootFolder = new Folder(Database.ROOT_PARENT, root.FullName); // Replace the current folder structure with the childless new root Response response = Folders.DeleteAllFolders(rootFolder); Console.Write(response); // If setting the new root was successful: if (response == Response.Success) { // Wipe all existing files Files.DeleteAllFiles(); // Begin building the tree from the root folder return GetAddedFolder(root.FullName, Database.ROOT_PARENT); } // If this was unsuccessful, the process stops here. return null; }
// delete a file from the database by ID // returns true if successful, false otherwise public static bool DeleteFolder(Folder folder) { return Database.SimpleDeleteQuery(_table, "id", folder.Id); }
// clear the database of all folders and reset the hierarchy // setting a provided folder as root // WARNING: silently kills the folders DB! BEWARE! // does not kill file locations. to do that, call DeleteAllFiles() // should probably only be called along with DeleteAllFiles() for that reason public static Response DeleteAllFolders(Folder newRoot) { Database.TruncateTable(_table, true); return AddFolder(newRoot, false); }