-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBook.page.xaml.cs
More file actions
281 lines (241 loc) · 8.34 KB
/
Copy pathAddBook.page.xaml.cs
File metadata and controls
281 lines (241 loc) · 8.34 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
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
using Google.Protobuf.WellKnownTypes;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Asn1.Ocsp;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
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.Xml.Linq;
namespace Library_DB
{
public partial class AddBook : Page
{
ListBooks listPage = new ListBooks();
//SQL comands //TODO put in maneger klass
MySqlConnection conn;
int id; // book ID- id variable that saves id from row that is being selected
TextBox[] txtBoxes;
public AddBook(int id) //konstruktor med parameter
{
this.id = id;
InitializeComponent();
ConnPath();
fillCombo();
txtBoxes = new TextBox[] { authorTbx, pageTbx, TitleTbx, yearTbx, langTbx};
}
public AddBook() //konstruktor
{
InitializeComponent();
ConnPath();
fillCombo();
txtBoxes = new TextBox[] { authorTbx, pageTbx, TitleTbx, yearTbx, langTbx };
}
public void ConnPath()
{
string filePath = @"C:\Users\tobia\OneDrive\Dokument\GitHub\Library_DB\Resources\lösen.txt";
string passFile = File.ReadAllText(filePath);
string server = "localhost";
string database = "librarydbmodel";
string user = "root";
string pass = passFile;
string connString = $"SERVER={server};DATABASE={database};UID={user};PASSWORD={pass};";
conn = new MySqlConnection(connString);
}
private void confirmBtn_Click(object sender, RoutedEventArgs e)
{
insertToTable();
}
void insertToTable()
{
string SQLquerry;
string nameA;
int page;
string nameB;
int year;
string nameL;
string cb2;
int favo;
bool valid = true;
foreach (TextBox textbox in txtBoxes)
{
if (textbox.Text == "") //if box null then no insert to databas from that box
{
textbox.Text = null;
}
else if(textbox.Text == "Author" || textbox.Text == "Pages" || textbox.Text == "Book Title" || textbox.Text == "Publishing year" || textbox.Text == "Language")
{
textbox.Text = null;
MessageBox.Show("Unvalide input");
return;
}
else if (typeCheckInt(txtBoxes[1].Text) != true || typeCheckInt(txtBoxes[3].Text) != true)
{
valid = false;
txtBoxes[1].Text = "Error";
txtBoxes[3].Text = "Error";
if(valid == false)
{
MessageBox.Show("Unvalid input! You can only write numbers here");
}
return;
}
}
if (cbx2.SelectedItem == null)
{
MessageBox.Show("Unvalid input! must choose category");
return;
}
//values from boxes to query
nameA = txtBoxes[0].Text;
page = Convert.ToInt32(txtBoxes[1].Text);
nameB = txtBoxes[2].Text;
year = Convert.ToInt32(txtBoxes[3].Text);
nameL = txtBoxes[4].Text;
cb2 = cbx2.SelectedItem.ToString();
favo = Convert.ToInt32(favoSlider.Value);
//sqlQuery
SQLquerry = $"CALL insert_toBooks('{nameB}', {page}, {year},'{nameA}','{cb2}',{favo},'{nameL}');";
MySqlCommand cmd = new MySqlCommand(SQLquerry, conn);
try
{
//Öppna koppling till DB
conn.Open();
//Exekvera SQL querry
cmd.ExecuteReader();
//stänger kopplingen till DB
conn.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
MessageBox.Show("Inserted successfully!");
clearAllboxes();
listPage.getTable();
Frame2.Content = listPage;
}
public bool typeCheckInt(String input)//check for int
{
int nr = 0;
return int.TryParse(input, out nr);
}
private void updateBtn1_Click(object sender, RoutedEventArgs e)
{
updateData();
}
public void updateData()
{
bool v = true;
string Title = TitleTbx.Text;
string AuthorName = authorTbx.Text;
int fav = Convert.ToInt32(favoSlider.Value);
int pages = Convert.ToInt32(pageTbx.Text.ToString());
if (typeCheckInt(txtBoxes[1].Text) != true || typeCheckInt(txtBoxes[1].Text) != true)
{
v = false;
txtBoxes[1].Text = "Error";
txtBoxes[3].Text = "Error";
if (v == false)
{
MessageBox.Show("Unvalid input! You can only write numbers here");
}
}
try
{
//Öppna kommunimation
conn.Open();
string SQLQuerry;
if (favoSlider.Value < 1)
{
pages = Convert.ToInt32(pageTbx.Text);
SQLQuerry = $"CALL UpdateData_procedure({id}, '{Title}',{pages}, null ,'{AuthorName}');";
}
else
{
SQLQuerry = $"CALL UpdateData_procedure({id}, '{Title}', {pages}, {fav},'{AuthorName}');";
}
MySqlCommand cmd = new MySqlCommand(SQLQuerry, conn);
cmd.ExecuteReader();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Updated Successfully!");
clearAllboxes();
listPage.getTable();
Frame2.Content = listPage;
}
//Clearing txtbxs
public void clearAllboxes()
{
authorTbx.Text = "Author";
pageTbx.Text = "Pages";
TitleTbx.Text = "Book Title";
yearTbx.Text = "Publishing year";
langTbx.Text = "Language";
}
void fillCombo()
{
conn.Open();
string SQLQuerry1 = $"SELECT * FROM categorys ";
MySqlCommand cmd = new MySqlCommand(SQLQuerry1, conn);
//Exekvera command
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString(1);
cbx2.Items.Add(name);
string genre = reader.GetString(2);
cbx1.Items.Add(genre);
}
conn.Close();
}
//favorit pointer Slider
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (favoSlider.Value == 0 || favoSlider.Value == 1)
{
favoLabl.Content = "LIKED";
}
else if (favoSlider.Value == 2)
{
favoLabl.Content = "Quite Favored";
}
else if (favoSlider.Value == 3)
{
favoLabl.Content = "Favored";
}
else if (favoSlider.Value == 4)
{
favoLabl.Content = "Very Favored";
}
}
private void favoChecked(object sender, RoutedEventArgs e)
{
if (favoCheck.IsChecked == true)
{
favoSlider.IsEnabled = true;
}
else if (favoCheck.IsChecked == false)
{
favoSlider.IsEnabled = false;
}
}
}
}