-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookupOrg.cs
101 lines (88 loc) · 3.88 KB
/
LookupOrg.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
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System.Reflection.Emit;
namespace api.multifol.io
{
public class LookupOrg
{
private readonly ILogger<LookupOrg> _logger;
public LookupOrg(ILogger<LookupOrg> logger)
{
_logger = logger;
}
[Function("LookupOrg")]
public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
var userID = Environment.GetEnvironmentVariable("MYSQL_USER");
var dbName = Environment.GetEnvironmentVariable("MYSQL_DATABASE");
var pw = Environment.GetEnvironmentVariable("MYSQL_PASSWORD");
var server = Environment.GetEnvironmentVariable("MYSQL_SERVER");
var builder = new MySqlConnectionStringBuilder
{
Server = server + ".mysql.database.azure.com",
Database = dbName,
UserID = userID,
Password = pw,
SslMode = MySqlSslMode.Required,
};
// TODO: support multiple terms
string term = req.Query["term"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
term = term ?? data?.term;
if (string.IsNullOrEmpty(term))
{
return new OkObjectResult("Ok, 0, results returned");
}
try
{
await using var connection = new MySqlConnection(builder.ConnectionString);
await connection.OpenAsync();
string sql =
"select *, 1 as rowOrder from organizations where " +
"MATCH (Organization) AGAINST (@term IN BOOLEAN MODE)" +
"UNION " +
"select *, 2 as rowOrder from organizations where " +
"MATCH(Organization) AGAINST(concat(@term, '* -', @term) IN BOOLEAN MODE) " +
"ORDER by rowOrder, Organization";
int rowCount = 0;
using (var command = new MySqlCommand())
{
command.Connection = connection;
command.CommandText = sql;
command.Parameters.AddWithValue("term", term);
_logger.LogInformation($"Execute reader searching for {term}");
using var reader = await command.ExecuteReaderAsync();
bool fieldCountShown = false;
while (await reader.ReadAsync())
{
rowCount++;
if (!fieldCountShown)
{
_logger.LogInformation($"fields: {reader.FieldCount.ToString()}");
fieldCountShown = true;
}
var EIN = reader.GetString(0);
var organization = reader.GetString(1);
var city = reader.GetString(2);
var state = reader.GetString(3);
var businessCode = reader.GetString(4);
_logger.LogInformation($"{EIN}, {organization}, {city}, {state}, {businessCode}");
}
}
return new OkObjectResult($"Ok, {rowCount}, results returned");
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message + '\n' + ex.StackTrace);
return new BadRequestObjectResult("Returned BadRequest");
}
}
}
}