-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
137 lines (108 loc) · 5.37 KB
/
Database.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
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Windows;
namespace powerLabel
{
public static class Database
{
public static void AddSystem(ComputerSystem system)
{
try
{
using (var db = new ComputerSystemContext())
{
if (system.id != 0)
{
return;
}
ComputerSystem sysToInsert = new ComputerSystem();
db.ComputerSystems.Add(sysToInsert);
db.SaveChanges();
db.ComputerSystems.Attach(sysToInsert);
if (!db.Motherboards.ToList().Any(a => a.Equals(system.motherboard)))
{
db.Motherboards.Add(system.motherboard);
db.SaveChanges();
}
Motherboard mobo = db.Motherboards.ToList().Where(a => a.Equals(system.motherboard)).First();
sysToInsert.motherboard = mobo;
if (!db.Bioses.ToList().Any(a => a.Equals(system.bios)))
{
db.Bioses.Add(system.bios);
db.SaveChanges();
}
Bios bios = db.Bioses.ToList().Where(a => a.Equals(system.bios)).First();
sysToInsert.bios = bios;
if (!db.OSes.ToList().Any(a => a.Equals(system.operatingSystem)))
{
db.OSes.Add(system.operatingSystem);
db.SaveChanges();
}
OS os = db.OSes.ToList().Where(a => a.Equals(system.operatingSystem)).First();
sysToInsert.operatingSystem = os;
// Add processor if it doesnt exist in the database
if (!db.Processors.ToList().Any(a => a.Equals(system.processor)))
{
db.Processors.Add(system.processor);
db.SaveChanges();
}
Processor processor = db.Processors.ToList().Where(a => a.Equals(system.processor)).First();
sysToInsert.processorAmount = system.processorAmount;
sysToInsert.processor = processor;
foreach (MemoryConfig memoryConfig in system.memoryModules)
{
if (!db.MemoryModules.ToList().Any(a => a.Equals(memoryConfig.module)))
{
db.MemoryModules.Add(memoryConfig.module);
db.SaveChanges();
}
MemoryModule module = db.MemoryModules.ToList().Where(a => a.Equals(memoryConfig.module)).First();
db.MemoryConfigs.Add(new MemoryConfig() { module = module, currentClockspeed = memoryConfig.currentClockspeed, system = sysToInsert });
}
foreach (DiskConfig diskConfig in system.diskConfigs)
{
if (!db.Disks.ToList().Any(a => a.Equals(diskConfig.disk)))
{
db.Disks.Add(diskConfig.disk);
db.SaveChanges();
}
Disk disk = db.Disks.ToList().Where(a => a.Equals(diskConfig.disk)).First();
db.DiskConfigs.Add(new DiskConfig() { disk = disk, busType = diskConfig.busType, systemDisk = diskConfig.systemDisk, computerSystem = sysToInsert });
}
foreach (VideoControllerConfig videoControllerConfig in system.videoControllerConfigs)
{
if (!db.VideoControllerConfigs.Include(a => a.videoController).ToList().Any(a => a.videoController.Equals(videoControllerConfig.videoController)))
{
db.VideoControllers.Add(videoControllerConfig.videoController);
db.SaveChanges();
}
VideoController videoController = db.VideoControllers.ToList().Where(a => a.Equals(videoControllerConfig.videoController)).First();
db.VideoControllerConfigs.Add(new VideoControllerConfig() { videoController = videoController, computerSystem = sysToInsert });
}
db.ComputerSystems.Update(sysToInsert);
db.SaveChanges();
string employee = "";
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(MainWindow))
{
employee = (window as MainWindow).employeeDropdown.Text;
}
}
if (employee == "")
{
MessageBox.Show("No employee selected");
return;
}
db.Events.Add(Event.NewScanEvent(employee, DateTime.Now, db, sysToInsert));
db.SaveChanges();
}
}
catch (Exception ex)
{
MessageBox.Show("Database Error: " + ex.Message);
}
}
}
}