Skip to content

Commit

Permalink
CosmosDB function to log changes works local, config for prod added b…
Browse files Browse the repository at this point in the history
…ut not tested
  • Loading branch information
Torkelsen committed Oct 11, 2024
1 parent 5fd0dbd commit 8f909ae
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
/handlenett-backend/web-api/HandlenettAPI/Services/SlackProfile.json
/handlenett-backend/web-api/HandlenettAPI/Services/usersList.json
/handlenett-backend/serverless/HttpTriggerTest/Properties/serviceDependencies.local.json
/handlenett-backend/serverless/functions/Properties/ServiceDependencies/local
/handlenett-backend/serverless/functions/Properties/serviceDependencies.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>d5330943-52bd-4269-adae-63ac4d8b8e26</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Content Include="local.settings.json" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.11.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.8.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

namespace HandlenettNotifications
{
public class Function1Renamed
public class IsServiceUp
{
private readonly ILogger<Function1Renamed> _logger;
private readonly ILogger<IsServiceUp> _logger;

public Function1Renamed(ILogger<Function1Renamed> logger)
public IsServiceUp(ILogger<IsServiceUp> logger)
{
_logger = logger;
}

[Function("OnlyChangedAttribute")]
[Function("IsServiceUp")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult("Hello World!");
return new OkObjectResult("Yep, I'm alive!");
}
}
}
44 changes: 44 additions & 0 deletions handlenett-backend/serverless/functions/ItemsChangeFeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace HandlenettNotifications
{
public class ItemsChangeFeed
{
private readonly ILogger _logger;

public ItemsChangeFeed(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<ItemsChangeFeed>();
}

[Function("ItemsChangeFeed")]
public void Run([CosmosDBTrigger(
databaseName: "Handlenett",
containerName: "ShoppingListItems",
Connection = "ConnectionStrings--AzureFunctionsCosmosDB",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> input)
{

if (input != null && input.Count > 0)
{
_logger.LogInformation("Documents modified: " + input.Count);
_logger.LogInformation("First document Id: " + input[0].id);
}
}
}

public class MyDocument
{
public string id { get; set; }

Check warning on line 36 in handlenett-backend/serverless/functions/ItemsChangeFeed.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public string Text { get; set; }

Check warning on line 38 in handlenett-backend/serverless/functions/ItemsChangeFeed.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Text' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public int Number { get; set; }

public bool Boolean { get; set; }
}
}
1 change: 1 addition & 0 deletions handlenett-backend/serverless/functions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
})
.Build();


host.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"dependencies": {
"appInsights1": {
"type": "appInsights"
},
"secrets1": {
"type": "secrets.keyVault",
"connectionId": "VaultUri"
},
"cosmosdb1": {
"type": "cosmosdb.azure",
"connectionId": "ConnectionStrings--AzureFunctionsCosmosDB",
"secretStore": "AzureKeyVault"
}
}
}

0 comments on commit 8f909ae

Please sign in to comment.