Assign IDs when promoting dynamic blueprints
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
using QuestionsApi.Models;
|
||||
|
||||
namespace QuestionsApi.Services;
|
||||
@@ -116,9 +117,15 @@ public sealed class DynamicQuestionStore
|
||||
public async Task UpsertBlueprintsAsync(IEnumerable<DynamicQuestionBlueprint> blueprints)
|
||||
{
|
||||
foreach (var blueprint in blueprints)
|
||||
{
|
||||
var existing = await _blueprints.Find(
|
||||
value => value.ExternalId == blueprint.ExternalId && value.Version == blueprint.Version)
|
||||
.FirstOrDefaultAsync();
|
||||
blueprint.Id = existing is null ? ObjectId.GenerateNewId().ToString() : existing.Id;
|
||||
await _blueprints.ReplaceOneAsync(
|
||||
value => value.ExternalId == blueprint.ExternalId && value.Version == blueprint.Version,
|
||||
blueprint,
|
||||
new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user