-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
338 lines (270 loc) · 12 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MaterialSkin.Controls;
using MaterialSkin.Animations;
using MaterialSkin;
namespace Library_PR
{
public partial class Form1 : MaterialForm
{
public CustomerModel currCustomer;
BookModel currentBook;
OrderModel currentOrder;
public List<BookModel> books = new List<BookModel>();
public List<BookModel> busketList = new List<BookModel>();
public List<CustomerModel> customersForCheck = new List<CustomerModel>();
public Dictionary<BookModel, int> busketDictionary = new Dictionary<BookModel, int>();
List<OrderModel> ordersKek = new List<OrderModel>();
List<OrderBookModel> orderBookModels = new List<OrderBookModel>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
logoutBtn.Visible = false;
loginBtn.Visible = true;
hello2Label.Text = "Вітаю! \nБудь ласка авторизуйтесь";
Reload_Form();
}
public void Reload_Form()
{
books = SqliteDataAccess.LoadBooks();
bunifuCustomDataGrid1.DataSource = books;
bunifuCustomDataGrid1.Columns[0].Visible = false;
bunifuCustomDataGrid1.Columns[5].Visible = false;
bunifuCustomDataGrid1.Size = new Size(910, 450);
if (currCustomer == null)
{
logoutBtn.Visible = false;
panel1.Visible = true;
loginBtn.Visible = true;
helloLabel.Visible = false;
adminAddBookGroupBox.Visible = false;
addToBusketGroupBox.Visible = false;
hello2Label.Text = "Вітаю! Будь ласка авторизуйтесь";
}
else
{
logoutBtn.Visible = true;
panel1.Visible = false;
loginBtn.Visible = false;
helloLabel.Visible = true;
helloLabel.Text = "Привіт, "+currCustomer.Name;
hello2Label.Text = "Привіт, " + currCustomer.Name;
if (currCustomer.Login.Equals("admin"))
{
adminAddBookGroupBox.Visible = true;
addToBusketGroupBox.Visible = false;
tabPage3.Text = "Замовлення";
ordersKek = SqliteDataAccess.LoadOrders();
ordersDataGrid.DataSource = ordersKek;
orderBookModels = SqliteDataAccess.LoadInfoAboutOrdered();
orderBookBataGrid.DataSource = orderBookModels;
busketGroupBox.Visible = false;
ordersGroupBox.Visible = true;
bunifuCustomDataGrid1.Columns[0].Visible = true;
bunifuCustomDataGrid1.Columns[5].Visible = true;
bunifuCustomDataGrid1.Size = new Size(910, 330);
}
else
{
busketGroupBox.Visible = true;
ordersGroupBox.Visible = false;
tabPage3.Text = "Кошик";
adminAddBookGroupBox.Visible = false;
addToBusketGroupBox.Visible = true;
bunifuCustomDataGrid1.Size = new Size(910, 350);
}
}
}
private void loginBtn_Click(object sender, EventArgs e)
{
(new RegistrationLoginForm(this)).ShowDialog();
}
private void logoutBtn_Click(object sender, EventArgs e)
{
currCustomer = null;
Reload_Form();
}
private void materialSingleLineTextField1_TextChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
BindingList<BookModel> myObjList = new BindingList<BookModel>(books);
if (comboBox1.Text.Equals("Title"))
{
BindingList<BookModel> filtered = new BindingList<BookModel>(myObjList.Where(obj => obj.Title.Contains(materialSingleLineTextField1.Text)).ToList());
bunifuCustomDataGrid1.DataSource = filtered;
bunifuCustomDataGrid1.Update();
}
else if (comboBox1.Text.Equals("Author"))
{
BindingList<BookModel> filtered = new BindingList<BookModel>(myObjList.Where(obj => obj.Author.Contains(materialSingleLineTextField1.Text)).ToList());
bunifuCustomDataGrid1.DataSource = filtered;
bunifuCustomDataGrid1.Update();
}
else if (comboBox1.Text.Equals("Type"))
{
BindingList<BookModel> filtered = new BindingList<BookModel>(myObjList.Where(obj => obj.Type.Contains(materialSingleLineTextField1.Text)).ToList());
bunifuCustomDataGrid1.DataSource = filtered;
bunifuCustomDataGrid1.Update();
}
else if (comboBox1.Text.Equals("Pages"))
{
BindingList<BookModel> filtered = new BindingList<BookModel>(myObjList.Where(obj => obj.Pages.ToString().Contains(materialSingleLineTextField1.Text)).ToList());
bunifuCustomDataGrid1.DataSource = filtered;
bunifuCustomDataGrid1.Update();
}
}
private void bunifuCustomDataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
if (bunifuCustomDataGrid1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
currentBook = (BookModel)bunifuCustomDataGrid1.CurrentRow.DataBoundItem; //Отрмую нажату книгу
attentionLabel.Text = "Вибрана книга: " + currentBook;
}
}
}
private void materialSingleLineTextField1_Click(object sender, EventArgs e)
{
}
private void addToBasketBtn_Click(object sender, EventArgs e)
{
if (currCustomer != null)
{
if (currentBook.On_Storage > 0)
{
busketList.Add(currentBook);
if (busketDictionary.ContainsKey(currentBook))
{
DeleteItemsByKey(currentBook, busketDictionary);
}
busketDictionary.Add(currentBook, GetNumOfBooksInList(currentBook, busketList));
attentionLabel.Text = "Товар був добавлений у корзину";
}
else
{
MessageBox.Show("На складі товар відсутній!");
}
}
else
{
MessageBox.Show("Будь ласка авторизуйтесь!");
}
}
private void tabPage2_Validating(object sender, CancelEventArgs e)
{
//busketDataGrid2.DataSource = busketList;
//busketDataGrid2.Update();
var source = new BindingSource();
//source.DataSource = busketList;
source.DataSource = (from d in busketDictionary orderby d.Value select new { d.Key, d.Value }).ToList();
busketDataGrid2.DataSource = source;
busketDataGrid2.Columns[0].HeaderText = "Book";
busketDataGrid2.Columns[1].HeaderText = "Amount";
}
private int GetNumOfBooksInList(BookModel book, List<BookModel> bookModels)
{
int count=0;
foreach (BookModel bookModel in bookModels)
{
if (book.Book_Id == bookModel.Book_Id)
{
count++;
}
}
return count;
}
private void DeleteItemsByKey(BookModel bookKey, Dictionary <BookModel, int> busketDictionary)
{
busketDictionary.Remove(bookKey);
}
private void orderBtn_Click(object sender, EventArgs e)
{
DateTime dateTime = DateTime.UtcNow.Date;
currentOrder = new OrderModel(currCustomer.Customer_Id, dateTime.ToString("dd/MM/yyyy"));
SqliteDataAccess.SaveOrders(currentOrder);
List<OrderModel> orders = SqliteDataAccess.LoadOrders();
foreach (KeyValuePair<BookModel, int> entry in busketDictionary)
{
SqliteDataAccess.SaveOrderedBook(orders[orders.Capacity-1].Order_Id, entry.Key.Book_Id, entry.Value);
SqliteDataAccess.UpdateStorageByBookID(entry.Key.Book_Id, entry.Key.On_Storage - entry.Value);
}
MessageBox.Show("Замовлення прийнято!");
busketDictionary.Clear();
busketList.Clear();
var source = new BindingSource();
source.DataSource = (from d in busketDictionary orderby d.Value select new { d.Key, d.Value }).ToList();
busketDataGrid2.DataSource = source;
busketDataGrid2.Columns[0].HeaderText = "Book";
busketDataGrid2.Columns[1].HeaderText = "Amount";
}
private void tabPage4_Validating(object sender, CancelEventArgs e)
{
idLabel.Text = "ID: "+currCustomer.Customer_Id;
currNameTextBox.Text = currCustomer.Name;
currLoginTextBox.Text = currCustomer.Login;
currEmailTextBox.Text = currCustomer.Email;
currPassTextBox.Text = currCustomer.Password;
}
private void submitChangesBtn_Click(object sender, EventArgs e)
{
customersForCheck = SqliteDataAccess.LoadCustomers();
attenLabel.Visible = false;
bool flag = false;
foreach (CustomerModel customer in customersForCheck)
{
if (customer.Customer_Id != currCustomer.Customer_Id)
{
if (customer.Login.Equals(currLoginTextBox.Text) || customer.Email.Equals(currEmailTextBox.Text))
{
flag = true;
break;
}
}
}
if (!flag)
{
if(!(currNameTextBox.Equals("") || currLoginTextBox.Equals("") || currEmailTextBox.Equals("") || currPassTextBox.Equals("")))
{
SqliteDataAccess.UpdateCustomerByID(currCustomer.Customer_Id, new CustomerModel(currNameTextBox.Text, currLoginTextBox.Text, currEmailTextBox.Text, currPassTextBox.Text));
attenLabel.Visible = true;
attenLabel.Text = "Дані оновлено!";
}
else
{
attenLabel.Visible = true;
attenLabel.Text = "Всі поля повинні бути заповнені!";
}
}
else
{
attenLabel.Visible = true;
attenLabel.Text = "Користувач з таким логіном або поштою уже присутній";
}
}
private void addBookBtn_Click(object sender, EventArgs e)
{
if(!(titleTB.Text.Equals("") || authorTB.Text.Equals("") || pagesTB.Text.Equals("") || typeTB.Text.Equals("") || storageTB.Text.Equals("")))
{
BookModel tempBook = new BookModel(titleTB.Text, authorTB.Text, typeTB.Text, Convert.ToInt32(pagesTB.Text), Convert.ToInt32(storageTB.Text));
tempBook.Book_Id = books.Capacity+1;
SqliteDataAccess.SaveBooks(tempBook);
MessageBox.Show("Нова книга добавлена");
Reload_Form();
}
}
private void hello2Label_Click(object sender, EventArgs e)
{
}
}
}