Moved to System.Text.Json - Removed all Library dependencies - Implemented DeleteCommand
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using TBDel.Models;
|
using TBDel.Models;
|
||||||
using TBDel.Services;
|
using TBDel.Services;
|
||||||
|
|
||||||
|
@@ -2,7 +2,7 @@ using TBDel.Services;
|
|||||||
|
|
||||||
namespace TBDel.Commands;
|
namespace TBDel.Commands;
|
||||||
|
|
||||||
public class DeleteCommand
|
public class DeleteCAllCommand
|
||||||
{
|
{
|
||||||
public static async Task DeleteAll()
|
public static async Task DeleteAll()
|
||||||
{
|
{
|
||||||
|
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,11 +1,26 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace TBDel.Models;
|
namespace TBDel.Models;
|
||||||
|
|
||||||
public class FileEntry
|
public class FileEntry
|
||||||
{
|
{
|
||||||
// Unique 5 digit number for each entry
|
// Unique 5 digit number for each entry
|
||||||
uint Id { get; set; }
|
public uint Id { get; set; }
|
||||||
// Absolute path
|
// Absolute path
|
||||||
public string Path { get; set; } = string.Empty;
|
public string Path { get; set; } = string.Empty;
|
||||||
// Date added
|
// Date added
|
||||||
public DateTime DateAdded { get; set; }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,16 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace TBDel.Models;
|
namespace TBDel.Models;
|
||||||
|
|
||||||
// The same as FileEntry
|
// The same as FileEntry
|
||||||
public class FolderEntry : 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
15
Models/JsonContext.cs
Normal 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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@@ -23,10 +23,10 @@ namespace TBDel
|
|||||||
await AddCommand.AddEntry(args);
|
await AddCommand.AddEntry(args);
|
||||||
break;
|
break;
|
||||||
case "delete":
|
case "delete":
|
||||||
|
await DeleteCommand.DeleteEntry(args);
|
||||||
break;
|
break;
|
||||||
case "deleteall":
|
case "deleteall":
|
||||||
await DeleteCommand.DeleteAll();
|
await DeleteCAllCommand.DeleteAll();
|
||||||
break;
|
break;
|
||||||
case "list":
|
case "list":
|
||||||
await ListCommand.List(args);
|
await ListCommand.List(args);
|
||||||
|
@@ -1,64 +1,105 @@
|
|||||||
using JsonFlatFileDataStore;
|
using System.Text.Json;
|
||||||
using TBDel.Models;
|
using TBDel.Models;
|
||||||
|
|
||||||
namespace TBDel.Services;
|
namespace TBDel.Services
|
||||||
|
|
||||||
public class DbService
|
|
||||||
{
|
{
|
||||||
private readonly DataStore _store;
|
public class DbService
|
||||||
private readonly IDocumentCollection<FileEntry> _fileCollection;
|
|
||||||
private readonly IDocumentCollection<FolderEntry> _folderCollection;
|
|
||||||
|
|
||||||
public DbService()
|
|
||||||
{
|
{
|
||||||
string dbPath =
|
private readonly string _dbPath;
|
||||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TBDel_Db.json");
|
private List<FileEntry> _fileCollection;
|
||||||
var _store = new DataStore(dbPath,minifyJson:true);
|
private List<FolderEntry> _folderCollection;
|
||||||
_fileCollection = _store.GetCollection<FileEntry>();
|
|
||||||
_folderCollection = _store.GetCollection<FolderEntry>();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public DbService()
|
||||||
public async Task<Boolean> AddFileEntryAsync(FileEntry entry)
|
|
||||||
{
|
|
||||||
return await _fileCollection.InsertOneAsync(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Boolean> AddFolderEntryAsync(FolderEntry entry)
|
|
||||||
{
|
|
||||||
return await _folderCollection.InsertOneAsync(entry);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<FileEntry>> GetFileEntriesAsync()
|
|
||||||
{
|
|
||||||
return _fileCollection.AsQueryable().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<FolderEntry>> GetFolderEntriesAsync()
|
|
||||||
{
|
|
||||||
return _folderCollection.AsQueryable().ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<Boolean> RemoveFileEntryAsync(string path)
|
|
||||||
{
|
|
||||||
var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path);
|
|
||||||
if (entryToRemove != null)
|
|
||||||
{
|
{
|
||||||
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<FileEntry>();
|
||||||
|
_folderCollection = new List<FolderEntry>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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<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 await Task.FromResult(_fileCollection.ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<FolderEntry>> GetFolderEntriesAsync()
|
||||||
|
{
|
||||||
|
return await Task.FromResult(_folderCollection.ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> 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<bool> 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<Boolean> RemoveFolderEntryAsync(string path)
|
public class DatabaseContent
|
||||||
{
|
{
|
||||||
var entryToRemove = _folderCollection.AsQueryable().FirstOrDefault(e => e.Path == path);
|
public DatabaseContent()
|
||||||
if (entryToRemove != null)
|
|
||||||
{
|
{
|
||||||
return await _folderCollection.DeleteOneAsync(e => e.Path == path); // or entryToRemove.Path
|
FileEntries = new List<FileEntry>();
|
||||||
|
FolderEntries = new List<FolderEntry>();
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
public List<FileEntry> FileEntries { get; set; }
|
||||||
|
public List<FolderEntry> FolderEntries { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -5,11 +5,7 @@
|
|||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<PublishAot>true</PublishAot>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="JsonFlatFileDataStore" Version="2.4.2" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -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("#");
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user