/
DataBaseModel.cs
986 lines (878 loc) · 33.7 KB
/
DataBaseModel.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
using EatInEurope.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
namespace EatInEurope
{
class DataBaseModel : IModel
{
DBConnect dBConnect;
public DataBaseModel(DBConnect dbConnect)
{
dBConnect = dbConnect;
CountriesOptions = getCountries();
if (CountriesOptions == null)
raiseError("getCountries");
TypesOptions = getStyles();
if (TypesOptions == null)
raiseError("getStyles");
/* CountriesOptions = new List<string> {
"Netherlands", "England","Swiss", "France","Germany"
};
*/
/* CitiesOptions = new List<string> {
"Amsterdam", "Athens","Barcelona", "Berlin","Bratislava","Brussels","Budapest",
"Copenhagen","Dublin","Edinburgh","Geneva","Hamburg","Helsinki","Krakow","Lisbon","Ljubljana",
"London","Luxembourg","Lyon","Madrid","Milan","Munich","Oporto","Oslo","Paris","Prague","Rome",
"Stockholm","Vienna","Warsaw","Zurich"
};
CountriesOptions = getCountries();
TypesOptions = new List<string> {
"Chinese", "Indian","vegeterian", "vegan","Italian"
};*/
// RestsResults = new List<Restaurant>
//{
//new List<string> {"Martine of Martine's Table","Amsterdam", "French", "Dutch", "European"
// , "5", "$$ - $$$", "136", "Just like home", "A Warm Welcome to Wintry Amsterdam", "/Restaurant_Review-g188590-d11752080-Reviews-Martine_of_Martine_s_Table-Amsterdam_North_Holland_Province.html" },
// new List<string> {"De Silveren Spiegel" ,"Amsterdam","Dutch", "European", "Vegetarian Friendly",
// "4.5","$$$$", "812", "Great food and staff", "just perfect","/Restaurant_Review-g188590-d693419-Reviews-De_Silveren_Spiegel-Amsterdam_North_Holland_Province.html" },
// new List<string> {"La Rive" ,"Amsterdam","Mediterranean", "French", "International",
// "4.5","$$$$", "567", "Satisfaction", "Delicious old school restaurant","/Restaurant_Review-g188590-d696959-Reviews-La_Rive-Amsterdam_North_Holland_Province.html"}
/*
new Restaurant("d11752080","Martine of Martine's Table","Netherlands", "Amsterdam",new List<string>{ "French", "Dutch", "European" }
, 5, "$$ - $$$", 136,new List<UserReview>{new UserReview("d11752080","gooooooooooooooood", "10/1/99", 0.0), new UserReview("d11752080","very recomend rest", "20/01/19", 2.5), new UserReview("d11752080","I LIKE THIS REST", "29/08/2000", 5) },
"/Restaurant_Review-g188590-d11752080-Reviews-Martine_of_Martine_s_Table-Amsterdam_North_Holland_Province.html", "Orel"),
new Restaurant("d693419", "De Silveren Spiegel","Netherlands", "Amsterdam",new List<string>{ "Dutch", "European", "Vegetarian Friendly", "Gluten Free Options" }
, 4.5, "$$$$", 812,new List<UserReview>(),
"/Restaurant_Review-g188590-d693419-Reviews-De_Silveren_Spiegel-Amsterdam_North_Holland_Province.html", "shani"),
new Restaurant("d696959", "La Rive","Netherlands", "Amsterdam",new List<string>{ "Mediterranean", "French", "International", "European", "Vegetarian Friendly", "Vegan Options" }
, 1.5, "$$$$", 567,new List<UserReview>(),
"/Restaurant_Review-g188590-d696959-Reviews-La_Rive-Amsterdam_North_Holland_Province.html", "reut")
};*/
//RestsResults = new List<List<string>> { };
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string PropName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(PropName));
}
}
// properties
private bool endOfRests;
public bool EndOfRests {
set {
endOfRests = value;
NotifyPropertyChanged("endOfRests");
}
get
{
return endOfRests;
}
}
private bool loadMoreRests = true;
public bool LoadMoreRests
{
set
{
loadMoreRests = value;
NotifyPropertyChanged("loadMoreRests");
queryContinue = true;
List<Restaurant> result = getRestByFilter();
if (result == null)
raiseError("getRestByFilter");
else
{
RestsResults.AddRange(result);
queryContinue = false;
}
queryContinue = false;
}
get
{
return loadMoreRests;
}
}
private string username;
public string UserName {
get { return username; }
set {
username = value;
NotifyPropertyChanged("username");
}
}
private string password;
public string Password
{
get { return password; }
set
{
password = value;
NotifyPropertyChanged("password");
//LoginOK = true;
RestsResults.Clear();
if (signIn(username, password))
{
LoginOK = true;
//get relevent rest
RestsResults = getRestByFilter();
if (RestsResults == null)
raiseError("getRestByFilter");
}
}
}
private string newPassword;
public string NewPassword
{
get { return newPassword; }
set
{
newPassword = value;
NotifyPropertyChanged("newPassword");
RestsResults.Clear();
UsernameFree = true;
if (!signIn(username, null))
{
if (register(username, newPassword) == false)
raiseError("register");
}
else
{
UsernameFree = false;
}
}
}
private bool isClient = false;
public bool IsClient {
set
{
isClient = value;
NotifyPropertyChanged("isClient");
if (isClient)
{
CountriesOptions = getCountries();
if (CountriesOptions == null)
raiseError("getCountries");
TypesOptions = getStyles();
if (TypesOptions == null)
raiseError("getStyles");
}
}
get {
return isClient;
}
}
private bool isEdit = false;
public bool IsEdit {
get
{
return isEdit;
}
set
{
isEdit = value;
if (isEdit)
{
CitiesOptions = getAllCities();
if (CitiesOptions == null)
raiseError("getAllCities");
}
NotifyPropertyChanged("isEdit");
}
}
private bool loginOK = false;
public bool LoginOK {
get { return loginOK; }
set {
loginOK = value;
NotifyPropertyChanged("loginOK");
}
}
private bool usernameFree = false;
public bool UsernameFree {
get {
// TODO: update flag by calling function checks username
return usernameFree;
}
set
{
usernameFree = value;
NotifyPropertyChanged("usernameFree");
}
}
private string[] top5RestValue = null;
private string[] top5Rests = new string[5];
public string[] Top5Rests {
get { return top5Rests; }
set {
top5Rests = value;
NotifyPropertyChanged("top5Rests");
}
}
private string countryFilter;
public string CountryFilter {
get { return countryFilter; }
set {
countryFilter = value;
NotifyPropertyChanged("countryFilter");
CitiesOptions = getCities();
if (CitiesOptions == null)
raiseError("getCities");
}
}
private List<string> citiesFilter = new List<string>();
public List<string> CitiesFilter {
get { return citiesFilter; }
set
{
citiesFilter = value;
NotifyPropertyChanged("citiesFilter");
}
}
private List<string> typesFilter = new List<string>();
public List<string> TypesFilter {
get { return typesFilter; }
set
{
typesFilter = value;
NotifyPropertyChanged("typesFilter");
}
}
private bool startSearch;
public bool StartSearch {
set {
startSearch = value;
NotifyPropertyChanged("startSearch");
if (startSearch)
{
RestsResults = getRestByFilter();
if (RestsResults == null)
raiseError("getRestByFilter");
}
}
get
{
return startSearch;
}
}
private List<string> countriesOptions = new List<string>();
public List<string> CountriesOptions
{
get { return countriesOptions; }
set
{
countriesOptions = value;
NotifyPropertyChanged("countriesOptions");
}
}
private List<string> citiesOptions = new List<string>();
public List<string> CitiesOptions
{
get { return citiesOptions; }
set
{
citiesOptions = value;
NotifyPropertyChanged("citiesOptions");
}
}
private List<string> typesOptions = new List<string>();
public List<string> TypesOptions
{
get { return typesOptions; }
set
{
typesOptions = value;
NotifyPropertyChanged("typesOptions");
}
}
private List<double> ratesFilter = new List<double>();
public List<double> RateFilter {
get { return ratesFilter; }
set
{
ratesFilter = value;
NotifyPropertyChanged("ratesFilter");
}
}
private List<string> priceFilter = new List<string>();
public List<string> PriceFilter {
get { return priceFilter; }
set
{
priceFilter = value;
NotifyPropertyChanged("priceFilter");
}
}
private string order;
public string Order {
get { return order; }
set
{
order = value;
NotifyPropertyChanged("order");
}
}
private bool asc;
public bool Asc {
get { return asc; }
set
{
asc = value;
NotifyPropertyChanged("asc");
RestsResults = getRestByFilter();
if (RestsResults == null)
raiseError("getRestByFilter");
}
}
private string restID;
public string RestID {
get { return restID; }
set
{
// Delete this restId from the DB
restID = value;
NotifyPropertyChanged("restID");
}
}
private string restName;
public string RestName {
get { return restName; }
set
{
restName = value;
NotifyPropertyChanged("restName");
if (restName != null)
{
RestsResults = getRestByFilter();
if (RestsResults == null)
raiseError("getRestByFilter");
}
}
}
private double restRating;
public double RestRating {
get
{
restRating = RestsResults[restsResults.FindIndex(x => x.ID == RestID)].Rate;
return restRating;
}
}
public Restaurant RestDetails
{
get
{
if (restID != null)
{
if (!IsEdit)
{
Restaurant rest = restDetails();
if (rest == null)
{
raiseError("restDetails");
return null;
}
return rest;
}
return RestsResults[restsResults.FindIndex(x => x.ID == restID)];
} else
{
return null;
}
}
set
{
RestsResults[restsResults.FindIndex(x => x.ID == restID)] = value;
bool result = updateRestaurant(value);
if (result == false)
raiseError("updateRestaurant");
NotifyPropertyChanged("RestDetails");
}
}
private string idToRemove;
public string IDToRemove {
get
{
return idToRemove;
}
set
{
idToRemove = value;
NotifyPropertyChanged("idToRemove");
if(!deleteRest())
{
raiseError("deleteRest");
}
}
}
private UserReview newReview;
public UserReview NewReview {
get { return newReview; }
set
{
newReview = value;
RestsResults.Find(x => x.ID == restID).Reviews.Add(value);
NotifyPropertyChanged("newReview");
bool result = addReview(newReview);
if (result == false)
raiseError("addReview");
}
}
private bool isAddRest;
public bool IsAddRest {
get {
return isAddRest;
}
set {
isAddRest = value;
NotifyPropertyChanged("isAddRest");
}
}
private List<Restaurant> restsResults = new List<Restaurant>();
public List<Restaurant> RestsResults
{
get { return restsResults; }
set
{
if (IsAddRest)
{
bool result = addRest(restsResults[restsResults.Count - 1]);
if (result == false)
raiseError("addRest");
IsAddRest = false;
}
restsResults = value;
NotifyPropertyChanged("restsResults");
}
}
private Dictionary<string, int> countriesPartStyle = new Dictionary<string, int>();
public Dictionary<string, int> CountriesPartStyle
{
get
{
Dictionary<string, int> dic = graphCountriesByType("VeganOptions");
if (dic != null)
return dic;
raiseError("CountriesPartStyle");
return null;
}
}
bool queryContinue = false;
//TODO update endReading
// functions
private List<string> calculatePriceRange()
{
List<string> priceRange = new List<string>();
if (PriceFilter[0] == "$" && PriceFilter[1] == "$$$$$")
return priceRange;
int priceFrom = 0;
int priceto = 0;
if (PriceFilter[0] == "$")
priceFrom = 1;
else if (PriceFilter[0] == "$$")
priceFrom = 2;
else if (PriceFilter[0] == "$$$")
priceFrom = 3;
else if (PriceFilter[0] == "$$$$")
priceFrom = 4;
else
priceFrom = 5;
if (PriceFilter[1] == "$")
priceto = 1;
else if (PriceFilter[1] == "$$")
priceto = 2;
else if (PriceFilter[1] == "$$$")
priceto = 3;
else if (PriceFilter[1] == "$$$$")
priceto = 4;
else
priceto = 5;
List<List<int>> tmp = new List<List<int>>();
for (int i = priceto; i >= priceFrom; i--)
{
List<int> range = new List<int>();
range.Add(i);
range.Add(-1);
tmp.Add(range);
for (int j = i-1; j>= priceFrom; j--)
{
List<int> range1 = new List<int>();
range1.Add(j);
range1.Add(i);
tmp.Add(range1);
}
}
foreach(List<int> range in tmp)
{
if (range[1] != -1)
priceRange.Add(priceToStr(range[0]) + " - " + priceToStr(range[1]));
else
priceRange.Add(priceToStr(range[0]));
}
return priceRange;
}
private string priceToStr(int price)
{
switch (price)
{
case 1:
return "$";
case 2:
return "$$";
case 3:
return "$$$";
case 4:
return "$$$$";
case 5:
return "$$$$$";
default://-
return null;
}
}
private double calculateRate(double value)
{
return (Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2);
}
public void raiseError(string error)
{
string error1 = "An error occurred while connecting to the database";
MessageBox.Show(error1);
}
// create data base queries:
public bool deleteRest()
{
//TODO CHANGE ID
return dBConnect.Delete("restaurants", "ID_TA=" +IDToRemove );
}
public List<Restaurant> getTop5Rest()
{
//select * from restaurants where Price_Range LIKE '$' order by Rating LIMIT 5;
top5RestValue[0] = " Price_Range LIKE '$' And ";
top5RestValue[1] = " order by Rating LIMIT 5";
List<Restaurant> top5 = getRestByFilter();
if (top5 == null)
return null;
top5RestValue[0] = null;
top5RestValue[1] = null;
return top5;
}
public bool register(string username, string password)
{
// check if not exist:
/* List<string> result = dBConnect.Check_existing(username, password, "owners");
if (result == null)
throw new Exception("error from db");
if (result[0].Equals("0"))
{
return false;
}*/
List<string> values = new List<string> { username, password };
bool result = dBConnect.Insert("('" + username + "','" + password +"')", "owners");
if (result == false)
return false;
UserName = username;
return true;
}
public List<Restaurant> getRestByFilter()
{
// create sql query
string select = "restaurants.ID_TA, restaurants.name, city.Name as city, country.Name as country, restaurants.rating, restaurants.owner";
string from = "restaurants join city on restaurants.city_id = city.id join country on city.countrycode = country.code";
string where;
if (isClient == true)
{
if (RestName == null)
{
where = "restaurants.city_id = (select city.id from city where city.name = '" + CitiesFilter[0] + "')";
if (TypesFilter.Count != 0)
{
if (TypesFilter[0] != null)
{
where += " And restaurants.ID_TA IN (select ID_TA from style_rest join style on style_rest.styleid = style.id where style.style='" + TypesFilter[0] + "'";
for (int i = 1; i < TypesFilter.Count; i++)
{
where += " OR style.style='" + TypesFilter[i] + "'";
}
where += ")";
}
}
List<string> ranges = calculatePriceRange();
if (ranges.Count != 0)
{
where += " And (restaurants.Price_Range = '" + ranges[0] + "'";
for (int i = 1; i < ranges.Count; i++)
{
where += " OR Price_Range = '" + ranges[i] + "'";
}
where += ")";
}
if (RateFilter.Count != 0)
{
where += " And restaurants.rating Between " + RateFilter[0].ToString() + " AND " + RateFilter[1].ToString();
}
}
else
{
where = " restaurants.Name LIKE '%" + RestName + "%'";
}
}
else
{
where = "restaurants.owner = '" + UserName + "'";
}
/*if (top5RestValue[0] != null)
{
//add filter of 5 top rest
where = top5RestValue[0] + where + top5RestValue[1];
}*/
string orderByValue = null;
string order = null;
if (Order != null)
{
orderByValue = Order;
if (Asc == true)
order = "Asc";
else
order = "Desc";
}
bool endReading = false;
List<Restaurant> all_rest = new List<Restaurant>();
List<string>[] rest = dBConnect.SelectRest(from, where , orderByValue, order, select,10, queryContinue, ref endReading);
if (endReading == true)
EndOfRests = true;
else
EndOfRests = false;
if (rest == null)
return null;
select = "restaurants.ID_TA, style.style";
from = "restaurants join style_rest on restaurants.ID_TA = style_rest.ID_TA join style on style.id = style_rest.styleid";
for (int i = 0; i < rest[0].Count ;i++)
{
Restaurant new_rest = new Restaurant(rest[0][i], rest[1][i], rest[4][i], rest[2][i], null, calculateRate(Convert.ToDouble(rest[3][i])),null, -1 ,null, null, rest[5][i]);
// condition for specific id -restaurant-styles
string id = rest[0][i];
where = " restaurants.ID_TA='" + id + "'";
List<string>[] dbStyles = dBConnect.Select(from, where, null, null, select,1);
if (dbStyles == null)
return null;
List<string> styles = new List<string>();
if (dbStyles[9].Count != 0)
{
styles.Add(dbStyles[9][0]);
new_rest.Types = styles;
}
all_rest.Add(new_rest);
}
return all_rest;
}
public bool signIn(string username, string password)
{
List<string> result = dBConnect.Check_existing(username, password, "owners");
if (result == null) {
raiseError("signIn");
return false;
}
if (result[0].Equals("0")) {
return false;
}
UserName = username;
return true;
}
public Restaurant restDetails()
{
Restaurant currRest = RestsResults[restsResults.FindIndex(x => x.ID == RestID)];
// create sql query
string select = "restaurants.price_range, restaurants.Numbers_of_reviews, restaurants.url_TA ";
string from = "restaurants";
string where = "restaurants.ID_TA = " + currRest.ID;
List<string>[] rest = dBConnect.Select(from, where, null, null, select,-1);
if (rest == null)
return null;
select = "restaurants.ID_TA, style.style";
from = "restaurants inner join style_rest on restaurants.ID_TA = style_rest.ID_TA inner join style on style.id = style_rest.styleid";
for (int i = 0; i < rest[0].Count; i++)
{
currRest.PriceRange = rest[4][i];
currRest.NumOfReviews = Convert.ToInt32(rest[7][i]);
currRest.URL = rest[5][i];
// condition for specific id -restaurant-styles
;
where = " restaurants.ID_TA='" + currRest.ID + "' ";
List<string>[] dbStyles = dBConnect.Select(from, where, null, null, select,3);
if (dbStyles == null)
return null;
List<string> styles = new List<string>();
for (int j = 0; j < dbStyles[0].Count; j++)
{
styles.Add(dbStyles[9][j]);
}
// get -restaurant -reviews
List<string>[] dbReviews = dBConnect.Select("reviews", "ID_TA= " + currRest.ID , null, null, null,-1);
if (dbReviews == null)
return null;
List<UserReview> listReviews = new List<UserReview>();
for (int j = 0; j < dbReviews[0].Count; j++)
{
double rate;
if (Convert.ToDouble(dbReviews[12][j]) == -1)
rate = calculateRate(currRest.Rate);
else
rate = calculateRate(Convert.ToDouble(dbReviews[12][j]));
UserReview newReview = new UserReview(currRest.ID, dbReviews[10][j], dbReviews[11][j], rate);
listReviews.Add(newReview);
}
currRest.Types = styles;
currRest.Reviews = listReviews;
}
return currRest;
}
public bool addReview(UserReview userReview)
{
// insert into reviews values('ID_TA', 'review', 'date');
bool result = dBConnect.Insert("(" + userReview.RestID + ", \"" + userReview.Content + "\" ,\"" + userReview.Date + "\" , " + userReview.Rate.ToString() + ")", "reviews");
if (result ==false)
{
return false;
}
List<string>[] data = dBConnect.Select("restaurants", "ID_TA = '" + userReview.RestID + "'", null, null, null,-1);
if (data == null)
return false;
int numOfReviews = Int32.Parse(data[7][0]);
double rate = Convert.ToDouble(data[3][0]);
double newRate = (numOfReviews * rate + userReview.Rate) / (numOfReviews + 1);
// UPDATE restaurants SET Num_of_reviews = 'num', Rating =rate WHERE ID_TA = 'ID_TA';
result = dBConnect.Update("restaurants","Numbers_of_reviews= "
+ (numOfReviews+1).ToString() + " ,Rating= \"" + newRate.ToString() + "\"", "ID_TA= \"" + userReview.RestID + "\"");
if (result == false)
{
return false;
}
RestsResults[restsResults.FindIndex(x => x.ID == userReview.RestID)].Rate = newRate;
return true;
}
public bool addStyle()
{
//TODO CHANGE
//insert into style_rest
//select 'ID_TA' , style.id from style where style.style = 'French';
/*string select = "'"+ID_TA+"' , style.id from style";
string where = " style.style = '" + style + "'";
bool result = dBConnect.InsertSelect(select,where, "style_rest");
if (result == false)
{
return false;
}*/
return true;
}
public bool addRest(Restaurant rest)
{
bool price = false;
bool url = false;
string select = "'" + rest.Name + "', city.id, 0";
if (rest.PriceRange != null)
{
select += ", '" + rest.PriceRange + "'";
price = true;
}
select += ", 0";
if (!rest.URL.Equals(""))
{
select += ", '" + rest.URL + "'";
url = true;
}
select += ", '" + username + "' from city";
string where = "city.name = '" + rest.City + "'";
string values = "name, city_id,rating ,";
if (price)
values += "price_range,";
values += "numbers_of_reviews,";
if (url)
values += "url_ta,";
values += "owner ";
// insert restaurant
bool result = dBConnect.InsertSelect(select, where, "restaurants", values);
if (result == false)
return false;
int id = dBConnect.select_last_inserted_id();
if (id == -1)
return false;
// insert styles
if (rest.Types.Count != 0)
{
select = id.ToString() + ", style.id from style ";
foreach (string type in rest.Types)
{
where = " style.style = \'" + type + "\';";
result = dBConnect.InsertSelect(select, where, "style_rest", null);
if (result == false)
return false;
}
}
int new_id = dBConnect.select_last_inserted_id();
if (new_id == -1)
return false;
restsResults[RestsResults.Count - 1].ID = new_id.ToString();
return true;
}
public Dictionary<string, int> graphCountriesByType(string type)
{
string query = "select country.name ,count(*) from restaurants join city on restaurants.city_id = city.id join country on city.countrycode = country.code where restaurants.ID_TA in (select ID_TA from style_rest where styleid =(select id from style where style = '" + TypesFilter[0] + "'))";
query += " group by country.name order by count(*) Desc Limit 5";
List<string>[] result = dBConnect.Count(query);
Dictionary<string, int> dic = new Dictionary<string, int>();
if (result == null) {
return null;
}
double total = 0;
for (int i = 0; i < result[0].Count; i++)
{
total += Convert.ToInt32(result[1][i]);
}
double percent = 0;
int sumPercent = 0;
for (int i = 0; i < result[0].Count-1; i++)
{
percent = (Convert.ToInt32(result[1][i]) / total) * 100;
if (Convert.ToInt32(percent) == 0)
continue;
sumPercent += Convert.ToInt32(percent);
dic.Add(result[0][i], Convert.ToInt32(percent));
}
if((100-sumPercent) != 0)
dic.Add(result[0][result[0].Count-1], 100-sumPercent);
/*dic.Add("france", 5);
dic.Add("germany", 22);
dic.Add("Netherlands", 27);
dic.Add("England", 46);*/
return dic;
}
public List<string> getCountries()
{
return dBConnect.SelectColumn("country", null, null, null, "name");
}
public List<string> getCities()
{
string whereCond = "countrycode = (select code from country where name = \"" + countryFilter + "\")";
return dBConnect.SelectColumn("city", whereCond ,"name", "ASC", "name");
}
public List<string> getAllCities()
{
return dBConnect.SelectColumn("city", null, "name", "Asc", "name");
}
public List<string> getStyles()
{
return dBConnect.SelectColumn("style", null , "style", "Asc", "style");
}
public bool updateRestaurant(Restaurant rest)
{
string set = "name='" + rest.Name + "' ,URL_TA= '" + rest.URL + "' ,price_range= '" + rest.PriceRange + "' ,restaurants.city_id = (SELECT city.id FROM city WHERE city.name = '" + rest.City + "')";
return dBConnect.Update("restaurants", set, "ID_TA = " + rest.ID);
}
}
}