-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDoJsonHandler.cs
152 lines (122 loc) · 4.67 KB
/
ToDoJsonHandler.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 ToDoJsonHandler
{
const string TEST = "json";
private static async Task<JToken> GetBodyData(HttpRequest req)
{
JToken bodyData = new JObject();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if (!string.IsNullOrEmpty(requestBody))
{
// Can't do this: need a schema
//bodyData = JsonConvert.DeserializeObject<JToken>(requestBody);
// This is the way :)
var t = JsonConvert.DeserializeObject<ToDo>(requestBody);
bodyData = JObject.FromObject(t);
}
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());
}
var resultSet = await conn.QueryAsync<string>(
sql: $"web.{verb}_todo_{TEST}",
param: parameters,
commandType: CommandType.StoredProcedure
);
var jr = new JArray();
resultSet.ToList().ForEach(i =>
{
jr.Add(JObject.Parse(i));
});
if (jr.Count() == 1)
result = jr[0];
else
result = jr;
}
return result;
}
[FunctionName("get-json")]
public static async Task<IActionResult> Get(
[HttpTrigger("get", Route = "todo/json/{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);
// If requesting ALL todo, always return an array
if (id == null && result.Type == JTokenType.Object)
result = new JArray() { result };
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
[FunctionName("post-json")]
public static async Task<IActionResult> Post(
[HttpTrigger("post", Route = "todo/json/{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-json")]
public static async Task<IActionResult> Patch(
[HttpTrigger("patch", Route = "todo/json/{id?}")] HttpRequest req,
int? id,
ILogger log)
{
// Load existing todo
var target_json = await ExecuteProcedure("get", new JObject { ["id"] = id.Value });
// Get new todo
var source_json = (JObject)(await GetBodyData(req));
// Patch
((JObject)target_json).Merge(source_json);
// Save to database
var payload = new JObject
{
["id"] = id.Value,
["todo"] = target_json
};
JToken result = await ExecuteProcedure("patch", payload);
Utils.EnrichJsonResult(req, result, TEST);
return new OkObjectResult(result);
}
[FunctionName("delete-json")]
public static async Task<IActionResult> Delete(
[HttpTrigger("delete", Route = "todo/json/{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);
}
}
}