Skip to content

Commit

Permalink
allow creating parts of speech as CRDTs and setup a PoC of pre seedin…
Browse files Browse the repository at this point in the history
…g them.
  • Loading branch information
hahn-kev committed Jun 13, 2024
1 parent fb2c62b commit e933d90
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
7 changes: 6 additions & 1 deletion backend/LcmCrdt.Tests/LexboxApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Crdt;
using Crdt.Db;
using LcmCrdt.Changes;
using LcmCrdt.Tests.Mocks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -330,6 +331,8 @@ public async Task UpdateSense()
[Fact]
public async Task UpdateSensePartOfSpeech()
{
var partOfSpeechId = Guid.NewGuid();
await DataModel.AddChange(Guid.NewGuid(), new CreatePartOfSpeechChange(partOfSpeechId, new MultiString() { { "en", "Adverb" } }));
var entry = await _api.CreateEntry(new Entry
{
LexemeForm = new MultiString
Expand Down Expand Up @@ -358,8 +361,10 @@ public async Task UpdateSensePartOfSpeech()
entry.Senses[0].Id,
_api.CreateUpdateBuilder<Sense>()
.Set(e => e.PartOfSpeech, "updated")
.Set(e => e.PartOfSpeechId, partOfSpeechId)
.Build());
updatedSense.PartOfSpeech.Should().Be("updated");
updatedSense.PartOfSpeechId.Should().Be(partOfSpeechId);
}

[Fact]
Expand Down Expand Up @@ -392,7 +397,7 @@ public async Task UpdateSenseSemanticDomain()
var updatedSense = await _api.UpdateSense(entry.Id,
entry.Senses[0].Id,
_api.CreateUpdateBuilder<Sense>()
.Set(e => e.SemanticDomains[0], new SemanticDomain() { Id = Guid.Empty, Code = "updated", Name = new MultiString() })
.Add(e => e.SemanticDomains, new SemanticDomain() { Id = Guid.Empty, Code = "updated", Name = new MultiString() })
.Build());
updatedSense.SemanticDomains.Select(sd => sd.Code).Should().Contain("updated");
}
Expand Down
19 changes: 19 additions & 0 deletions backend/LcmCrdt/Changes/CreatePartOfSpeechChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Crdt;
using Crdt.Changes;
using Crdt.Entities;
using MiniLcm;
using PartOfSpeech = LcmCrdt.Objects.PartOfSpeech;

namespace LcmCrdt.Changes;

public class CreatePartOfSpeechChange(Guid entityId, MultiString name, bool predefined = false)
: CreateChange<PartOfSpeech>(entityId), ISelfNamedType<CreatePartOfSpeechChange>
{
public MultiString Name { get; } = name;
public bool Predefined { get; } = predefined;

public override async ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
{
return new PartOfSpeech { Id = EntityId, Name = Name, Predefined = Predefined };
}
}
7 changes: 6 additions & 1 deletion backend/LcmCrdt/LcmCrdtKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PartOfSpeech = LcmCrdt.Objects.PartOfSpeech;

namespace LcmCrdt;

Expand Down Expand Up @@ -107,19 +108,23 @@ private static void ConfigureCrdt(CrdtConfig config)
.HasConversion(list => JsonSerializer.Serialize(list, (JsonSerializerOptions?)null),
json => JsonSerializer.Deserialize<string[]>(json, (JsonSerializerOptions?)null) ??
Array.Empty<string>());
});
}).Add<PartOfSpeech>();

config.ChangeTypeListBuilder.Add<JsonPatchChange<Entry>>()
.Add<JsonPatchChange<Sense>>()
.Add<JsonPatchChange<ExampleSentence>>()
.Add<JsonPatchChange<WritingSystem>>()
.Add<JsonPatchChange<PartOfSpeech>>()
.Add<DeleteChange<Entry>>()
.Add<DeleteChange<Sense>>()
.Add<DeleteChange<ExampleSentence>>()
.Add<DeleteChange<WritingSystem>>()
.Add<DeleteChange<PartOfSpeech>>()
.Add<SetPartOfSpeechChange>()
.Add<CreateEntryChange>()
.Add<CreateSenseChange>()
.Add<CreateExampleSentenceChange>()
.Add<CreatePartOfSpeechChange>()
.Add<CreateWritingSystemChange>();
}

Expand Down
46 changes: 46 additions & 0 deletions backend/LcmCrdt/Objects/PartOfSpeech.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Crdt;
using Crdt.Entities;
using LcmCrdt.Changes;
using MiniLcm;

namespace LcmCrdt.Objects;

public class PartOfSpeech : MiniLcm.PartOfSpeech, IObjectBase<PartOfSpeech>
{
Guid IObjectBase.Id
{
get => Id;
init => Id = value;
}
public DateTimeOffset? DeletedAt { get; set; }
public bool Predefined { get; set; }
public Guid[] GetReferences()
{
return [];
}

public void RemoveReference(Guid id, Commit commit)
{
}

public IObjectBase Copy()
{
return new PartOfSpeech
{
Id = Id,
Name = Name,
DeletedAt = DeletedAt,
Predefined = Predefined
};
}

public static async Task PredefinedPartsOfSpeech(DataModel dataModel, Guid clientId)
{
//todo load from xml instead of hardcoding
await dataModel.AddChanges(clientId,
[
new CreatePartOfSpeechChange(new Guid("46e4fe08-ffa0-4c8b-bf98-2c56f38904d9"), new MultiString() { { "en", "Adverb" } }, true)
],
new Guid("023faebb-711b-4d2f-b34f-a15621fc66bb"));
}
}
13 changes: 11 additions & 2 deletions backend/LcmCrdt/ProjectsService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Crdt.Db;
using Crdt;
using Crdt.Db;
using Microsoft.Extensions.DependencyInjection;
using MiniLcm;
using PartOfSpeech = LcmCrdt.Objects.PartOfSpeech;

namespace LcmCrdt;

Expand Down Expand Up @@ -37,8 +39,10 @@ public async Task<CrdtProject> CreateProject(string name,
var crdtProject = new CrdtProject(name, sqliteFile);
await using var serviceScope = CreateProjectScope(crdtProject);
var db = serviceScope.ServiceProvider.GetRequiredService<CrdtDbContext>();
await InitProjectDb(db, new ProjectData(name, id ?? Guid.NewGuid(), ProjectData.GetOriginDomain(domain), Guid.NewGuid()));
var projectData = new ProjectData(name, id ?? Guid.NewGuid(), ProjectData.GetOriginDomain(domain), Guid.NewGuid());
await InitProjectDb(db, projectData);
await serviceScope.ServiceProvider.GetRequiredService<CurrentProjectService>().PopulateProjectDataCache();
await SeedSystemData(serviceScope.ServiceProvider.GetRequiredService<DataModel>(), projectData.ClientId);
await (afterCreate?.Invoke(serviceScope.ServiceProvider, crdtProject) ?? Task.CompletedTask);
return crdtProject;
}
Expand All @@ -50,6 +54,11 @@ internal static async Task InitProjectDb(CrdtDbContext db, ProjectData data)
await db.SaveChangesAsync();
}

internal static async Task SeedSystemData(DataModel dataModel, Guid clientId)
{
await PartOfSpeech.PredefinedPartsOfSpeech(dataModel, clientId);
}

public AsyncServiceScope CreateProjectScope(CrdtProject crdtProject)
{
var serviceScope = provider.CreateAsyncScope();
Expand Down

0 comments on commit e933d90

Please sign in to comment.