From 83347587e4cc80d4ac6af7815c014c02bd2c5b37 Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Tue, 4 Feb 2025 13:28:06 +0330 Subject: [PATCH] Moved to System.Text.Json - Removed all Library dependencies - Implemented DeleteCommand --- Commands/AddCommand.cs | 1 - Commands/DeleteAllCommand.cs | 2 +- Commands/DeleteCommand.cs | 35 +++++++++ Models/FileEntry.cs | 17 ++++- Models/FolderEntry.cs | 10 +++ Models/JsonContext.cs | 15 ++++ Program.cs | 4 +- Services/DbService.cs | 141 ++++++++++++++++++++++------------- TBDel.csproj | 6 +- TUIHelper/Header.cs | 61 --------------- 10 files changed, 171 insertions(+), 121 deletions(-) create mode 100644 Models/JsonContext.cs delete mode 100644 TUIHelper/Header.cs diff --git a/Commands/AddCommand.cs b/Commands/AddCommand.cs index 4b354cf..38b501c 100644 --- a/Commands/AddCommand.cs +++ b/Commands/AddCommand.cs @@ -1,4 +1,3 @@ -using Microsoft.Extensions.Logging; using TBDel.Models; using TBDel.Services; diff --git a/Commands/DeleteAllCommand.cs b/Commands/DeleteAllCommand.cs index a531664..f2a863a 100644 --- a/Commands/DeleteAllCommand.cs +++ b/Commands/DeleteAllCommand.cs @@ -2,7 +2,7 @@ using TBDel.Services; namespace TBDel.Commands; -public class DeleteCommand +public class DeleteCAllCommand { public static async Task DeleteAll() { diff --git a/Commands/DeleteCommand.cs b/Commands/DeleteCommand.cs index e69de29..b8d000b 100644 --- a/Commands/DeleteCommand.cs +++ b/Commands/DeleteCommand.cs @@ -0,0 +1,35 @@ +using TBDel.Models; +using TBDel.Services; + +namespace TBDel.Commands; + + +public class DeleteCommand +{ + // Will be done by unique file Id + public static async Task DeleteEntry(string[] args) + { + var dbService = new DbService(); + if (args.Length > 1) + { + if (File.Exists(args[1])) + { + Console.WriteLine($"Deleteing file: {args[1]}"); + if (await dbService.RemoveFileEntryAsync(args[1])) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("File deleted successfully"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("something went wrong"); + Console.ResetColor(); + } + } + } + + + } +} \ No newline at end of file diff --git a/Models/FileEntry.cs b/Models/FileEntry.cs index 3ca7152..3aa0646 100644 --- a/Models/FileEntry.cs +++ b/Models/FileEntry.cs @@ -1,11 +1,26 @@ +using System.Text.Json.Serialization; + namespace TBDel.Models; public class FileEntry { // Unique 5 digit number for each entry - uint Id { get; set; } + public uint Id { get; set; } // Absolute path public string Path { get; set; } = string.Empty; // Date added public DateTime DateAdded { get; set; } + + public FileEntry() + { + } + + // To support trimmed binary + [JsonConstructor] + public FileEntry(uint id, string path, DateTime dateAdded) + { + Id = id; + Path = path; + DateAdded = dateAdded; + } } \ No newline at end of file diff --git a/Models/FolderEntry.cs b/Models/FolderEntry.cs index 0e26a4e..d30855e 100644 --- a/Models/FolderEntry.cs +++ b/Models/FolderEntry.cs @@ -1,6 +1,16 @@ +using System.Text.Json.Serialization; + namespace TBDel.Models; // The same as FileEntry public class FolderEntry : FileEntry { + public FolderEntry() : base() + { + } + + [JsonConstructor] + public FolderEntry(uint id, string path, DateTime dateAdded) : base(id, path, dateAdded) + { + } } \ No newline at end of file diff --git a/Models/JsonContext.cs b/Models/JsonContext.cs new file mode 100644 index 0000000..16c620b --- /dev/null +++ b/Models/JsonContext.cs @@ -0,0 +1,15 @@ +using System.Text.Json.Serialization; +using TBDel.Models; + + +// All this just because the binary size was 40MB for this little CLI tool +namespace TBDel.Services +{ + [JsonSourceGenerationOptions(WriteIndented = true)] + [JsonSerializable(typeof(DatabaseContent))] + [JsonSerializable(typeof(FileEntry))] + [JsonSerializable(typeof(FolderEntry))] + internal partial class JsonContext : JsonSerializerContext + { + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 6220d9d..2e43513 100644 --- a/Program.cs +++ b/Program.cs @@ -23,10 +23,10 @@ namespace TBDel await AddCommand.AddEntry(args); break; case "delete": - + await DeleteCommand.DeleteEntry(args); break; case "deleteall": - await DeleteCommand.DeleteAll(); + await DeleteCAllCommand.DeleteAll(); break; case "list": await ListCommand.List(args); diff --git a/Services/DbService.cs b/Services/DbService.cs index 9b46751..b91dba9 100644 --- a/Services/DbService.cs +++ b/Services/DbService.cs @@ -1,64 +1,105 @@ -using JsonFlatFileDataStore; +using System.Text.Json; using TBDel.Models; -namespace TBDel.Services; - -public class DbService +namespace TBDel.Services { - private readonly DataStore _store; - private readonly IDocumentCollection _fileCollection; - private readonly IDocumentCollection _folderCollection; - - public DbService() + public class DbService { - 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(); - } + private readonly string _dbPath; + private List _fileCollection; + private List _folderCollection; - - public async Task AddFileEntryAsync(FileEntry entry) - { - return await _fileCollection.InsertOneAsync(entry); - } - - public async Task AddFolderEntryAsync(FolderEntry entry) - { - return await _folderCollection.InsertOneAsync(entry); - - } - - public async Task> GetFileEntriesAsync() - { - return _fileCollection.AsQueryable().ToList(); - } - - public async Task> GetFolderEntriesAsync() - { - return _folderCollection.AsQueryable().ToList(); - } - - - public async Task RemoveFileEntryAsync(string path) - { - var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path); - if (entryToRemove != null) + public DbService() { - return await _fileCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path + _dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TBDel_Db.json"); + LoadData(); } - return false; + private void LoadData() + { + if (File.Exists(_dbPath)) + { + var json = File.ReadAllText(_dbPath); + var dbContent = JsonSerializer.Deserialize(json, JsonContext.Default.DatabaseContent) ?? new DatabaseContent(); + _fileCollection = dbContent.FileEntries; + _folderCollection = dbContent.FolderEntries; + } + else + { + _fileCollection = new List(); + _folderCollection = new List(); + } + } + + private void SaveData() + { + var dbContent = new DatabaseContent + { + FileEntries = _fileCollection, + FolderEntries = _folderCollection + }; + var json = JsonSerializer.Serialize(dbContent, JsonContext.Default.DatabaseContent); + File.WriteAllText(_dbPath, json); + } + + public async Task AddFileEntryAsync(FileEntry entry) + { + _fileCollection.Add(entry); + SaveData(); + return await Task.FromResult(true); + } + + public async Task AddFolderEntryAsync(FolderEntry entry) + { + _folderCollection.Add(entry); + SaveData(); + return await Task.FromResult(true); + } + + public async Task> GetFileEntriesAsync() + { + return await Task.FromResult(_fileCollection.ToList()); + } + + public async Task> GetFolderEntriesAsync() + { + return await Task.FromResult(_folderCollection.ToList()); + } + + public async Task RemoveFileEntryAsync(string path) + { + var entryToRemove = _fileCollection.FirstOrDefault(e => e.Path == path); + if (entryToRemove != null) + { + _fileCollection.Remove(entryToRemove); + SaveData(); + return await Task.FromResult(true); + } + return await Task.FromResult(false); + } + + public async Task RemoveFolderEntryAsync(string path) + { + var entryToRemove = _folderCollection.FirstOrDefault(e => e.Path == path); + if (entryToRemove != null) + { + _folderCollection.Remove(entryToRemove); + SaveData(); + return await Task.FromResult(true); + } + return await Task.FromResult(false); + } } - public async Task RemoveFolderEntryAsync(string path) + public class DatabaseContent { - var entryToRemove = _folderCollection.AsQueryable().FirstOrDefault(e => e.Path == path); - if (entryToRemove != null) + public DatabaseContent() { - return await _folderCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path + FileEntries = new List(); + FolderEntries = new List(); } - return false; + + public List FileEntries { get; set; } + public List FolderEntries { get; set; } } -} \ No newline at end of file +} diff --git a/TBDel.csproj b/TBDel.csproj index 22d2269..6e543da 100644 --- a/TBDel.csproj +++ b/TBDel.csproj @@ -5,11 +5,7 @@ net9.0 enable enable + true - - - - - diff --git a/TUIHelper/Header.cs b/TUIHelper/Header.cs deleted file mode 100644 index 3c45698..0000000 --- a/TUIHelper/Header.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace TBDel.TUIHelper; - -public class Header -{ - public static void Show(String _headerText) - { - // Get third of the terminal width - int viewWidth = Console.WindowWidth/3; - - // Show the header seperator - Console.Write("#"); - for (int i = 0; i <= viewWidth-2; i++) - { - Console.Write("="); - } - - // Move to the next line - Console.WriteLine("#"); - - Console.Write("#"); - - for (int i = 0; i <= viewWidth - 2;i++) - { - Console.Write(" "); - } - - Console.Write("#"); - Console.Write("\n#"); - - int textReletiveStartPosition = ((viewWidth - 2)- _headerText.Length)/2; - - for (int i = 0; i < textReletiveStartPosition; i++) - { - Console.Write(" "); - } - - Console.Write(_headerText); - - for (int i = 0; i <= textReletiveStartPosition; i++) - { - Console.Write(" "); - } - Console.WriteLine("#"); - Console.Write("#"); - - - for (int i = 0; i <= viewWidth - 2;i++) - { - Console.Write(" "); - } - - Console.Write("#"); - Console.Write("\n#"); - - for (int i = 0; i <= viewWidth-2; i++) - { - Console.Write("="); - } - Console.WriteLine("#"); - } -} \ No newline at end of file