From 07feac2f65c2a3d8414a1b5a18094a412c4a54c8 Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Wed, 29 Jan 2025 23:36:57 +0330 Subject: [PATCH] Basic DB service loose implementation (no DI) --- Interface/IDbService.cs | 13 +++++++++ Models/DbEntry.cs | 8 ++++++ Models/FileEntry.cs | 9 ++++++ Models/FolderEntry.cs | 6 ++++ Program.cs | 2 ++ Services/DbService.cs | 64 +++++++++++++++++++++++++++++++++++++++++ TBDel.csproj | 4 +++ 7 files changed, 106 insertions(+) create mode 100644 Interface/IDbService.cs create mode 100644 Models/DbEntry.cs diff --git a/Interface/IDbService.cs b/Interface/IDbService.cs new file mode 100644 index 0000000..ba5b84d --- /dev/null +++ b/Interface/IDbService.cs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..799fd25 --- /dev/null +++ b/Models/DbEntry.cs @@ -0,0 +1,8 @@ +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 e69de29..93431d6 100644 --- a/Models/FileEntry.cs +++ b/Models/FileEntry.cs @@ -0,0 +1,9 @@ +namespace TBDel.Models; + +public class FileEntry +{ + // File path + public string Path { get; set; } = string.Empty; + // Date added + public DateTime DateAdded { get; set; } +} \ No newline at end of file diff --git a/Models/FolderEntry.cs b/Models/FolderEntry.cs index e69de29..0e26a4e 100644 --- a/Models/FolderEntry.cs +++ b/Models/FolderEntry.cs @@ -0,0 +1,6 @@ +namespace TBDel.Models; + +// The same as FileEntry +public class FolderEntry : FileEntry +{ +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 8b13789..7598ec5 100644 --- a/Program.cs +++ b/Program.cs @@ -1 +1,3 @@ +using TBDel.Interface; +using TBDel.Services; diff --git a/Services/DbService.cs b/Services/DbService.cs index e69de29..c51bf27 100644 --- a/Services/DbService.cs +++ b/Services/DbService.cs @@ -0,0 +1,64 @@ +using JsonFlatFileDataStore; +using TBDel.Interface; +using TBDel.Models; + +namespace TBDel.Services; + +public class DbService : IDbService +{ + private readonly DataStore _store; + /*private readonly IDataCollection _fileCollection; + private readonly IDataCollection _folderCollection;*/ + + public DbService(string filePath) // Constructor takes the file path + { + /*_store = new DataStore(filePath); + _fileCollection = _store.GetCollection(); + _folderCollection = _store.GetCollection();*/ + } + + public async Task AddFileEntryAsync(FileEntry entry) + { + //await _fileCollection.InsertOneAsync(entry); + return false; + } + + public async Task AddFolderEntryAsync(FolderEntry entry) + { + //await _folderCollection.InsertOneAsync(entry); + return false; + } + + public async Task> GetFileEntriesAsync() + { + //return _fileCollection.AsQueryable().ToList(); + return new List(); + } + + public async Task> GetFolderEntriesAsync() + { + //return _folderCollection.AsQueryable().ToList(); + return new List(); + } + + + public async Task RemoveFileEntryAsync(string path) + { + //var entryToRemove = _fileCollection.AsQueryable().FirstOrDefault(e => e.Path == path); + /*if (entryToRemove != null) + { + 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) + { + 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 fd4bd08..c416f2b 100644 --- a/TBDel.csproj +++ b/TBDel.csproj @@ -7,4 +7,8 @@ enable + + + +