Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 10 additions & 20 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CrdtMiniLcmApi(
CrdtMorphTypeApi morphTypeApi,
CrdtPartsOfSpeechApi partsOfSpeechApi,
CrdtComplexFormTypesApi complexFormTypesApi,
CrdtPictureApi pictureApi,
CrdtMediaApi mediaApi,
CrdtCustomViewApi customViewApi,
CrdtCommentApi commentApi,
Expand Down Expand Up @@ -860,41 +861,34 @@ static SetFirstTranslationIdChange GetSetFirstTranslationIdChange(Guid exampleSe
}
}

#region PictureApi
public async Task<Picture> CreatePicture(Guid entryId,
Guid senseId,
Picture picture,
BetweenPosition? between = null)
{
await using var repo = await repoFactory.CreateRepoAsync();
var change = new CreateSensePictureChange(picture, senseId, between);
await harmonyChangeWriter.AddChange(change);
return await GetPicture(entryId, senseId, change.PictureId) ?? throw NotFoundException.ForType<Picture>(change.PictureId);
return await pictureApi.CreatePicture(entryId, senseId, picture, between);
}

public async Task<Picture?> GetPicture(Guid entryId, Guid senseId, Guid id)
{
await using var repo = await repoFactory.CreateRepoAsync();
var sense = await repo.GetSense(senseId);
return sense?.Pictures.FirstOrDefault(pic => pic.Id == id);
return await pictureApi.GetPicture(entryId, senseId, id);
}

public async Task SubmitUpdatePicture(Guid entryId,
Guid senseId,
Guid pictureId,
UpdateObjectInput<Picture> update)
{
var jsonPatch = update.Patch;
var patchChange = new UpdateSensePictureChange(pictureId, senseId, jsonPatch);
await harmonyChangeWriter.AddChange(patchChange);
await pictureApi.SubmitUpdatePicture(entryId, senseId, pictureId, update);
}

public async Task<Picture> UpdatePicture(Guid entryId,
Guid senseId,
Guid pictureId,
UpdateObjectInput<Picture> update)
{
await SubmitUpdatePicture(entryId, senseId, pictureId, update);
return await GetPicture(entryId, senseId, pictureId) ?? throw NotFoundException.ForType<Picture>(pictureId);
return await pictureApi.UpdatePicture(entryId, senseId, pictureId, update);
}

public async Task<Picture> UpdatePicture(Guid entryId,
Expand All @@ -903,23 +897,19 @@ public async Task<Picture> UpdatePicture(Guid entryId,
Picture after,
IMiniLcmApi? api = null)
{
await PictureSync.Sync(entryId, senseId, before, after, api ?? this);
return await GetPicture(entryId, senseId, after.Id) ?? throw NotFoundException.ForType<Picture>(after.Id);
return await pictureApi.UpdatePicture(entryId, senseId, before, after, api ?? this);
}

public async Task MovePicture(Guid entryId, Guid senseId, Guid pictureId, BetweenPosition between)
{
await using var repo = await repoFactory.CreateRepoAsync();
var sense = await repo.GetSense(senseId);
if (sense is null) throw NotFoundException.ForType<Sense>(senseId);
var order = OrderPicker.PickOrder(sense.Pictures, between);
await harmonyChangeWriter.AddChange(new ReorderSensePictureChange(pictureId, senseId, order));
await pictureApi.MovePicture(entryId, senseId, pictureId, between);
}

public async Task DeletePicture(Guid entryId, Guid senseId, Guid pictureId)
{
await harmonyChangeWriter.AddChange(new RemoveSensePictureChange(pictureId, senseId));
await pictureApi.DeletePicture(entryId, senseId, pictureId);
}
#endregion

#region MediaApi
public async Task<ReadFileResponse> GetFileStream(MediaUri mediaUri, bool downloadIfMissing = true)
Expand Down
1 change: 1 addition & 0 deletions backend/FwLite/LcmCrdt/LcmCrdtKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static IServiceCollection AddLcmCrdtClientCore(this IServiceCollection se
services.AddScoped<CrdtMorphTypeApi>();
services.AddScoped<CrdtPartsOfSpeechApi>();
services.AddScoped<CrdtComplexFormTypesApi>();
services.AddScoped<CrdtPictureApi>();
services.AddScoped<CrdtMediaApi>();
services.AddScoped<CrdtCustomViewApi>();
services.AddScoped<CrdtCommentApi>();
Expand Down
70 changes: 70 additions & 0 deletions backend/FwLite/LcmCrdt/MiniLcmImp/CrdtPictureApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using LcmCrdt.Changes;
using LcmCrdt.Data;
using LcmCrdt.Harmony;
using MiniLcm.Exceptions;
using MiniLcm.SyncHelpers;

namespace LcmCrdt.MiniLcmImp;

public class CrdtPictureApi(MiniLcmRepositoryFactory repoFactory, HarmonyChangeWriter harmonyChangeWriter)
{
public async Task<Picture> CreatePicture(Guid entryId,
Guid senseId,
Picture picture,
BetweenPosition? between = null)
{
var change = new CreateSensePictureChange(picture, senseId, between);
await harmonyChangeWriter.AddChange(change);
return await GetPicture(entryId, senseId, change.PictureId) ?? throw NotFoundException.ForType<Picture>(change.PictureId);
}

public async Task<Picture?> GetPicture(Guid entryId, Guid senseId, Guid id)
{
await using var repo = await repoFactory.CreateRepoAsync();
var sense = await repo.GetSense(senseId);
return sense?.Pictures.FirstOrDefault(pic => pic.Id == id);
}

public async Task SubmitUpdatePicture(Guid entryId,
Guid senseId,
Guid pictureId,
UpdateObjectInput<Picture> update)
{
var jsonPatch = update.Patch;
var patchChange = new UpdateSensePictureChange(pictureId, senseId, jsonPatch);
await harmonyChangeWriter.AddChange(patchChange);
}

public async Task<Picture> UpdatePicture(Guid entryId,
Guid senseId,
Guid pictureId,
UpdateObjectInput<Picture> update)
{
await SubmitUpdatePicture(entryId, senseId, pictureId, update);
return await GetPicture(entryId, senseId, pictureId) ?? throw NotFoundException.ForType<Picture>(pictureId);
}

public async Task<Picture> UpdatePicture(Guid entryId,
Guid senseId,
Picture before,
Picture after,
IMiniLcmApi api)
{
await PictureSync.Sync(entryId, senseId, before, after, api);
return await GetPicture(entryId, senseId, after.Id) ?? throw NotFoundException.ForType<Picture>(after.Id);
}

public async Task MovePicture(Guid entryId, Guid senseId, Guid pictureId, BetweenPosition between)
{
await using var repo = await repoFactory.CreateRepoAsync();
var sense = await repo.GetSense(senseId);
if (sense is null) throw NotFoundException.ForType<Sense>(senseId);
var order = OrderPicker.PickOrder(sense.Pictures, between);
await harmonyChangeWriter.AddChange(new ReorderSensePictureChange(pictureId, senseId, order));
}

public async Task DeletePicture(Guid entryId, Guid senseId, Guid pictureId)
{
await harmonyChangeWriter.AddChange(new RemoveSensePictureChange(pictureId, senseId));
}
}
Loading