-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ami Hollander
committed
Jan 6, 2024
1 parent
4874a1c
commit ad544ca
Showing
2 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
using backend.Entities; | ||
using backend.Models; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Extensions.DurableTask; | ||
namespace backend | ||
{ | ||
public class CosmosTodoItemTrigger | ||
{ | ||
[FunctionName("CosmosTodoItemTrigger")] | ||
public async Task Run( | ||
[CosmosDBTrigger( | ||
databaseName: "%CosmosDatabaseName%", | ||
containerName: "TodoItem", | ||
Connection = "CosmosConnectionOptions", | ||
LeaseContainerName = "Leases", | ||
StartFromBeginning = true, | ||
CreateLeaseContainerIfNotExists = false)] IReadOnlyList<TodoItem> input, | ||
[CosmosDB( | ||
databaseName: "%CosmosDatabaseName%", | ||
containerName: "TodoItem", | ||
Connection = "CosmosConnectionOptions")] | ||
IAsyncCollector<TodoItem> output, | ||
[DurableClient] IDurableEntityClient durableEntityClient) | ||
{ | ||
if (input != null && input.Count > 0) | ||
{ | ||
foreach (var item in input) | ||
{ | ||
bool updated = await UpdateDbState(output, item); | ||
|
||
if (!updated) | ||
{ | ||
await UpdateEntityState(durableEntityClient, item); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private async Task<bool> UpdateDbState(IAsyncCollector<TodoItem> output, TodoItem item) | ||
{ | ||
if (item.State != TodoItemState.Overdue && item.IsOverdue()) | ||
{ | ||
item.State = TodoItemState.Overdue; | ||
item.UpdatedDate = DateTimeOffset.UtcNow.DateTime; | ||
await output.AddAsync(item); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private async Task UpdateEntityState(IDurableEntityClient durableEntityClient, TodoItem item) | ||
{ | ||
if (item.DueDate is null || item.IsOverdue()) | ||
{ | ||
await durableEntityClient.SignalEntityAsync<ITodoItemEntity>(item.Id, proxy => proxy.Delete()); | ||
return; | ||
} | ||
|
||
await durableEntityClient.SignalEntityAsync<ITodoItemEntity>(item.Id, proxy => proxy.Create(item)); | ||
} | ||
} | ||
} |