-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (67 loc) · 3.25 KB
/
Program.cs
File metadata and controls
78 lines (67 loc) · 3.25 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Headstorm_Database_Challenge
{
public class Record
{
public int RecordID { get; set; }
public string Name { get; set; }
public string CellPhone { get; set; }
public string WorkPhone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public int? BasicWidgetOrder { get; set; }
public int? AdvancedWidgetOrder { get; set; }
public bool ProtectionPlan { get; set; }
}
class Program
{
public static string SQLQueryMaker()
{
StringBuilder result = new StringBuilder();
List<Record> records = new List<Record>();
string fileName = System.Environment.CurrentDirectory + "\\DatabaseRecords.json";
if (File.Exists(fileName))
{
String JSONtxt = File.ReadAllText(fileName);
records = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(JSONtxt);
}
result.AppendLine("DECLARE @CustomerID AS INT");
result.AppendLine("");
foreach (Record record in records)
{
//Insert the customer into the Customers table if it's a new customer, otherwise get the existing customer ID to link the order.
//Usually would use parameters instead of adding strings, but that doesn't work when printing a query to a file.
//I haven't tested the SQL statements themselves, but they should either work or be close to working.
result.AppendLine("IF " + record.Email + " IN (SELECT Email FROM Customers)");
result.AppendLine("BEGIN");
result.AppendLine("SELECT @CustomerID = CustomerID FROM Customers WHERE Email = " + record.Email);
result.AppendLine("END");
result.AppendLine("ELSE");
result.AppendLine("BEGIN");
result.AppendLine("INSERT INTO Orders (Name, CellPhone, WorkPhone, Email, Address)");
result.AppendLine("OUTPUT Inserted.ID INTO @CustomerID");
result.AppendLine("VALUES( " + record.Name + ", " + record.CellPhone + ", " + record.WorkPhone + ", " + record.Email + ", " + record.Address + " )");
result.AppendLine("END");
result.AppendLine("");
result.AppendLine("INSERT INTO Orders (CustomerID, RecordID, WidgetOrder, Advanced, ProtectionPlan) VALUES (@CustomerID, " + record.RecordID + ", " + (record.BasicWidgetOrder == null ? record.AdvancedWidgetOrder : record.BasicWidgetOrder) + ", " + (record.BasicWidgetOrder == null ? 1 : 0) + ", " + (record.ProtectionPlan ? 1 : 0 ) + " )");
result.AppendLine("");
}
return result.ToString();
}
static void Main(string[] args)
{
string fileName = System.Environment.CurrentDirectory + "\\test.txt";
File.Delete(fileName);
TextWriter tw = new StreamWriter(@fileName, true);
tw.Write(SQLQueryMaker());
tw.Flush();
tw.Close();
}
}
}