layout | title | nav_order |
---|---|---|
default |
Task 2 |
2 |
In this and future tasks, you will be working with a website for rating pizza. The site collects ratings for various pizzas and users can give a score from 0 to 4. The ratings are stored in an Azure Cosmos DB. Cosmos DB is a fully managed NoSQL database for modern app development.
Your task will be to create an Azure Function that is triggered each time a new rating is given on the site. This can be done using a Azure function with a Cosmos DB trigger.
For the purpose of this workshop, each instance of the site stores the ratings in a separate collection in Cosmos DB. This way, you can test you function by adding a rating to the site, and it only triggers your function.
Your individual resources will be identified by using the GUID displayed in the top right corner of the website.
Keep this GUID handy and insert it everywhere in the description where [INSERT GUID]
is mentioned.
- In VS Code, under the
Workspace
section of the Azure extension, selectAdd
andCreate Function
.
You will now be prompted for configurations for the project and login to Azure. Input the following values:
[TODO: go through steps and confirm what needs to be selected] [Note: Could we have a file ready for them where they only need to replace certain parts of the code? ]
-
Template for function: Azure Cosmos DB trigger
-
Function name: CosmosTriggerFunction
-
Namespace: LearningFunctions.CosmosTriggerFunction
-
Storage settings: AzureWebJobsStorage
-
Database name: storage
-
Collection name: ratings_[INSERT GUID] e.g.
ratings_88a3175c-310a-45b4-920d-c0576f617e5d
-
Storage account prompt: Use local emulator
Your function is now being set up and a file CosmosTriggerFunction.cs
will be added to your folder.
- In
local.settings.json
you will now be adding a new property to the values section.
New settings property
"CosmosConnection": "AccountEndpoint=https://abakus-workshop.documents.azure.com:443/;AccountKey=XnSfxZw1Npwzw5oDg1OvIDzBpX8h9KirkDLTsghy7myFCyW3YmOdyVIIyB0bINwmQju0UxIE6aN7C8CKhNK05w==;"-
Complete the trigger configuration in
CosmosTriggerFunction.cs
The
Run
function has a configuration for the Cosmos DB trigger, but it is not yet complete.[CosmosDBTrigger( databaseName: "storage", collectionName: "ratings_[INSERT GUID]", // insert your guid here ConnectionStringSetting = "", LeaseCollectionName = "leases")]
-
Ensure that database name is set to
storage
, and that the collection name is set toratings_[GUID]
. -
The value of
ConnectionStringSetting
should be"CosmosConnection"
. This refers to the value that was included in local.settings.json -
Change the value of
LeaseCollectionName
from leases to"leases_[GUID]"
Remember to insert your custom guid.
The fully configured attribute should look like this "
[CosmosDBTrigger( databaseName: "storage", collectionName: "ratings_88a3175c-310a-45b4-920d-c0576f617e5d", ConnectionStringSetting = "CosmosConnection", LeaseCollectionName = "leases_88a3175c-310a-45b4-920d-c0576f617e5d")]
-
-
Add rating model class to be used by the function
In
CosmosTriggerFunction.cs
, replace the classMyDocument
with the model for ratingpublic class Rating { public Guid Id { get; set; } public int PizzaId { get; set; } public int Score { get; set; } public DateTime Created { get; set; } }
and change type for input from
IReadOnlyList<MyDocument>
toIReadOnlyList<Rating>
-
Run the function
Your function should now be ready to go and you can run it by typing the cmd
func start
in the terminal.Each time a rating is given on the web site, you should see activity in your console.
Question
The template function only accesses the first element in the input collection. In what cases would the collection hold more than one element?
-
Ensure that all changes to the ratings results in a log line in the console.
Hint: Try looping through the input collection with a ForEach loop.
-
Print the content of the rating in the console.
To convert the input object to a rating element by casting it.
Rating r = (Rating) input[i];
Hint: Use System.Text.Json.JsonSerializer to serialize the rating object to a json string.
-
Print a different string to the console depending of the score of the rating.
Hint: Use a switch case.