Skip to content

Commit bf33045

Browse files
Lanning, MarkLanning, Mark
authored andcommitted
update local insert to use id not resource key
1 parent 5f6cac0 commit bf33045

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

ItemStore/src/ThingsLibrary.ItemStore.Cosmos/ItemStoreCosmos.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public async Task<bool> ExistsAsync(string partitionKey, string resourceKey, Can
184184

185185
return await this.GetAsync(partitionKey, resourceKey, cancellationToken) != null;
186186
}
187-
187+
188188
/// <inheritdoc />
189189
public async Task<ItemEnvelope?> GetAsync(string partitionKey, string resourceKey, CancellationToken cancellationToken)
190190
{

ItemStore/src/ThingsLibrary.ItemStore.Local/ItemStoreLocal.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,21 @@ public Task<bool> ExistsAsync(string partitionKey, string resourceKey, Cancellat
217217
return Task.FromResult<bool>(this.Collection.Count(x => x.ResourceKey == resourceKey) > 0);
218218
}
219219

220+
/// <inheritdoc />
221+
public Task<ItemEnvelope?> GetAsync(string id, CancellationToken cancellationToken)
222+
{
223+
var result = this.Collection.FindOne(x => x.Id == id);
224+
225+
return Task.FromResult<ItemEnvelope?>(result);
226+
}
227+
220228
/// <inheritdoc />
221229
public Task<ItemEnvelope?> GetAsync(string partitionKey, string resourceKey, CancellationToken cancellationToken)
222230
{
223231
ArgumentException.ThrowIfNullOrEmpty(partitionKey);
224232
ArgumentException.ThrowIfNullOrEmpty(resourceKey);
225233

226-
var result = this.Collection.FindOne(x => x.ResourceKey == resourceKey);
234+
var result = this.Collection.FindOne(x => x.Partition == partitionKey && x.ResourceKey == resourceKey);
227235

228236
return Task.FromResult<ItemEnvelope?>(result);
229237
}
@@ -236,7 +244,7 @@ public Task<List<ItemEnvelope>> GetFamilyAsync(string partitionKey, string resou
236244

237245
var resourceKeyPrefix = $"{resourceKey}/";
238246

239-
var items = this.Collection.Query().Where(x => x.ResourceKey == resourceKey || x.ResourceKey.StartsWith(resourceKeyPrefix)).ToList();
247+
var items = this.Collection.Query().Where(x => x.Partition == partitionKey && (x.ResourceKey == resourceKey || x.ResourceKey.StartsWith(resourceKeyPrefix))).ToList();
240248

241249
return Task.FromResult<List<ItemEnvelope>>(items);
242250
}
@@ -257,7 +265,7 @@ public Task InsertAsync(ItemEnvelope itemEnvelope, CancellationToken cancellatio
257265
lock (LockObject)
258266
{
259267
// creates
260-
this.Collection.Insert(itemEnvelope);
268+
this.Collection.Insert(itemEnvelope.Id, itemEnvelope);
261269
}
262270

263271
//save out entity?
@@ -279,7 +287,8 @@ public Task UpdateAsync(ItemEnvelope itemEnvelope, CancellationToken cancellatio
279287
lock (LockObject)
280288
{
281289
// creates (if missing) or replaces entity
282-
this.Collection.Upsert(itemEnvelope.ResourceKey, itemEnvelope);
290+
bool updated = this.Collection.Update(itemEnvelope.Id, itemEnvelope);
291+
if (!updated) { throw new InvalidOperationException($"Unable to update ItemEnvelope with ResourceKey '{itemEnvelope.ResourceKey}' because it does not exist."); }
283292
}
284293

285294
//save out entity?

ItemStore/tests/ThingsLibrary.ItemStore.Tests/UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public async Task Test_InsertUpdate()
126126
await ItemStore.UpdateAsync(envelope2, UnitTests.CancellationToken);
127127
Thread.Sleep(100);
128128

129-
// ======================================================================
129+
// =================================>=====================================
130130
// FETCH
131131
var envelope3 = await ItemStore.GetAsync(partitionKey, resourceKey, UnitTests.CancellationToken);
132132

0 commit comments

Comments
 (0)