-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow editing fwdata files directly without importing them
- Loading branch information
Showing
19 changed files
with
288 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using FwDataMiniLcmBridge.LcmUtils; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MiniLcm; | ||
|
||
namespace FwDataMiniLcmBridge; | ||
|
||
public static class FwDataBridgeKernel | ||
{ | ||
public const string FwDataApiKey = "FwDataApiKey"; | ||
public static IServiceCollection AddFwDataBridge(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<FwDataFactory>(); | ||
//todo since this is scoped it gets created on each request (or hub method call), which opens the project file on each request | ||
//this is not ideal since opening the project file can be slow. It should be done once per hub connection. | ||
services.AddKeyedScoped<ILexboxApi>(FwDataApiKey, (provider, o) => provider.GetRequiredService<FwDataFactory>().GetCurrentFwDataMiniLcmApi()); | ||
services.AddSingleton<FwDataProjectContext>(); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using FwDataMiniLcmBridge.Api; | ||
using FwDataMiniLcmBridge.LcmUtils; | ||
using Microsoft.Extensions.Logging; | ||
using MiniLcm; | ||
|
||
namespace FwDataMiniLcmBridge; | ||
|
||
public class FwDataFactory(FwDataProjectContext context, ILogger<FwDataMiniLcmApi> logger) | ||
{ | ||
public FwDataMiniLcmApi GetFwDataMiniLcmApi(string projectName, bool saveOnDispose) | ||
{ | ||
var project = FieldWorksProjectList.GetProject(projectName) ?? throw new InvalidOperationException($"Project {projectName} not found."); | ||
return GetFwDataMiniLcmApi(project, saveOnDispose); | ||
} | ||
public FwDataMiniLcmApi GetFwDataMiniLcmApi(FwDataProject project, bool saveOnDispose) | ||
{ | ||
var lcmCache = ProjectLoader.LoadCache(project.FileName); | ||
return new FwDataMiniLcmApi(lcmCache, saveOnDispose, logger); | ||
} | ||
|
||
public FwDataMiniLcmApi GetCurrentFwDataMiniLcmApi() | ||
{ | ||
var fwDataProject = context.Project; | ||
if (fwDataProject is null) | ||
{ | ||
throw new InvalidOperationException("No project is set in the context."); | ||
} | ||
return GetFwDataMiniLcmApi(fwDataProject, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MiniLcm; | ||
|
||
namespace FwDataMiniLcmBridge; | ||
|
||
public class FwDataProject(string name, string fileName) : IProjectIdentifier | ||
{ | ||
public string Name { get; } = name; | ||
public string FileName { get; } = fileName; | ||
public string Origin { get; } = "FieldWorks"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace FwDataMiniLcmBridge; | ||
|
||
public class FwDataProjectContext | ||
{ | ||
private sealed class ProjectHolder | ||
{ | ||
public FwDataProject? Project; | ||
} | ||
|
||
private static readonly AsyncLocal<ProjectHolder> _projectHolder = new(); | ||
|
||
public virtual FwDataProject? Project | ||
{ | ||
get => _projectHolder.Value?.Project; | ||
set | ||
{ | ||
var holder = _projectHolder.Value; | ||
if (holder != null) | ||
{ | ||
// Clear current Project trapped in the AsyncLocals, as its done. | ||
holder.Project = null; | ||
} | ||
|
||
if (value is not null) | ||
{ | ||
// Use an object indirection to hold the Project in the AsyncLocal, | ||
// so it can be cleared in all ExecutionContexts when its cleared above. | ||
_projectHolder.Value = new ProjectHolder { Project = value }; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
namespace LocalWebApp; | ||
using LocalWebApp.Hubs; | ||
|
||
namespace LocalWebApp; | ||
|
||
public static class HttpHelpers | ||
{ | ||
public static string? GetProjectName(this HttpContext? context) | ||
{ | ||
var name = context?.Request.RouteValues.GetValueOrDefault(LexboxApiHub.ProjectRouteKey, null)?.ToString(); | ||
var name = context?.Request.RouteValues.GetValueOrDefault(CrdtMiniLcmApiHub.ProjectRouteKey, null)?.ToString(); | ||
return string.IsNullOrWhiteSpace(name) ? null : name; | ||
} | ||
|
||
public static string? GetFwDataName(this HttpContext? context) | ||
{ | ||
var name = context?.Request.RouteValues.GetValueOrDefault(FwDataMiniLcmHub.ProjectRouteKey, null)?.ToString(); | ||
return string.IsNullOrWhiteSpace(name) ? null : name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using FwDataMiniLcmBridge; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Extensions.Options; | ||
using MiniLcm; | ||
using SystemTextJsonPatch; | ||
|
||
namespace LocalWebApp.Hubs; | ||
|
||
public class FwDataMiniLcmHub([FromKeyedServices(FwDataBridgeKernel.FwDataApiKey)] ILexboxApi lexboxApi) : Hub<ILexboxClient> | ||
{ | ||
public const string ProjectRouteKey = "fwdata"; | ||
public override async Task OnConnectedAsync() | ||
{ | ||
} | ||
|
||
public async Task<WritingSystems> GetWritingSystems() | ||
{ | ||
return await lexboxApi.GetWritingSystems(); | ||
} | ||
|
||
public async Task<WritingSystem> CreateWritingSystem(WritingSystemType type, WritingSystem writingSystem) | ||
{ | ||
var newWritingSystem = await lexboxApi.CreateWritingSystem(type, writingSystem); | ||
return newWritingSystem; | ||
} | ||
|
||
public async Task<WritingSystem> UpdateWritingSystem(WritingSystemId id, WritingSystemType type, JsonPatchDocument<WritingSystem> update) | ||
{ | ||
var writingSystem = await lexboxApi.UpdateWritingSystem(id, type, new JsonPatchUpdateInput<WritingSystem>(update)); | ||
return writingSystem; | ||
} | ||
|
||
public IAsyncEnumerable<Entry> GetEntriesForExemplar(string exemplar, QueryOptions? options = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public IAsyncEnumerable<Entry> GetEntries(QueryOptions? options = null) | ||
{ | ||
return lexboxApi.GetEntries(options); | ||
} | ||
|
||
public IAsyncEnumerable<Entry> SearchEntries(string query, QueryOptions? options = null) | ||
{ | ||
return lexboxApi.SearchEntries(query, options); | ||
} | ||
|
||
public async Task<Entry?> GetEntry(Guid id) | ||
{ | ||
return await lexboxApi.GetEntry(id); | ||
} | ||
|
||
public async Task<Entry> CreateEntry(Entry entry) | ||
{ | ||
var newEntry = await lexboxApi.CreateEntry(entry); | ||
await NotifyEntryUpdated(newEntry); | ||
return newEntry; | ||
} | ||
|
||
public async Task<Entry> UpdateEntry(Guid id, JsonPatchDocument<Entry> update) | ||
{ | ||
var entry = await lexboxApi.UpdateEntry(id, new JsonPatchUpdateInput<Entry>(update)); | ||
await NotifyEntryUpdated(entry); | ||
return entry; | ||
} | ||
|
||
public async Task DeleteEntry(Guid id) | ||
{ | ||
await lexboxApi.DeleteEntry(id); | ||
} | ||
|
||
public async Task<Sense> CreateSense(Guid entryId, Sense sense) | ||
{ | ||
var createdSense = await lexboxApi.CreateSense(entryId, sense); | ||
return createdSense; | ||
} | ||
|
||
public async Task<Sense> UpdateSense(Guid entryId, Guid senseId, JsonPatchDocument<Sense> update) | ||
{ | ||
var sense = await lexboxApi.UpdateSense(entryId, senseId, new JsonPatchUpdateInput<Sense>(update)); | ||
return sense; | ||
} | ||
|
||
public async Task DeleteSense(Guid entryId, Guid senseId) | ||
{ | ||
await lexboxApi.DeleteSense(entryId, senseId); | ||
} | ||
|
||
public async Task<ExampleSentence> CreateExampleSentence(Guid entryId, | ||
Guid senseId, | ||
ExampleSentence exampleSentence) | ||
{ | ||
var createdSentence = await lexboxApi.CreateExampleSentence(entryId, senseId, exampleSentence); | ||
return createdSentence; | ||
} | ||
|
||
public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId, | ||
Guid senseId, | ||
Guid exampleSentenceId, | ||
JsonPatchDocument<ExampleSentence> update) | ||
{ | ||
var sentence = await lexboxApi.UpdateExampleSentence(entryId, | ||
senseId, | ||
exampleSentenceId, | ||
new JsonPatchUpdateInput<ExampleSentence>(update)); | ||
return sentence; | ||
} | ||
|
||
public async Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId) | ||
{ | ||
await lexboxApi.DeleteExampleSentence(entryId, senseId, exampleSentenceId); | ||
} | ||
|
||
private async Task NotifyEntryUpdated(Entry entry) | ||
{ | ||
await Clients.Others.OnEntryUpdated(entry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.