add data storage

This commit is contained in:
gil zhang
2024-10-28 00:02:04 +08:00
parent a0faa528b6
commit 9cbc499b48
12 changed files with 250 additions and 0 deletions
+19
View File
@@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Verdure.Plugin.Copilot", "s
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{8EF77FC4-6C54-4F46-8957-E23E5E58E847}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Verdure.Braincase.DataStorage", "src\Verdure.Braincase.DataStorage\Verdure.Braincase.DataStorage.csproj", "{3ADA05B0-2834-475A-8635-9686EB158353}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -301,6 +303,22 @@ Global
{0958CE17-DFF9-4AF8-8B25-8D9B9708BA8F}.Release|x64.Build.0 = Release|Any CPU
{0958CE17-DFF9-4AF8-8B25-8D9B9708BA8F}.Release|x86.ActiveCfg = Release|Any CPU
{0958CE17-DFF9-4AF8-8B25-8D9B9708BA8F}.Release|x86.Build.0 = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|arm64.ActiveCfg = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|arm64.Build.0 = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|x64.ActiveCfg = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|x64.Build.0 = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|x86.ActiveCfg = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Debug|x86.Build.0 = Debug|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|Any CPU.Build.0 = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|arm64.ActiveCfg = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|arm64.Build.0 = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|x64.ActiveCfg = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|x64.Build.0 = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|x86.ActiveCfg = Release|Any CPU
{3ADA05B0-2834-475A-8635-9686EB158353}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -321,6 +339,7 @@ Global
{4A9E0A47-6386-45F2-84FF-3ACEC37B6A8E} = {B93FAB57-EB95-4516-9132-A584BA674458}
{6085A146-AAB7-4CC5-8A5D-17A9FD1D495B} = {8EF77FC4-6C54-4F46-8957-E23E5E58E847}
{0958CE17-DFF9-4AF8-8B25-8D9B9708BA8F} = {239CCEB2-7AA4-4250-BDE5-2120D3832DBF}
{3ADA05B0-2834-475A-8635-9686EB158353} = {8EF77FC4-6C54-4F46-8957-E23E5E58E847}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0EBF82F3-30A6-4BCC-950A-360C126FC118}
@@ -0,0 +1,12 @@
using Verdure.Braincase.Core.Models.Lingxi;
using Verdure.Braincase.Core.Models.Lingxi.Filters;
namespace Verdure.Braincase.Core.Contracts.Services;
public interface ILingxiSpaceService
{
Task<LingxiSpace> AddAsync(LingxiSpace space);
Task<bool> RemoveAsync(string id);
Task<LingxiSpace> UpdateAsync(LingxiSpace space);
Task<LingxiSpace> GetAsync(string id);
Task<List<LingxiSpace>> GetAllAsync(LingxiSpaceFilter filter);
}
@@ -0,0 +1,9 @@
namespace Verdure.Braincase.Core.Models.Lingxi.Filters;
public class LingxiSpaceFilter
{
public string ConversationId
{
get; set;
}
}
@@ -0,0 +1,36 @@
using System.Text.Json;
namespace Verdure.Braincase.Core.Models.Lingxi;
public class LingxiSpace
{
public string Id
{
get; set;
}
public string Name
{
get; set;
}
public string Desc
{
get; set;
}
public string Type
{
get; set;
}
public JsonDocument Content
{
get; set;
}
public DateTime CreatedTime
{
get; set;
}
public string ConversationId
{
get; set;
}
}
@@ -282,6 +282,7 @@
<ProjectReference Include="..\HelloWordKeyboard.DotNet\HelloWordKeyboard.DotNet.csproj" />
<ProjectReference Include="..\Mediapipe.Net\Mediapipe.Net.csproj" />
<ProjectReference Include="..\Verdure.Braincase.Controls\Verdure.Braincase.Controls.csproj" />
<ProjectReference Include="..\Verdure.Braincase.DataStorage\Verdure.Braincase.DataStorage.csproj" />
<ProjectReference Include="..\Verdure.IoT.Net\Verdure.IoT.Net.csproj" />
<ProjectReference Include="..\Verdure.NotificationArea\Verdure.NotificationArea.csproj" />
<ProjectReference Include="..\Verdure.WinUI.Common\Verdure.WinUI.Common.csproj" />
@@ -0,0 +1,12 @@
namespace Verdure.Braincase.DataStorage;
public class BraincaseDatabaseSettings
{
public string BraincaseLiteDB
{
get; set;
} = string.Empty;
public string TablePrefix
{
get; set;
} = string.Empty;
}
@@ -0,0 +1,48 @@
using System.Globalization;
using LiteDB;
using Verdure.Braincase.DataStorage.Collections;
namespace Verdure.Braincase.DataStorage;
public class BraincaseLiteDBContext : IDisposable
{
private readonly LiteDatabase _liteDBClient;
private readonly string _collectionPrefix;
private const string DB_NAME_INDEX = "authSource";
public BraincaseLiteDBContext(BraincaseDatabaseSettings dbSettings)
{
var mapper = new BsonMapper();
mapper.RegisterType(
value => value.ToString("o", CultureInfo.InvariantCulture),
bson => DateTime.ParseExact
(bson, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind));
mapper.RegisterType(
value => value.ToString("o", CultureInfo.InvariantCulture),
bson => DateTimeOffset.ParseExact
(bson, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind));
var dbConnectionString = dbSettings.BraincaseLiteDB;
var connectionString = new ConnectionString(dbConnectionString)
{
Connection = ConnectionType.Shared
};
_liteDBClient = new LiteDatabase(connectionString, mapper);
_collectionPrefix = dbSettings.TablePrefix ?? "Braincase";
}
private LiteDatabase Database => _liteDBClient;
public void Dispose()
{
_liteDBClient?.Dispose();
}
public ILiteCollection<LingxiSpaceDocument> LingxiSpaces
=> Database.GetCollection<LingxiSpaceDocument>($"{_collectionPrefix}_LingxiSpaces");
}
@@ -0,0 +1,35 @@
using System.Text.Json;
namespace Verdure.Braincase.DataStorage.Collections;
public class LingxiSpaceDocument : LiteDBBase
{
public string Name
{
get; set;
} = string.Empty;
public string Desc
{
get; set;
} = string.Empty;
public string Type
{
get; set;
} = string.Empty;
public JsonDocument? Content
{
get; set;
}
public DateTime CreatedTime
{
get; set;
}
public string ConversationId
{
get; set;
} = string.Empty;
}
@@ -0,0 +1,31 @@
using LiteDB;
namespace Verdure.Braincase.DataStorage;
public abstract class LiteDBBase
{
private string? id;
public string Id
{
get
{
if (string.IsNullOrEmpty(id))
{
id = GenerateId();
}
return id;
}
set => id = value;
}
private string GenerateId()
{
// Generate and return the id here
// You can use any logic to generate the id
// For example, you can use Guid.NewGuid().ToString() to generate a unique id
return Guid.NewGuid().ToString();
}
}
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verdure.Braincase.Core.Contracts.Services;
using Verdure.Braincase.Core.Models.Lingxi;
using Verdure.Braincase.Core.Models.Lingxi.Filters;
namespace Verdure.Braincase.DataStorage.Services;
public class LiteDBLingxiSpaceService : ILingxiSpaceService
{
public Task<LingxiSpace> AddAsync(LingxiSpace space) => throw new NotImplementedException();
public Task<List<LingxiSpace>> GetAllAsync(LingxiSpaceFilter filter) => throw new NotImplementedException();
public Task<LingxiSpace> GetAsync(string id) => throw new NotImplementedException();
public Task<bool> RemoveAsync(string id) => throw new NotImplementedException();
public Task<LingxiSpace> UpdateAsync(LingxiSpace space) => throw new NotImplementedException();
}
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectronBot.Braincase.Contracts.Services;
namespace Verdure.Braincase.DataStorage.Services;
public class LiteDBLocalSettingsService : ILocalSettingsService
{
public Task<T?> ReadSettingAsync<T>(string key) => throw new NotImplementedException();
public Task SaveSettingAsync<T>(string key, T value) => throw new NotImplementedException();
}
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.21" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ElectronBot.Braincase.Core\Verdure.Braincase.Core.csproj" />
</ItemGroup>
</Project>