-
Notifications
You must be signed in to change notification settings - Fork 62
/
AddProductWithIdentityColumnIncluded.cs
39 lines (36 loc) · 1.96 KB
/
AddProductWithIdentityColumnIncluded.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common;
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples
{
public static class AddProductWithIdentityColumnIncluded
{
/// <summary>
/// This shows an example of a SQL Output binding where the target table has a primary key
/// which is an identity column and the identity column is included in the output object. In
/// this case the identity column is used to match the rows and all non-identity columns are
/// updated if a match is found. Otherwise a new row is inserted (with the identity column being
/// ignored since that will be generated by the engine).
/// </summary>
/// <param name="req">The original request that triggered the function</param>
/// <param name="product">The created Product object</param>
/// <returns>The CreatedResult containing the new object that was inserted</returns>
[FunctionName(nameof(AddProductWithIdentityColumnIncluded))]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "addproductwithidentitycolumnincluded")]
HttpRequest req,
[Sql("dbo.ProductsWithIdentity", "SqlConnectionString")] out ProductWithOptionalId product)
{
product = new ProductWithOptionalId
{
Name = req.Query["name"],
ProductId = string.IsNullOrEmpty(req.Query["productId"]) ? null : int.Parse(req.Query["productId"]),
Cost = int.Parse(req.Query["cost"])
};
return new CreatedResult($"/api/addproductwithidentitycolumnincluded", product);
}
}
}