Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Damien's unresolved comments #562

Merged
merged 2 commits into from
Dec 5, 2024
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
32 changes: 16 additions & 16 deletions src/Serval/src/Serval.Client/Client.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7073,15 +7073,28 @@ public partial class Corpus
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class CorpusFile
{
[Newtonsoft.Json.JsonProperty("fileId", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string FileId { get; set; } = default!;
[Newtonsoft.Json.JsonProperty("file", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public ResourceLink File { get; set; } = new ResourceLink();

[Newtonsoft.Json.JsonProperty("textId", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string? TextId { get; set; } = default!;

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ResourceLink
{
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Id { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("url", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Url { get; set; } = default!;

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class CorpusConfig
{
Expand Down Expand Up @@ -7408,19 +7421,6 @@ public partial class TranslationCorpus

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ResourceLink
{
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Id { get; set; } = default!;

[Newtonsoft.Json.JsonProperty("url", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Url { get; set; } = default!;

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class TranslationCorpusFile
{
Expand Down
15 changes: 10 additions & 5 deletions src/Serval/src/Serval.DataFiles/Consumers/GetCorpusConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ public async Task Consume(ConsumeContext<GetCorpus> context)
context.Message.Owner,
context.CancellationToken
);
IEnumerable<string> corpusFileIds = corpus.Files.Select(f => f.FileRef);
IDictionary<string, DataFile> corpusDataFilesDict = (
await _dataFileService.GetAllAsync(corpusFileIds, context.CancellationToken)
).ToDictionary(f => f.Id);

await context.RespondAsync(
new CorpusResult
{
CorpusId = corpus.Id,
Name = corpus.Name,
Language = corpus.Language,
Files = await Task.WhenAll(
corpus.Files.Select(async f => new CorpusFileResult
Files = corpus
.Files.Select(f => new CorpusFileResult
{
TextId = f.TextId!,
File = Map(await _dataFileService.GetAsync(f.FileId))
TextId = f.TextId ?? corpusDataFilesDict[f.FileRef].Name,
File = Map(corpusDataFilesDict[f.FileRef])
})
)
.ToList()
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serval/src/Serval.DataFiles/Contracts/CorpusFileDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Serval.DataFiles.Contracts;

public record CorpusFileDto
{
public required string FileId { get; init; }
public required ResourceLinkDto File { get; init; }
public string? TextId { get; init; }
}
12 changes: 10 additions & 2 deletions src/Serval/src/Serval.DataFiles/Controllers/CorporaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ CancellationToken cancellationToken
DataFile? dataFile = await _dataFileService.GetAsync(file.FileId, cancellationToken);
if (dataFile == null)
throw new InvalidOperationException($"DataFile with id {file.FileId} does not exist.");
dataFiles.Add(new CorpusFile { FileId = file.FileId, TextId = file.TextId });
dataFiles.Add(new CorpusFile { FileRef = file.FileId, TextId = file.TextId });
}
return dataFiles;
}
Expand All @@ -197,6 +197,14 @@ private CorpusDto Map(Corpus source)

private CorpusFileDto Map(CorpusFile source)
{
return new CorpusFileDto { FileId = source.FileId, TextId = source.TextId };
return new CorpusFileDto
{
File = new ResourceLinkDto
{
Id = source.FileRef,
Url = _urlService.GetUrl(Endpoints.GetDataFile, new { id = source.FileRef })
},
TextId = source.TextId
};
}
}
2 changes: 1 addition & 1 deletion src/Serval/src/Serval.DataFiles/Models/CorpusFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Serval.DataFiles.Models;

public record CorpusFile
{
public required string FileId { get; init; }
public required string FileRef { get; init; }
public string? TextId { get; init; }
}
19 changes: 11 additions & 8 deletions src/Serval/src/Serval.DataFiles/Services/CorpusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ namespace Serval.DataFiles.Services;

public class CorpusService(
IRepository<Corpus> corpora,
IRepository<DataFile> dataFiles,
IDataAccessContext dataAccessContext,
IDataFileService dataFileService,
IScopedMediator mediator
) : OwnedEntityServiceBase<Corpus>(corpora), ICorpusService
{
private readonly IDataAccessContext _dataAccessContext = dataAccessContext;
private readonly IDataFileService _dataFileService = dataFileService;

private readonly IRepository<DataFile> _dataFiles = dataFiles;
private readonly IScopedMediator _mediator = mediator;

public async Task<Corpus> GetAsync(string id, string owner, CancellationToken cancellationToken = default)
Expand All @@ -36,17 +35,21 @@ public async Task<Corpus> UpdateAsync(
);
if (corpus is null)
throw new EntityNotFoundException($"Could not find Corpus '{id}.");
HashSet<string> corpusFileIds = corpus.Files.Select(f => f.FileRef).ToHashSet();
IDictionary<string, DataFile> corpusDataFilesDict = (
await _dataFiles.GetAllAsync(f => corpusFileIds.Contains(f.Id), ct)
).ToDictionary(f => f.Id);
await _mediator.Publish(
new CorpusUpdated
{
CorpusId = corpus.Id,
Files = await Task.WhenAll(
corpus.Files.Select(async f => new CorpusFileResult
Files = corpus
.Files.Select(f => new CorpusFileResult
{
TextId = f.TextId!,
File = Map(await _dataFileService.GetAsync(f.FileId))
TextId = f.TextId ?? corpusDataFilesDict[f.FileRef].Name,
File = Map(corpusDataFilesDict[f.FileRef])
})
)
.ToList()
},
ct
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public interface IDataFileService
{
Task<IEnumerable<DataFile>> GetAllAsync(string owner, CancellationToken cancellationToken = default);
Task<IEnumerable<DataFile>> GetAllAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default);
Task<DataFile> GetAsync(string id, CancellationToken cancellationToken = default);
Task<DataFile> GetAsync(string id, string owner, CancellationToken cancellationToken = default);
Task CreateAsync(DataFile dataFile, Stream stream, CancellationToken cancellationToken = default);
Expand Down
9 changes: 9 additions & 0 deletions src/Serval/src/Serval.Shared/Services/EntityServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public virtual async Task<T> GetAsync(string id, CancellationToken cancellationT
return entity;
}

public virtual async Task<IEnumerable<T>> GetAllAsync(
IEnumerable<string> ids,
CancellationToken cancellationToken = default
)
{
HashSet<string> idSet = ids.ToHashSet();
return await Entities.GetAllAsync(e => idSet.Contains(e.Id), cancellationToken);
}

public virtual async Task<T> CreateAsync(T entity, CancellationToken cancellationToken = default)
{
await Entities.InsertAsync(entity, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static CorpusFile Map(CorpusFileResult corpusFile)
return new CorpusFile
{
Id = corpusFile.File.DataFileId,
TextId = corpusFile.TextId,
TextId = corpusFile.TextId ?? corpusFile.File.Name,
Filename = corpusFile.File.Filename,
Format = corpusFile.File.Format,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ CancellationToken cancellationToken
Id = f.File.DataFileId,
Filename = f.File.Filename,
Format = f.File.Format,
TextId = f.TextId
TextId = f.TextId ?? f.File.Name
})
.ToList(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,21 @@ public async Task SetUp()
Id = SOURCE_CORPUS_ID_1,
Language = "en",
Owner = "client1",
Files = [new() { FileId = srcFile.Id, TextId = "all" }]
Files = [new() { FileRef = srcFile.Id, TextId = "all" }]
};
var srcCorpus2 = new DataFiles.Models.Corpus
{
Id = SOURCE_CORPUS_ID_2,
Language = "en",
Owner = "client1",
Files = [new() { FileId = srcFile.Id, TextId = "all" }]
Files = [new() { FileRef = srcFile.Id, TextId = "all" }]
};
var trgCorpus = new DataFiles.Models.Corpus
{
Id = TARGET_CORPUS_ID,
Language = "en",
Owner = "client1",
Files = [new() { FileId = trgFile.Id, TextId = "all" }]
Files = [new() { FileRef = trgFile.Id, TextId = "all" }]
};
await _env.Corpora.InsertAllAsync([srcCorpus, srcCorpus2, trgCorpus]);
}
Expand Down Expand Up @@ -2045,7 +2045,7 @@ public async Task DataFileUpdate_Propagated()
DataFiles.Models.DataFile orgFileFromRepo = (await _env.DataFiles.GetAsync(FILE1_SRC_ID))!;
DataFiles.Models.Corpus orgCorpusFromRepo = (await _env.Corpora.GetAsync(TARGET_CORPUS_ID))!;
Assert.That(orgFileFromClient.Name, Is.EqualTo(orgFileFromRepo.Name));
Assert.That(orgCorpusFromRepo.Files[0].FileId, Is.EqualTo(FILE2_TRG_ID));
Assert.That(orgCorpusFromRepo.Files[0].FileRef, Is.EqualTo(FILE2_TRG_ID));

// Update the file
await dataFilesClient.UpdateAsync(FILE1_SRC_ID, new FileParameter(new MemoryStream([1, 2, 3]), "test.txt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CorpusServiceTests
Owner = "owner1",
Name = "corpus1",
Language = "en",
Files = new List<CorpusFile>() { new() { FileId = DefaultDataFile.Id } }
Files = new List<CorpusFile>() { new() { FileRef = DefaultDataFile.Id } }
};

[Test]
Expand Down Expand Up @@ -56,8 +56,8 @@ public TestEnvironment()
});
Service = new CorpusService(
Corpora,
Substitute.For<IRepository<DataFile>>(),
DataAccessContext,
Substitute.For<IDataFileService>(),
Substitute.For<IScopedMediator>()
);
}
Expand Down
Loading