From f0339e5c0effb51f7116b91edeecf4e11b48dc64 Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Fri, 31 Jan 2025 21:42:43 +0330 Subject: [PATCH] Implemented "Add" command and finished Db stuff. --- Commands/AddCommand.cs | 63 +++++++++++++++++++++++++++++++++++++++++ Commands/HelpCommand.cs | 28 ++++++++++++++++++ Interface/IDbService.cs | 13 --------- Models/DbEntry.cs | 8 ------ Models/FileEntry.cs | 4 ++- Program.cs | 44 ++++++++++++++++++++++++++-- Services/DbService.cs | 48 +++++++++++++++---------------- TBDel.csproj | 1 + 8 files changed, 161 insertions(+), 48 deletions(-) create mode 100644 Commands/HelpCommand.cs delete mode 100644 Interface/IDbService.cs delete mode 100644 Models/DbEntry.cs diff --git a/Commands/AddCommand.cs b/Commands/AddCommand.cs index e69de29..4b354cf 100644 --- a/Commands/AddCommand.cs +++ b/Commands/AddCommand.cs @@ -0,0 +1,63 @@ +using Microsoft.Extensions.Logging; +using TBDel.Models; +using TBDel.Services; + +namespace TBDel.Commands; + +public class AddCommand +{ + + public static async Task AddEntry(string[] args) + { + // TODO: Add unique Id support + // TODO: Add duplicate path check + // TODO: Add support for multiple paths + + if (args.Length > 1) + { + string workingDirectory = Directory.GetCurrentDirectory(); + string absolutePath = Path.Combine(workingDirectory, args[1]); + if (File.Exists(absolutePath)) + { + Console.WriteLine($"Adding: {absolutePath}"); + var entry = new FileEntry { Path = absolutePath, DateAdded = DateTime.Now }; + var dbService = new DbService(); + if (await dbService.AddFileEntryAsync(entry)) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("File added successfully."); + Console.ResetColor(); + return true; + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Failed to add file."); + Console.ResetColor(); + return false; + } + } + else if (Directory.Exists(absolutePath)) + { + Console.WriteLine($"Adding: {absolutePath}"); + var entry = new FolderEntry() { Path = absolutePath, DateAdded = DateTime.Now }; + var dbService = new DbService(); + if (await dbService.AddFolderEntryAsync(entry)) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("Directory added successfully."); + Console.ResetColor(); + return true; + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Failed to add directory."); + Console.ResetColor(); + return false; + } + } + } + return false; + } +} \ No newline at end of file diff --git a/Commands/HelpCommand.cs b/Commands/HelpCommand.cs new file mode 100644 index 0000000..62b36bc --- /dev/null +++ b/Commands/HelpCommand.cs @@ -0,0 +1,28 @@ + +namespace TBDel.Commands; + +public static class HelpCommand +{ + // Show the help message + /// + /// Shows the help message. + /// + /// If true, displays an error message indicating that an invalid command was entered. + public static void Show( + Boolean showOnWrongCommand = false) + { + if (showOnWrongCommand) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Error.WriteLine("Invalid command. Use 'tbdel help' for help."); + Console.ResetColor(); + } + Console.WriteLine("Usage: tbdel [arguments]"); + Console.WriteLine("Available commands:"); + Console.WriteLine(" add Add a file or folder to the list"); + Console.WriteLine(" delete Deletes a file or folder from the list"); + Console.WriteLine(" deleteall Deletes all items in the list"); + Console.WriteLine(" list Lists all items in the list"); + Console.WriteLine(" help Shows this help message"); + } +} \ No newline at end of file diff --git a/Interface/IDbService.cs b/Interface/IDbService.cs deleted file mode 100644 index ba5b84d..0000000 --- a/Interface/IDbService.cs +++ /dev/null @@ -1,13 +0,0 @@ -using TBDel.Models; - -namespace TBDel.Interface; - -public interface IDbService -{ - Task AddFileEntryAsync(FileEntry entry); - Task AddFolderEntryAsync(FolderEntry entry); - Task> GetFileEntriesAsync(); - Task> GetFolderEntriesAsync(); - Task RemoveFileEntryAsync(string path); - Task RemoveFolderEntryAsync(string path); -} \ No newline at end of file diff --git a/Models/DbEntry.cs b/Models/DbEntry.cs deleted file mode 100644 index 799fd25..0000000 --- a/Models/DbEntry.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TBDel.Models; - -// The DB entry - A list of files and folders -public class DbEntry -{ - public List Files { get; set; } = new(); - public List Folders { get; set; } = new(); -} \ No newline at end of file diff --git a/Models/FileEntry.cs b/Models/FileEntry.cs index 93431d6..3ca7152 100644 --- a/Models/FileEntry.cs +++ b/Models/FileEntry.cs @@ -2,7 +2,9 @@ namespace TBDel.Models; public class FileEntry { - // File path + // Unique 5 digit number for each entry + uint Id { get; set; } + // Absolute path public string Path { get; set; } = string.Empty; // Date added public DateTime DateAdded { get; set; } diff --git a/Program.cs b/Program.cs index 7598ec5..008abb9 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,43 @@ -using TBDel.Interface; -using TBDel.Services; +using TBDel.Commands; +namespace TBDel +{ + internal class Program + { + // TODO: Add a command to show the Db path + static async Task Main(string[] args) + { + // Show the help message if no arguments are provided + if (args.Length == 0) + { + HelpCommand.Show(); + return; + } + + string command = args[0].ToLower(); + + switch (command) + { + case "add": + await AddCommand.AddEntry(args); + break; + case "delete": + + break; + case "deleteall": + + break; + case "list": + + break; + case "help": + HelpCommand.Show(); + break; + default: + HelpCommand.Show(true); + break; + } + } + + } +} \ No newline at end of file diff --git a/Services/DbService.cs b/Services/DbService.cs index c51bf27..9b46751 100644 --- a/Services/DbService.cs +++ b/Services/DbService.cs @@ -1,64 +1,64 @@ using JsonFlatFileDataStore; -using TBDel.Interface; using TBDel.Models; namespace TBDel.Services; -public class DbService : IDbService +public class DbService { private readonly DataStore _store; - /*private readonly IDataCollection _fileCollection; - private readonly IDataCollection _folderCollection;*/ + private readonly IDocumentCollection _fileCollection; + private readonly IDocumentCollection _folderCollection; - public DbService(string filePath) // Constructor takes the file path + public DbService() { - /*_store = new DataStore(filePath); + string dbPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TBDel_Db.json"); + var _store = new DataStore(dbPath,minifyJson:true); _fileCollection = _store.GetCollection(); - _folderCollection = _store.GetCollection();*/ + _folderCollection = _store.GetCollection(); } + public async Task AddFileEntryAsync(FileEntry entry) { - //await _fileCollection.InsertOneAsync(entry); - return false; + return await _fileCollection.InsertOneAsync(entry); } public async Task AddFolderEntryAsync(FolderEntry entry) { - //await _folderCollection.InsertOneAsync(entry); - return false; + return await _folderCollection.InsertOneAsync(entry); + } public async Task> GetFileEntriesAsync() { - //return _fileCollection.AsQueryable().ToList(); - return new List(); + return _fileCollection.AsQueryable().ToList(); } public async Task> GetFolderEntriesAsync() { - //return _folderCollection.AsQueryable().ToList(); - return new List(); + return _folderCollection.AsQueryable().ToList(); } - public async Task RemoveFileEntryAsync(string path) + public async Task RemoveFileEntryAsync(string path) { - //var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path); - /*if (entryToRemove != null) + var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path); + if (entryToRemove != null) { - await _fileCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path - }*/ + return await _fileCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path + } + return false; } public async Task RemoveFolderEntryAsync(string path) { - //var entryToRemove = _folderCollection.AsQueryable().FirstOrDefault(e => e.Path == path); - /*if (entryToRemove != null) + var entryToRemove = _folderCollection.AsQueryable().FirstOrDefault(e => e.Path == path); + if (entryToRemove != null) { - await _folderCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path - }*/ + return await _folderCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path + } return false; } } \ No newline at end of file diff --git a/TBDel.csproj b/TBDel.csproj index c416f2b..22d2269 100644 --- a/TBDel.csproj +++ b/TBDel.csproj @@ -9,6 +9,7 @@ +