-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimalsHandler.cs
342 lines (332 loc) · 12 KB
/
AnimalsHandler.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;
namespace AnimalsWPF
{
class AnimalsHandler
{
private static bool _haveDBContext = false;
public int allCatsIndex = 0;
public int allDogsIndex = 0;
public AnimalsDBEntities ctx = null;
public List<Cat> allCatsList = null;
public List<Dog> allDogsList = null;
public List<Cat> catsList = null;
public List<CatBreed> catBreedsList = null;
public List<Animal> animalsList = null;
public List<Dog> dogsList = null;
public List<DogBreed> dogBreedsList = null;
public List<Domecile> domecilesList = null;
public AnimalsHandler()
{
//MessageBox.Show("In constructor");
if(!_haveDBContext)
{
try
{
ctx = new AnimalsDBEntities();
catsList = ctx.Cats.ToList();
catBreedsList = ctx.CatBreeds.ToList();
animalsList = ctx.Animals.ToList();
dogsList = ctx.Dogs.ToList();
dogBreedsList = ctx.DogBreeds.ToList();
domecilesList = ctx.Domeciles.ToList();
allCatsList = ctx.Cats.Include("CatBreed1").Include("Animal").Include("Domecile").ToList();
allDogsList = ctx.Dogs.Include("DogBreed").Include("Animal").Include("Domecile").ToList();
_haveDBContext = true;
}
catch (Exception e)
{
throw e;
}
}
else
{
MessageBox.Show("already have DB context");
}
}
public void UpdateCatsIndex()
{
if (this.allCatsIndex >= this.catsList.Count() - 1)
{
this.allCatsIndex = 0;
}
else
{
this.allCatsIndex += 1;
}
}
public void UpdateDogsIndex()
{
if (this.allDogsIndex >= this.dogsList.Count() - 1)
{
this.allDogsIndex = 0;
}
else
{
this.allDogsIndex += 1;
}
}
public int GetKeyDomecile(string tempDomecileName)
{
for (int i=0; i < domecilesList.Count(); ++i)
if(domecilesList[i].DomecileName == tempDomecileName)
{
return domecilesList[i].DomecileID;
}
//logic to update Domeciles table with the new name and return the new key
Domecile tempRecord = new Domecile();
tempRecord.DomecileName = tempDomecileName;
ctx.Domeciles.Add(tempRecord);
ctx.SaveChanges();
this.domecilesList.Add(tempRecord);
//MessageBox.Show(tempRecord.DomecileID.ToString());
return tempRecord.DomecileID;
}
public int GetKeyDogBreed(string tempDogBreedName)
{
for (int i = 0; i < dogBreedsList.Count(); ++i)
if (dogBreedsList[i].DogBreedName == tempDogBreedName)
{
return dogBreedsList[i].DogBreedID;
}
//logic to update dog breeds table with the new name and return the new key
DogBreed tempRecord = new DogBreed();
tempRecord.DogBreedName = tempDogBreedName;
ctx.DogBreeds.Add(tempRecord);
ctx.SaveChanges();
this.dogBreedsList.Add(tempRecord);
//MessageBox.Show(tempRecord.DomecileID.ToString());
return tempRecord.DogBreedID;
}
public int GetKeyCatBreed(string tempCatBreedName)
{
for (int i = 0; i < catBreedsList.Count(); ++i)
if (catBreedsList[i].CatBreedName == tempCatBreedName)
{
return catBreedsList[i].CatBreedID;
}
//logic to update catbreeds table with the new name and return the new key
CatBreed tempRecord = new CatBreed();
tempRecord.CatBreedName = tempCatBreedName;
ctx.CatBreeds.Add(tempRecord);
ctx.SaveChanges();
this.catBreedsList.Add(tempRecord);
//MessageBox.Show(tempRecord.DomecileID.ToString());
return tempRecord.CatBreedID;
}
public int GetKeyAnimal(string tempName, string sTempWeight, string sTempBirthday)
{
for(int i = 0; i < animalsList.Count(); ++i)
{
if (animalsList[i].Name == tempName)
{
MessageBox.Show(tempName + " is already known to us.");
return animalsList[i].AnimalID;
}
}
// Convert sTempWeight to a double
double tempWeight = Convert.ToDouble(sTempWeight);
// Convert sTempBirthday to a DateTime
DateTime tempBirthday = Convert.ToDateTime(sTempBirthday);
//MessageBox.Show("Adding Animal" + tempName + tempWeight.ToString() + tempBirthday.ToString());
//logic to update animals table with the new name and return the new key
Animal tempRecord = new Animal();
tempRecord.Name = tempName;
tempRecord.Weight = tempWeight;
tempRecord.Birthday = tempBirthday;
ctx.Animals.Add(tempRecord);
ctx.SaveChanges();
this.animalsList.Add(tempRecord);
return tempRecord.AnimalID;
}
public bool AddCritter(string tempAnimalType, int tempAnimalID,
int tempBreedID, int tempDocecileID, bool tempHobbby)
{
bool isNewCritter = true;
switch (tempAnimalType)
{
case "Dog":
foreach (Dog tempDog in allDogsList)
{
if((tempDog.AnimalID == tempAnimalID) &&
(tempDog.DomecileID == tempDocecileID))
{
// Critter of type dog living at domecile is already in the database, do nothing
isNewCritter = false;
return isNewCritter;
}
}
// This is a new to us dog, add to database.
Dog addMe = new Dog();
addMe.AnimalID = tempAnimalID;
addMe.DogBreedID = tempBreedID;
addMe.DomecileID = tempDocecileID;
addMe.PlaysFetch = tempHobbby;
ctx.Dogs.Add(addMe);
ctx.SaveChanges();
this.dogsList.Add(addMe);
isNewCritter = true;
break;
case "Cat":
foreach (Cat tempCat in allCatsList)
{
if ((tempCat.AnimalID == tempAnimalID) &&
(tempCat.DomecileID == tempDocecileID))
{
// Critter of type cat living at domecile is already in the database, do nothing
isNewCritter = false;
return isNewCritter;
}
}
// This is a new to us cat, add to database.
Cat addMe2 = new Cat();
addMe2.AnimalID = tempAnimalID;
addMe2.CatBreed = tempBreedID;
addMe2.DomecileID = tempDocecileID;
addMe2.LikesCatnip = tempHobbby;
ctx.Cats.Add(addMe2);
ctx.SaveChanges();
isNewCritter = true;
this.catsList.Add(addMe2);
break;
default:
throw new InvalidOperationException();
}
return isNewCritter;
}
public string GetCatBreed(int tempBreedKey)
{
for (int i=0; i < catBreedsList.Count(); ++i)
{
if (tempBreedKey == this.catBreedsList[i].CatBreedID)
{
return this.catBreedsList[i].CatBreedName;
}
}
return "";
}
public Task<string> AsyncGetCatBreed(int tempBreedKey)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetCatBreed(tempBreedKey);
});
}
public string GetDogBreed(int tempBreedKey)
{
for (int i = 0; i < dogBreedsList.Count(); ++i)
{
if (tempBreedKey == this.dogBreedsList[i].DogBreedID)
{
return this.dogBreedsList[i].DogBreedName;
}
}
return "";
}
public Task<string> AsyncGetDogBreed(int tempBreedKey)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetDogBreed(tempBreedKey);
});
}
public string GetDomecile(int tempDomecileKey)
{
for (int i = 0; i < domecilesList.Count(); ++i)
{
if (tempDomecileKey == this.domecilesList[i].DomecileID)
{
return this.domecilesList[i].DomecileName;
}
}
return "";
}
public Task<string> AsyncGetDomecile(int tempDomecileKey)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetDomecile(tempDomecileKey);
});
}
public Animal GetAnimal(int tempAnimalKey)
{
for (int i = 0; i < animalsList.Count(); ++i)
{
if (tempAnimalKey == this.animalsList[i].AnimalID)
{
return this.animalsList[i];
}
}
return null;
}
public Task<Animal> AsyncGetAnimal(int tempAnimalKey)
{
return Task.Run<Animal>(() =>
{
randomWait();
return this.GetAnimal(tempAnimalKey);
});
}
public void randomWait()
{
// Wait randomly 2-5 seconds
Random r = new Random(Guid.NewGuid().GetHashCode());
int sleepTime = r.Next(1000, 4000);
//MessageBox.Show(sleepTime.ToString());
Thread.Sleep(sleepTime);
}
public string GetAnimalName(Animal tempAnimal)
{
return tempAnimal.Name;
}
public Task<string> AsyncGetAnimalName(Animal tempAnimal)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetAnimalName(tempAnimal);
});
}
public string GetAnimalWeight(Animal tempAnimal)
{
return tempAnimal.Weight.ToString();
}
public Task<string> AsyncGetAnimalWeight(Animal tempAnimal)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetAnimalWeight(tempAnimal);
});
}
public string GetAnimalBirthday(Animal tempAnimal)
{
return tempAnimal.Birthday.ToString();
}
public Task<string> AsyncGetAnimalBirthday(Animal tempAnimal)
{
return Task.Run<string>(() =>
{
randomWait();
return this.GetAnimalBirthday(tempAnimal);
});
}
}
}