Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (75 loc) · 3.32 KB
/
Copy pathProgram.cs
File metadata and controls
82 lines (75 loc) · 3.32 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
81
// Program.cs
using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace ConsoleApp
{
public class Program
{
public void Main(string[] args)
{
JArray o1 = JArray.Parse(File.ReadAllText(@"pricebook.json"));
Console.WriteLine(" items read: {0}", o1.Count);
using(var pricebook = new PricebookContext())
{
foreach(var item in o1.Children())
{
var itemProperties = item.Children<JProperty>();
Item priceItem = new Item {
Name = (string)itemProperties.FirstOrDefault(x => x.Name == "item").Value,
Division = (string)itemProperties.FirstOrDefault(x => x.Name == "division").Value,
Size = (string)itemProperties.FirstOrDefault(x => x.Name == "size").Value,
BotanicalName = (string)itemProperties.FirstOrDefault(x => x.Name == "botanicalName").Value,
ItemNumber = (string)itemProperties.FirstOrDefault(x => x.Name == "itemNumber").Value,
LaborHours = GetDouble((string)itemProperties.FirstOrDefault(x => x.Name == "laborHours").Value),
MaterialCost = GetDouble((string)itemProperties.FirstOrDefault(x => x.Name == "materialCost").Value),
CostDate = GetDate((string)itemProperties.FirstOrDefault(x => x.Name == "costDate").Value),
Taxable = GetBoolean((string)itemProperties.FirstOrDefault(x => x.Name == "taxable").Value),
Warranty = GetBoolean((string)itemProperties.FirstOrDefault(x => x.Name == "warranty").Value),
CompEase = (string)itemProperties.FirstOrDefault(x => x.Name == "compEase").Value,
Description = (string)itemProperties.FirstOrDefault(x => x.Name == "description").Value,
Price = GetDouble((string)itemProperties.FirstOrDefault(x => x.Name == "price").Value),
RoundedPrice = GetDouble((string)itemProperties.FirstOrDefault(x => x.Name == "roundedPrice").Value)
};
pricebook.Items.Add(priceItem);
}
var count = pricebook.SaveChanges();
Console.WriteLine(" {0} records saved to database", count);
}
}
private double GetDouble(string str )
{
try
{
return Convert.ToDouble(str);
}
catch (System.Exception)
{
// maybe log which one blew up
return 0.0;
}
}
private Boolean GetBoolean(string str)
{
return "y".Equals(str, StringComparison.OrdinalIgnoreCase);
}
private DateTime GetDate(string str)
{
try
{
return DateTime.Parse(str);
}
catch (System.Exception)
{
return DateTime.Parse("01/01/1901");
}
}
}
// TODO: Remove. Will be unnecessary when bug #2357 fixed
// See https://github.com/aspnet/EntityFramework/issues/2357
public class Startup
{
public void Configure() { }
}
}