-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetHighScores.csx
More file actions
80 lines (51 loc) · 1.89 KB
/
GetHighScores.csx
File metadata and controls
80 lines (51 loc) · 1.89 KB
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
#r "Newtonsoft.Json"
# load "serialize.csx"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
using System.Data.SqlClient;
public static async Task < HttpResponseMessage > Run(HttpRequest req, ILogger log) {
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string json = " ";
try {
var str = Environment.GetEnvironmentVariable("SQLDB_CONNECTION");
using(SqlConnection conn = new SqlConnection(str)) {
using(SqlCommand cmd = new SqlCommand())
{
System.Data.SqlClient.SqlDataReader dataReader;
cmd.CommandText = "SELECT SUBSTRING(player_a, 1, 5) as player_a, SUBSTRING(player_b, 1, 5) as player_b, score FROM dbo.high_scores ORDER BY score DESC";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
dataReader = cmd.ExecuteReader();
var r = Serialize(dataReader);
json = JsonConvert.SerializeObject(r, Formatting.Indented);
}
}
} catch (SqlException sqlex)
{
log.LogInformation(sqlex.Message);
log.LogInformation(sqlex.ToString());
return new HttpResponseMessage(HttpStatusCode.BadRequest) {
Content = new StringContent(JsonConvert.SerializeObject($"The following SqlException happened: {sqlex.Message}"), Encoding.UTF8, "application/json")
};
} catch (Exception ex)
{
log.LogInformation(ex.Message);
log.LogInformation(ex.ToString());
return new HttpResponseMessage(HttpStatusCode.BadRequest){
Content = new StringContent(JsonConvert.SerializeObject($"The following SqlException happened: {ex.Message}"), Encoding.UTF8, "application/json")
};
}
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
}