Embed question verification references
This commit is contained in:
@@ -151,6 +151,32 @@ public class QuestionStore
|
||||
return systems.Where(s => !string.IsNullOrWhiteSpace(s)).OrderBy(s => s).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetMissingExternalIdsAsync(IEnumerable<string> externalIds)
|
||||
{
|
||||
var requested = externalIds.Distinct(StringComparer.Ordinal).ToList();
|
||||
var existing = await _col.Find(Builders<Question>.Filter.In(q => q.ExternalId, requested))
|
||||
.Project(q => q.ExternalId)
|
||||
.ToListAsync();
|
||||
return requested.Except(existing, StringComparer.Ordinal).OrderBy(id => id).ToList();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateReferencesByExternalIdAsync(IEnumerable<QuestionReferencesUpdate> updates)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var writes = updates.Select(update =>
|
||||
new UpdateOneModel<Question>(
|
||||
Builders<Question>.Filter.Eq(question => question.ExternalId, update.ExternalId.Trim()),
|
||||
Builders<Question>.Update
|
||||
.Set(question => question.References, NormalizeReferences(update.References))
|
||||
.Set(question => question.UpdatedUtc, now)))
|
||||
.ToList();
|
||||
if (writes.Count == 0)
|
||||
return 0;
|
||||
|
||||
var result = await _col.BulkWriteAsync(writes, new BulkWriteOptions { IsOrdered = true });
|
||||
return (int)result.MatchedCount;
|
||||
}
|
||||
|
||||
private static Question FromRequest(UpsertQuestionRequest req) => new()
|
||||
{
|
||||
ExternalId = req.ExternalId.Trim(),
|
||||
@@ -169,6 +195,18 @@ public class QuestionStore
|
||||
Labs = req.Labs,
|
||||
Media = req.Media,
|
||||
Tags = req.Tags.Where(t => !string.IsNullOrWhiteSpace(t)).Select(t => t.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).ToList(),
|
||||
References = NormalizeReferences(req.References),
|
||||
IsPublished = req.IsPublished
|
||||
};
|
||||
|
||||
private static List<QuestionReference> NormalizeReferences(IEnumerable<QuestionReference>? references) =>
|
||||
(references ?? []).Select(reference => new QuestionReference
|
||||
{
|
||||
Title = reference.Title.Trim(),
|
||||
Publisher = reference.Publisher.Trim(),
|
||||
Url = reference.Url.Trim(),
|
||||
Supports = reference.Supports.Trim(),
|
||||
AccessedOn = reference.AccessedOn.Trim(),
|
||||
Reviewer = reference.Reviewer.Trim()
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user