-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoClassicHandler.cs
125 lines (100 loc) · 3.91 KB
/
ToDoClassicHandler.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Data.SqlClient;
using Dapper;
using System.Data;
using System.Linq;
namespace azure_sql_todo_backend_func_dotnet
{
public static class ToDoClassicHandler
{
const string TEST = "classic";
private static async Task<JToken> GetBodyData(HttpRequest req)
{
JToken bodyData = new JObject();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if (!string.IsNullOrEmpty(requestBody))
{
bodyData = JsonConvert.DeserializeObject<JToken>(requestBody);
}
return bodyData;
}
private static async Task<JToken> ExecuteProcedure(string verb, JToken payload)
{
JToken result = new JArray();
using (var conn = new SqlConnection(Environment.GetEnvironmentVariable("AzureSQLConnectionString")))
{
DynamicParameters parameters = new DynamicParameters();
if (payload != null)
{
parameters.Add("payload", payload.ToString());
}
string stringResult = await conn.ExecuteScalarAsync<string>(
sql: $"web.{verb}_todo_{TEST}",
param: parameters,
commandType: CommandType.StoredProcedure
);
if (!string.IsNullOrEmpty(stringResult)) result = JToken.Parse(stringResult);
}
return result;
}
[FunctionName("get-classic")]
public static async Task<IActionResult> Get(
[HttpTrigger("get", Route = "todo/classic/{id?}")] HttpRequest req,
int? id,
ILogger log)
{
JToken payload = null;
if (id.HasValue) payload = new JObject { ["id"] = id.Value };
JToken result = await ExecuteProcedure("get", payload);
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
[FunctionName("post-classic")]
public static async Task<IActionResult> Post(
[HttpTrigger("post", Route = "todo/classic/{id?}")] HttpRequest req,
int? id,
ILogger log)
{
JToken payload = await GetBodyData(req);
JToken result = await ExecuteProcedure("post", payload);
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
[FunctionName("patch-classic")]
public static async Task<IActionResult> Patch(
[HttpTrigger("patch", Route = "todo/classic/{id?}")] HttpRequest req,
int? id,
ILogger log)
{
JToken payload = new JObject
{
["id"] = id.Value,
["todo"] = await GetBodyData(req)
};
JToken result = await ExecuteProcedure("patch", payload);
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
[FunctionName("delete-classic")]
public static async Task<IActionResult> Delete(
[HttpTrigger("delete", Route = "todo/classic/{id?}")] HttpRequest req,
int? id,
ILogger log)
{
JToken payload = null;
if (id.HasValue) payload = new JObject { ["id"] = id.Value };
JToken result = await ExecuteProcedure("delete", payload);
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
}
}