Skip to content

Commit

Permalink
migrate LcmCrdt to use new Create and Edit Change classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed May 24, 2024
1 parent 5074b9a commit 1deb7d4
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 65 deletions.
17 changes: 4 additions & 13 deletions backend/LcmCrdt/Changes/CreateEntryChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace LcmCrdt.Changes;


public class CreateEntryChange : Change<Entry>, ISelfNamedType<CreateEntryChange>
public class CreateEntryChange : CreateChange<Entry>, ISelfNamedType<CreateEntryChange>
{
public CreateEntryChange(MiniLcm.Entry entry) : base(entry.Id == Guid.Empty ? Guid.NewGuid() : entry.Id)
{
Expand All @@ -31,24 +31,15 @@ private CreateEntryChange(Guid entityId) : base(entityId)

public MultiString? Note { get; set; }

public override IObjectBase NewEntity(Commit commit)
public override ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
{
return new Entry
return new(new Entry
{
Id = EntityId,
LexemeForm = LexemeForm ?? new MultiString(),
CitationForm = CitationForm ?? new MultiString(),
LiteralMeaning = LiteralMeaning ?? new MultiString(),
Note = Note ?? new MultiString()
};
}

public override ValueTask ApplyChange(Entry entity, ChangeContext context)
{
if (LexemeForm is not null) entity.LexemeForm = LexemeForm;
if (CitationForm is not null) entity.CitationForm = CitationForm;
if (LiteralMeaning is not null) entity.LiteralMeaning = LiteralMeaning;
if (Note is not null) entity.Note = Note;
return ValueTask.CompletedTask;
});
}
}
18 changes: 4 additions & 14 deletions backend/LcmCrdt/Changes/CreateExampleSentenceChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace LcmCrdt.Changes;

public class CreateExampleSentenceChange: Change<ExampleSentence>, ISelfNamedType<CreateExampleSentenceChange>
public class CreateExampleSentenceChange: CreateChange<ExampleSentence>, ISelfNamedType<CreateExampleSentenceChange>
{
public CreateExampleSentenceChange(MiniLcm.ExampleSentence exampleSentence, Guid senseId)
: base(exampleSentence.Id == Guid.Empty ? Guid.NewGuid() : exampleSentence.Id)
Expand All @@ -30,26 +30,16 @@ private CreateExampleSentenceChange(Guid entityId, Guid senseId) : base(entityId
public MultiString? Translation { get; set; }
public string? Reference { get; set; }

public override IObjectBase NewEntity(Commit commit)
public override async ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
{
return new ExampleSentence
{
Id = EntityId,
SenseId = SenseId,
Sentence = Sentence ?? new MultiString(),
Translation = Translation ?? new MultiString(),
Reference = Reference
Reference = Reference,
DeletedAt = await context.IsObjectDeleted(SenseId) ? commit.DateTime : (DateTime?)null
};
}

public override async ValueTask ApplyChange(ExampleSentence entity, ChangeContext context)
{
if (Sentence is not null) entity.Sentence = Sentence;
if (Translation is not null) entity.Translation = Translation;
if (Reference is not null) entity.Reference = Reference;
if (await context.IsObjectDeleted(SenseId))
{
entity.DeletedAt = context.Commit.DateTime;
}
}
}
19 changes: 4 additions & 15 deletions backend/LcmCrdt/Changes/CreateSenseChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace LcmCrdt.Changes;

public class CreateSenseChange: Change<Sense>, ISelfNamedType<CreateSenseChange>
public class CreateSenseChange: CreateChange<Sense>, ISelfNamedType<CreateSenseChange>
{
public CreateSenseChange(MiniLcm.Sense sense, Guid entryId) : base(sense.Id == Guid.Empty ? Guid.NewGuid() : sense.Id)
{
Expand All @@ -31,7 +31,7 @@ private CreateSenseChange(Guid entityId, Guid entryId) : base(entityId)
public string? PartOfSpeech { get; set; }
public IList<string>? SemanticDomain { get; set; }

public override IObjectBase NewEntity(Commit commit)
public override async ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
{
return new Sense
{
Expand All @@ -40,19 +40,8 @@ public override IObjectBase NewEntity(Commit commit)
Definition = Definition ?? new MultiString(),
Gloss = Gloss ?? new MultiString(),
PartOfSpeech = PartOfSpeech ?? string.Empty,
SemanticDomain = SemanticDomain ?? []
SemanticDomain = SemanticDomain ?? [],
DeletedAt = await context.IsObjectDeleted(EntryId) ? commit.DateTime : (DateTime?)null
};
}

public override async ValueTask ApplyChange(Sense entity, ChangeContext context)
{
if (Definition is not null) entity.Definition = Definition;
if (Gloss is not null) entity.Gloss = Gloss;
if (PartOfSpeech is not null) entity.PartOfSpeech = PartOfSpeech;
if (SemanticDomain is not null) entity.SemanticDomain = SemanticDomain;
if (await context.IsObjectDeleted(EntryId))
{
entity.DeletedAt = context.Commit.DateTime;
}
}
}
19 changes: 4 additions & 15 deletions backend/LcmCrdt/Changes/CreateWritingSystemChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace LcmCrdt.Changes;

public class CreateWritingSystemChange : Change<WritingSystem>, ISelfNamedType<CreateWritingSystemChange>
public class CreateWritingSystemChange : CreateChange<WritingSystem>, ISelfNamedType<CreateWritingSystemChange>
{
public required WritingSystemId WsId { get; init; }
public required string Name { get; init; }
Expand Down Expand Up @@ -36,9 +36,9 @@ private CreateWritingSystemChange(Guid entityId) : base(entityId)
{
}

public override IObjectBase NewEntity(Commit commit)
public override ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
{
return new WritingSystem(EntityId)
return new(new WritingSystem(EntityId)
{
WsId = WsId,
Name = Name,
Expand All @@ -47,17 +47,6 @@ public override IObjectBase NewEntity(Commit commit)
Exemplars = Exemplars,
Type = Type,
Order = Order
};
}

public override ValueTask ApplyChange(WritingSystem entity, ChangeContext context)
{
entity.Name = Name;
entity.Abbreviation = Abbreviation;
entity.Font = Font;
entity.Exemplars = Exemplars;
entity.Type = Type;
entity.Order = Order;
return ValueTask.CompletedTask;
});
}
}
7 changes: 1 addition & 6 deletions backend/LcmCrdt/Changes/JsonPatchChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace LcmCrdt.Changes;

public class JsonPatchChange<T> : Change<T>, IPolyType where T : class, IPolyType, IObjectBase
public class JsonPatchChange<T> : EditChange<T>, IPolyType where T : class, IPolyType, IObjectBase
{
public static string TypeName => "jsonPatch:" + T.TypeName;
public JsonPatchChange(Guid entityId, Action<JsonPatchDocument<T>> action) : base(entityId)
Expand All @@ -31,11 +31,6 @@ public JsonPatchChange(Guid entityId, IJsonPatchDocument patchDocument, JsonSeri

public JsonPatchDocument<T> PatchDocument { get; }

public override IObjectBase NewEntity(Commit commit)
{
throw new Exception("Cannot create new entity from patch");
}

public override ValueTask ApplyChange(T entity, ChangeContext context)
{
PatchDocument.ApplyTo(entity);
Expand Down
2 changes: 1 addition & 1 deletion backend/LcmCrdt/ProjectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private sealed class ProjectHolder

private static readonly AsyncLocal<ProjectHolder> _projectHolder = new();

public CrdtProject? Project
public virtual CrdtProject? Project
{
get => _projectHolder.Value?.Project;
set
Expand Down
2 changes: 1 addition & 1 deletion backend/harmony

0 comments on commit 1deb7d4

Please sign in to comment.