Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/SIL.Harmony.Sample/Changes/NewWordChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class NewWordChange(Guid entityId, string text, string? note = null, Guid

public override async ValueTask<Word> NewEntity(Commit commit, IChangeContext context)
{
var antonymShouldBeNull = AntonymId is null || (await context.IsObjectDeleted(AntonymId.Value));
return (new Word { Text = Text, Note = Note, Id = EntityId, AntonymId = antonymShouldBeNull ? null : AntonymId });
var antonym = AntonymId is null ? null : await context.GetCurrent<Word>(AntonymId.Value);
antonym = antonym is { DeletedAt: null } ? antonym : null;
return new Word { Text = Text, Note = Note, Id = EntityId, Antonym = antonym, AntonymId = antonym?.Id };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@

namespace SIL.Harmony.Sample.Changes;

public class AddAntonymReferenceChange(Guid entityId, Guid antonymId)
: EditChange<Word>(entityId), ISelfNamedType<AddAntonymReferenceChange>
public class SetAntonymReferenceChange(Guid entityId, Guid antonymId, bool setObject = true)
: EditChange<Word>(entityId), ISelfNamedType<SetAntonymReferenceChange>
{
public Guid AntonymId { get; set; } = antonymId;
public bool SetObject { get; set; } = setObject;

public override async ValueTask ApplyChange(Word entity, IChangeContext context)
{
//if the word being referenced was deleted before this change was applied (could happen after a sync)
//then we don't want to apply the change
//if the change was already applied,
//then this reference is removed via Word.RemoveReference after the change which deletes the Antonym, see SnapshotWorker.MarkDeleted
if (!await context.IsObjectDeleted(AntonymId))
entity.AntonymId = AntonymId;
var antonym = await context.GetCurrent<Word>(AntonymId);
if (antonym is null or { DeletedAt: not null }) return;

entity.Antonym = SetObject ? antonym : null;
entity.AntonymId = AntonymId;
}
}
8 changes: 6 additions & 2 deletions src/SIL.Harmony.Sample/CrdtSampleKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi
.Add<EditExampleChange>()
.Add<SetWordTextChange>()
.Add<SetWordNoteChange>()
.Add<AddAntonymReferenceChange>()
.Add<SetAntonymReferenceChange>()
.Add<AddWordImageChange>()
.Add<SetOrderChange<Definition>>()
.Add<SetDefinitionPartOfSpeechChange>()
Expand All @@ -60,6 +60,10 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi
builder.HasMany(w => w.Tags)
.WithMany()
.UsingEntity<WordTag>();
builder.HasOne((w) => w.Antonym)
.WithMany()
.HasForeignKey(w => w.AntonymId)
.OnDelete(DeleteBehavior.SetNull);
})
.Add<Definition>(builder =>
{
Expand All @@ -81,4 +85,4 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi
});
return services;
}
}
}
12 changes: 9 additions & 3 deletions src/SIL.Harmony.Sample/Models/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Word : IObjectBase<Word>

public Guid Id { get; init; }
public DateTimeOffset? DeletedAt { get; set; }
public Word? Antonym { get; set; }
public Guid? AntonymId { get; set; }
public Guid? ImageResourceId { get; set; }
public List<Tag> Tags { get; set; } = new();
Expand All @@ -26,7 +27,11 @@ IEnumerable<Guid> Refs()

public void RemoveReference(Guid id, CommitBase commit)
{
if (AntonymId == id) AntonymId = null;
if (AntonymId == id)
{
AntonymId = null;
Antonym = null;
}
}

public IObjectBase Copy()
Expand All @@ -36,6 +41,7 @@ public IObjectBase Copy()
Id = Id,
Text = Text,
Note = Note,
Antonym = Antonym,
Comment thread
myieye marked this conversation as resolved.
AntonymId = AntonymId,
DeletedAt = DeletedAt,
ImageResourceId = ImageResourceId,
Expand All @@ -46,7 +52,7 @@ public IObjectBase Copy()
public override string ToString()
{
return
$"{nameof(Text)}: {Text}, {nameof(Id)}: {Id}, {nameof(Note)}: {Note}, {nameof(DeletedAt)}: {DeletedAt}, {nameof(AntonymId)}: {AntonymId}, {nameof(ImageResourceId)}: {ImageResourceId}" +
$"{nameof(Text)}: {Text}, {nameof(Id)}: {Id}, {nameof(Note)}: {Note}, {nameof(DeletedAt)}: {DeletedAt}, {nameof(Antonym)}: {Antonym}, {nameof(AntonymId)}: {AntonymId}, {nameof(ImageResourceId)}: {ImageResourceId}" +
$", {nameof(Tags)}: {string.Join(", ", Tags.Select(t => t.Text))}";
}
}
}
Loading
Loading