Moved to System.Text.Json - Removed all Library dependencies - Implemented DeleteCommand

This commit is contained in:
2025-02-04 13:28:06 +03:30
parent d716133230
commit 83347587e4
10 changed files with 171 additions and 121 deletions

View File

@@ -1,4 +1,3 @@
using Microsoft.Extensions.Logging;
using TBDel.Models;
using TBDel.Services;

View File

@@ -2,7 +2,7 @@ using TBDel.Services;
namespace TBDel.Commands;
public class DeleteCommand
public class DeleteCAllCommand
{
public static async Task DeleteAll()
{

View File

@@ -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();
}
}
}
}
}

View File

@@ -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;
}
}

View File

@@ -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)
{
}
}

15
Models/JsonContext.cs Normal file
View File

@@ -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
{
}
}

View File

@@ -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);

View File

@@ -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<FileEntry> _fileCollection;
private readonly IDocumentCollection<FolderEntry> _folderCollection;
public class DbService
{
private readonly string _dbPath;
private List<FileEntry> _fileCollection;
private List<FolderEntry> _folderCollection;
public DbService()
{
string dbPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TBDel_Db.json");
var _store = new DataStore(dbPath,minifyJson:true);
_fileCollection = _store.GetCollection<FileEntry>();
_folderCollection = _store.GetCollection<FolderEntry>();
_dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TBDel_Db.json");
LoadData();
}
public async Task<Boolean> AddFileEntryAsync(FileEntry entry)
private void LoadData()
{
return await _fileCollection.InsertOneAsync(entry);
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<FileEntry>();
_folderCollection = new List<FolderEntry>();
}
}
public async Task<Boolean> AddFolderEntryAsync(FolderEntry entry)
private void SaveData()
{
return await _folderCollection.InsertOneAsync(entry);
var dbContent = new DatabaseContent
{
FileEntries = _fileCollection,
FolderEntries = _folderCollection
};
var json = JsonSerializer.Serialize(dbContent, JsonContext.Default.DatabaseContent);
File.WriteAllText(_dbPath, json);
}
public async Task<bool> AddFileEntryAsync(FileEntry entry)
{
_fileCollection.Add(entry);
SaveData();
return await Task.FromResult(true);
}
public async Task<bool> AddFolderEntryAsync(FolderEntry entry)
{
_folderCollection.Add(entry);
SaveData();
return await Task.FromResult(true);
}
public async Task<List<FileEntry>> GetFileEntriesAsync()
{
return _fileCollection.AsQueryable().ToList();
return await Task.FromResult(_fileCollection.ToList());
}
public async Task<List<FolderEntry>> GetFolderEntriesAsync()
{
return _folderCollection.AsQueryable().ToList();
return await Task.FromResult(_folderCollection.ToList());
}
public async Task<Boolean> RemoveFileEntryAsync(string path)
public async Task<bool> RemoveFileEntryAsync(string path)
{
var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path);
var entryToRemove = _fileCollection.FirstOrDefault(e => e.Path == path);
if (entryToRemove != null)
{
return await _fileCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path
_fileCollection.Remove(entryToRemove);
SaveData();
return await Task.FromResult(true);
}
return await Task.FromResult(false);
}
return false;
}
public async Task<Boolean> RemoveFolderEntryAsync(string path)
public async Task<bool> RemoveFolderEntryAsync(string path)
{
var entryToRemove = _folderCollection.AsQueryable().FirstOrDefault(e => e.Path == path);
var entryToRemove = _folderCollection.FirstOrDefault(e => e.Path == path);
if (entryToRemove != null)
{
return await _folderCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path
_folderCollection.Remove(entryToRemove);
SaveData();
return await Task.FromResult(true);
}
return false;
return await Task.FromResult(false);
}
}
public class DatabaseContent
{
public DatabaseContent()
{
FileEntries = new List<FileEntry>();
FolderEntries = new List<FolderEntry>();
}
public List<FileEntry> FileEntries { get; set; }
public List<FolderEntry> FolderEntries { get; set; }
}
}

View File

@@ -5,11 +5,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonFlatFileDataStore" Version="2.4.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
</ItemGroup>
</Project>

View File

@@ -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("#");
}
}