using LiteDB; namespace StorageServer.IO; public class FileDatabase : IDatabase { private LiteDatabase liteDatabase; private const string tableName = "files"; public FileDatabase(string fileName) { this.liteDatabase = new LiteDatabase(new ConnectionString() { Filename = fileName, Connection = ConnectionType.Direct, }, new() { EnumAsInteger = true, }); } public void Dispose() { this.liteDatabase.Dispose(); } public long Count() { try { return this.liteDatabase.GetCollection(tableName).LongCount(); } catch (LiteException e) { throw new IOException(e.Message, e); } } public bool Delete(long id) { try { return this.liteDatabase.GetCollection(tableName).Delete(id); } catch (LiteException e) { throw new IOException(e.Message, e); } } public long Insert(T model) { try { return this.liteDatabase.GetCollection(tableName).Insert(model); } catch (LiteException e) { throw new IOException(e.Message, e); } } public T Select(long id) { try { return this.liteDatabase.GetCollection(tableName).FindById(id); } catch (LiteException e) { throw new IOException(e.Message, e); } } public IEnumerable SelectAll() { try { return this.liteDatabase.GetCollection(tableName).FindAll(); } catch (LiteException e) { throw new IOException(e.Message, e); } } public bool Update(T model) { try { return this.liteDatabase.GetCollection(tableName).Update(model); } catch (LiteException e) { throw new IOException(e.Message, e); } } }