|
| 1 | +package com.microsoft.azure.functions.endtoend; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.annotation.*; |
| 4 | +import com.google.gson.Gson; |
| 5 | +import com.microsoft.azure.functions.*; |
| 6 | +import com.microsoft.azure.functions.HttpMethod; |
| 7 | +import com.microsoft.azure.functions.sql.annotation.CommandType; |
| 8 | +import com.microsoft.azure.functions.sql.annotation.SQLInput; |
| 9 | +import com.microsoft.azure.functions.sql.annotation.SQLOutput; |
| 10 | +import com.microsoft.azure.functions.sql.annotation.SQLTrigger; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.lang.reflect.Array; |
| 14 | +import java.util.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * Azure Functions with Azure SQL DB. |
| 18 | + */ |
| 19 | +public class SqlTriggerTests { |
| 20 | + |
| 21 | + @FunctionName("GetProducts") |
| 22 | + public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, |
| 23 | + HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) |
| 24 | + HttpRequestMessage<Optional<String>> request, |
| 25 | + @SQLInput(name = "products", commandText = "SELECT TOP 1 * FROM Products WHERE ProductId = @ProductId", |
| 26 | + commandType = CommandType.Text, parameters = "@ProductId={productid}", |
| 27 | + connectionStringSetting = "AzureWebJobsSqlConnectionString") Product[] products, |
| 28 | + final ExecutionContext context) { |
| 29 | + |
| 30 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 31 | + |
| 32 | + if (products.length != 0) { |
| 33 | + return request.createResponseBuilder(HttpStatus.OK).body(products[0].toString()).build(); |
| 34 | + } else { |
| 35 | + return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) |
| 36 | + .body("Did not find expected product in table Products").build(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @FunctionName("AddProduct") |
| 41 | + public HttpResponseMessage AddProduct(@HttpTrigger(name = "req", methods = { HttpMethod.GET, |
| 42 | + HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 43 | + @SQLOutput(name = "product", commandText = "Products", connectionStringSetting = "AzureWebJobsSqlConnectionString") OutputBinding<Product> product, |
| 44 | + final ExecutionContext context) { |
| 45 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 46 | + |
| 47 | + String json = request.getBody().get(); |
| 48 | + product.setValue(new Gson().fromJson(json, Product.class)); |
| 49 | + |
| 50 | + return request.createResponseBuilder(HttpStatus.OK).body(product).build(); |
| 51 | + } |
| 52 | + |
| 53 | + public class Product { |
| 54 | + public int ProductId; |
| 55 | + public String Name; |
| 56 | + public int Cost; |
| 57 | + |
| 58 | + public String toString() { |
| 59 | + return "{\"ProductId\":" + ProductId + ",\"Name\":\"" + Name + "\",\"Cost\":" + Cost + "}"; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments