diff --git a/GBase/GBase.cs b/GBase/GBase.cs index b449665..56ad1c0 100644 --- a/GBase/GBase.cs +++ b/GBase/GBase.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using GBase.Api; using GBase.Attributes; using GBase.Factories; using GBase.Interfaces; @@ -96,7 +95,7 @@ namespace GBase return true; } - public IGBaseTable GetTable() where T : INotifyGBaseEntryChanged + public IGBaseTable GetTable() { if (Tables.OfType>().Any()) return Tables.OfType>().First(); @@ -118,7 +117,7 @@ namespace GBase return Tables.Remove(table); } - public async Task AddEntry(T entry, CancellationToken cancellationToken) where T : INotifyGBaseEntryChanged + public async Task AddEntry(T entry, CancellationToken cancellationToken) { IGBaseTable table = GetTable(); if (table == null) @@ -126,6 +125,34 @@ namespace GBase return await table.AddEntry(entry, cancellationToken); } + + public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) + { + IGBaseTable table = GetTable(); + if (table == null) + throw new Exception(); //TODO: Create exception + + await table.SetValue(entry, propertyName, value, cancellationToken); + } + + public async Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) + { + IGBaseTable table = GetTable(); + if (table == null) + throw new Exception(); //TODO: Create exception + + return await table.GetValue(entry, propertyName, cancellationToken); + } + + public async Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) + { + IGBaseTable table = GetTable(); + if (table == null) + throw new Exception(); //TODO: Create exception + + return await table.GetValues(entry, propertyName, cancellationToken); + } + /// /// Dispose used resources asynchronously diff --git a/GBase/GBaseTable.cs b/GBase/GBaseTable.cs index 1bddadd..644e0fe 100644 --- a/GBase/GBaseTable.cs +++ b/GBase/GBaseTable.cs @@ -13,6 +13,8 @@ using GBase.Attributes; using GBase.Factories; using GBase.FileHandling.Factories; using GBase.Interfaces; +using GBase.Interfaces.DataHandling; +using GBase.Interfaces.DataHandling.Pool; using GBase.Interfaces.FileHandling; namespace GBase @@ -20,17 +22,22 @@ namespace GBase /// /// A table /// - public class GBaseTable : IGBaseTable where T : INotifyGBaseEntryChanged + public class GBaseTable : IGBaseTable { private readonly IFileHandler _fileHandler; + private readonly IDataHandlerPool _dataHandlerPool; private readonly IGBaseColumnFactory _gBaseColumnFactory; /// /// A table /// - public GBaseTable(IFileHandlerFactory fileHandlerFactory, IGBaseColumnFactory gBaseColumnFactory) + /// + /// The + /// + public GBaseTable(IFileHandlerFactory fileHandlerFactory, IDataHandlerPool dataHandlerPool, IGBaseColumnFactory gBaseColumnFactory) { _fileHandler = fileHandlerFactory.Create(); + _dataHandlerPool = dataHandlerPool; _gBaseColumnFactory = gBaseColumnFactory; Columns = new List(); @@ -123,9 +130,10 @@ namespace GBase public async Task AddEntry(T entry, CancellationToken cancellationToken) { Entries.Add(entry); - entry.GBaseEntryChanged += OnGBaseEntryChanged; - await _fileHandler.AddEntry(entry, this, cancellationToken); + using IGBaseFile file = _fileHandler.CreateEntryFile(entry, this); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); + await dataHandler.AddEntry(entry, this, file.File, cancellationToken); return true; } @@ -140,38 +148,30 @@ namespace GBase if (!Entries.Contains(entry)) return false; - entry.GBaseEntryChanged -= OnGBaseEntryChanged; - - await _fileHandler.RemoveEntry(entry); + await _fileHandler.DeleteEntryFile(entry); return Entries.Remove(entry); } - /// - /// Modify the property of a given entry with the given value - /// - /// The entry - /// The name of the property - /// The new value to set - /// True if successful, false if not - private bool ModifyEntry(object entry, string propertyName, object value) //TODO: Write to file + public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) { - //don't need to change value of property in `Entries` list, the instance is saved there, any property change is already changed there as well - return true; + using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + await dataHandler.SetValue(file.File, propertyName, value, cancellationToken); } - /// - /// A entry changed - /// - /// The entry (sender) - /// The - private void OnGBaseEntryChanged(object entry, GBaseEntryChangedEventArgs args) + public async Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) { - //TODO: Might change, depending on #23 - bool success = ModifyEntry(entry, args.ColumnName, args.Value); - - if (!success) - throw new Exception("Failed to handle EntryChanged"); //TODO: Decide what to do here + using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + return await dataHandler.GetValue(file.File, propertyName, cancellationToken); + } + + public async Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) + { + using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + return await dataHandler.GetValues(file.File, propertyName, cancellationToken); } /// @@ -181,6 +181,7 @@ namespace GBase public async ValueTask DisposeAsync() { await _fileHandler.DisposeAsync(); + await _dataHandlerPool.DisposeAsync(); foreach (var column in Columns) { diff --git a/GBase/Interfaces/IGBase.cs b/GBase/Interfaces/IGBase.cs index b49e0be..74b571c 100644 --- a/GBase/Interfaces/IGBase.cs +++ b/GBase/Interfaces/IGBase.cs @@ -7,7 +7,6 @@ using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using GBase.Api; using GBase.Interfaces.Settings; namespace GBase.Interfaces @@ -49,7 +48,7 @@ namespace GBase.Interfaces /// True if successful, false if not bool AddTable(IGBaseTable table); - IGBaseTable GetTable() where T : INotifyGBaseEntryChanged; + IGBaseTable GetTable(); /// /// Removes a given from this @@ -58,6 +57,10 @@ namespace GBase.Interfaces /// True if successful, false if not bool RemoveTable(IGBaseTable table); - Task AddEntry(T entry, CancellationToken cancellationToken) where T : INotifyGBaseEntryChanged; + Task AddEntry(T entry, CancellationToken cancellationToken); + + Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken); + Task GetValue(T entry, string propertyName, CancellationToken cancellationToken); + Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/GBase/Interfaces/IGBaseTable.cs b/GBase/Interfaces/IGBaseTable.cs index ca18623..277c1c5 100644 --- a/GBase/Interfaces/IGBaseTable.cs +++ b/GBase/Interfaces/IGBaseTable.cs @@ -10,7 +10,7 @@ using GBase.Api; namespace GBase.Interfaces { - public interface IGBaseTable : IGBaseTable where T : INotifyGBaseEntryChanged + public interface IGBaseTable : IGBaseTable { /// /// The entries of this @@ -34,6 +34,12 @@ namespace GBase.Interfaces /// The entry implementing /// True if successful, false if not Task RemoveEntry(T entry); + + Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken); + + Task GetValue(T entry, string propertyName, CancellationToken cancellationToken); + + Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken); } ///